【发布时间】:2019-11-12 08:58:16
【问题描述】:
我已经使用 Powershell 一天了,我需要使用循环返回文件夹中每个文件的文件名。这是我目前拥有的:
$filePath = 'C:\Users\alibh\Desktop\Test Folder' #the path to the folder
cd $filePath
Get-ChildItem $filePath |
ForEach-Object{
$fileName = "here is where I return the name of each file so I can edit it
later on"
}
我想比较文件夹中不同文件的名称,稍后编辑或删除文件;但在开始之前,我首先需要能够一个接一个地获取每个文件的名称。
编辑:非常感谢大家
【问题讨论】:
-
您可以使用
$_或$PSiItem访问当前迭代的对象,并通过在其名称后附加一个点来访问特定属性。$_.FullName、$_.Name、$.BaseName(或者,您可以使用原始 cmdlet 指定-PipeLineVariable) -
你可以试试这里找到的答案:PowerShell ForEach $file in $Files
标签: powershell