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

发布于 2024-11-17 10:13:05 字数 711 浏览 2 评论 0原文

我有一个简单的脚本,可以读取文件夹名称并将其输出到文本文件中。我意识到我得到的输出比我想要的多得多,因此我使用 select-item cmdlet 从哈希表中仅选择 name 属性。问题是,仍然存在我省略的数据通常会填充的所有空白,这对我的问题没有帮助,因为空白会破坏我的脚本。

我尝试了一些 [regex] 命令来删除 (/S+) 中的空格,但我不太清楚我正在使用一些代码试图从某人帮助我的示例中进行调整。主题名称与此处的标题相同,并且也在该网站上。任何人都可以帮助我,我将不胜感激!

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


$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}

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.

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!

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天白云 2024-11-24 10:13:05

试试这个:

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

相关资源:

Try this one:

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

Related resources:

失而复得 2024-11-24 10:13:05

您可以使用 -Name 开关仅获取名称:

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

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

$accFolder = Read-Host "Enter the account folder container....: "
Get-ChildItem -Name "D:\influxcyst\$accFolder" | Out-File $HOME\desktop\$accFolder.txt
长发绾君心 2024-11-24 10:13:05
$accFolder = Read-Host "Enter the account folder container....:"
 cmd /c dir /b /ad D:\influxcyst\$accFolder > $HOME\desktop\$accFolder.txt 
$accFolder = Read-Host "Enter the account folder container....:"
 cmd /c dir /b /ad D:\influxcyst\$accFolder > $HOME\desktop\$accFolder.txt 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文