【发布时间】:2014-06-11 15:31:57
【问题描述】:
我有一个我一直在处理的脚本,它读取指定的目录,定位多个 .CSV 文件,并为每个 .CSV 文件执行一些逻辑并最终将它们重命名为 .csv.archived 。我试图尽可能干净地处理这个问题,但我弄得一团糟。
手头的问题是,我似乎无法弄清楚如何将单个文件名传递给字符串以重命名现有文件。该过程循环正常,文件最终被重命名,但出现以下错误:
#set the location where the .CSV files will be pulled from
$Filecsv = get-childitem "\\SERVER\Audit Test\" -recurse | where {$_.extension -eq ".csv"} | % {
$_.Name
}
在上面的代码中,我的想法是 $_.Name 是我(相信我)提取每个文件的文件名的地方。在下一个块的末尾,文件用文件名重命名。
#for each file found in the directory
ForEach ($item in $Filecsv) {
#count the times we've looped through
"Iterations : " + $iterations
# get the date and time from the system
$datetime = get-date -f MMddyy-hhmmtt
# rename the file
rename-item -path ("\\SERVER\Audit Test\" + $_.Name ) -newname ($filename + $datetime + ".csv.archived")
$iterations ++
}
我认为这个过程在这里很有趣:
rename-item -path ("\\SERVER\Audit Test\" + $_.Name )
出于测试目的,我已经删除了不相关的代码,如果有人能告诉我我做错了什么并且我没有疯,我会很高兴。
我不确定我是否正确理解 ForEach 循环的工作方式,TechNet 提供帮助:http://social.technet.microsoft.com/Forums/en-US/e8da8249-ea91-4772-ae85-582a4b37425b/powershell-foreachobject-vs-foreach?forum=smallbusinessserver
但没有回答我的特定问题。
有人想了解一下吗?
谢谢!这是完整的脚本:
$iterations = 1
#set the location where the .CSV files will be pulled from
$Filecsv = get-childitem "\\SERVER\Audit Test\" -recurse | where {$_.extension -eq ".csv"} | % {
$_.Name
}
#check to see if files exist, if not exit cleanly
#for each file found in the directory
ForEach ($item in $Filecsv) {
#count the times we've looped through
"Iterations : " + $iterations
# get the date and time from the system
$datetime = get-date -f MMddyy-hhmmtt
# rename the file
rename-item -path ("\\SERVER\Audit Test\" + $_.Name ) -newname ($_.Name + $datetime + ".csv.archived")
$iterations ++
}
【问题讨论】:
-
$_ 不应该是 $item 吗?
标签: string powershell foreach