Powershell 解析帮助 - 如何将文件夹名称列表输出到文本文件中
我有一个简单的脚本,可以读取文件夹名称并将其输出到文本文件中。我意识到我得到的输出比我想要的多得多,因此我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
相关资源:
Try this one:
Related resources:
您可以使用 -Name 开关仅获取名称:
You can get just the names with the -Name switch: