Powershell解析帮助—如何将文件夹名称列表输出到文本文件中

时间:2023-01-14 15:14:52

I have a simple script that reads the folder names and outputs them into a text file. I realized I got a lot more output then I wanted, so I used the select-item cmdlet to select just the name property from the hashtable. The problem is that there is still all the white space that the data I omitted would have normally filled, not helping my problem since the white space will destroy my script.

我有一个简单的脚本,它读取文件夹的名称并将其输出到文本文件中。我意识到我得到了比我想要的更多的输出,所以我使用select-item cmdlet从hashtable中选择name属性。问题是,我遗漏的数据仍然有所有的空白,这对我的问题没有帮助,因为空白会破坏我的脚本。

I have tried some [regex] commands to strip out the whitespace with (/S+) but I don't really know it that well I was using some code trying to tweak from an example someone helped me with. The topic name is the same as the title here and it is on this site too. Anyone that can help me I would appreciate it!

我尝试过一些[regex]命令,用(/S+)去掉空格,但我不太清楚,我使用了一些代码,试图从某人帮助我的示例中进行调整。主题名与这里的标题相同,它也在这个站点上。任何能帮助我的人我都很感激!

Basically, I cant figure out how to output the names of folders into a simple text file with ZERO whitespace (1 line per folder name).

基本上,我不知道如何将文件夹的名称输出到一个没有空格的简单文本文件中(每个文件夹名称一行)。


$accFolder = Read-Host "Enter the account folder container....: "

$dataArray = Get-ChildItem "D:\influxcyst\$accFolder" | select-object name

$dataArray
$dataArray | Out-File $HOME\desktop\$accFolder.txt

$newArray = get-content $HOME\desktop\$accFolder.txt

#[regex]$regex = "\s(\S+)\s"
#[regex]::matches($newArray,$regex) | foreach-object {$_.groups[1].value}

3 个解决方案

#1


15  

Try this one:

试试这个:

Get-ChildItem C:\Source\Path | ForEach-Object { $_.Name } > C:\Output\File.txt

Related resources:

相关资源:

#2


1  

You can get just the names with the -Name switch:

您可以通过-Name开关获得名称:

$accFolder = Read-Host "Enter the account folder container....: "
Get-ChildItem -Name "D:\influxcyst\$accFolder" | Out-File $HOME\desktop\$accFolder.txt

#3


0  

$accFolder = Read-Host "Enter the account folder container....:"
 cmd /c dir /b /ad D:\influxcyst\$accFolder > $HOME\desktop\$accFolder.txt 

#1


15  

Try this one:

试试这个:

Get-ChildItem C:\Source\Path | ForEach-Object { $_.Name } > C:\Output\File.txt

Related resources:

相关资源:

#2


1  

You can get just the names with the -Name switch:

您可以通过-Name开关获得名称:

$accFolder = Read-Host "Enter the account folder container....: "
Get-ChildItem -Name "D:\influxcyst\$accFolder" | Out-File $HOME\desktop\$accFolder.txt

#3


0  

$accFolder = Read-Host "Enter the account folder container....:"
 cmd /c dir /b /ad D:\influxcyst\$accFolder > $HOME\desktop\$accFolder.txt