【发布时间】:2013-11-16 21:59:17
【问题描述】:
任务:
将各种 .csv 文件转储到其中的目录。这些可能是 file1.csv、file2.csv 等。
问题:
遍历文件并将其中一个重命名为标准化的“newfilename.csv”。然后,该文件将由外部程序处理,并被重命名并移出目录。
我有一个 powershell 脚本可以做到这一点:
ent## Set the file location where the log files are
$file = "C:\logs"
$new = "newfilename.csv"
$chkFile = "C:\logs\newfilename.csv"
$fileExists = Test-Path $chkFile
## Loop through all the .csv files in the log folder
foreach ($file in gci $file -include *.csv -recurse){
if($fileExists -eq $True){
Write-Host "that file exists!"
Start-Sleep -s 60
} else{
Write-Host "file doesn't exist, renaming..."
rename-item -path $file -newname ("$new")}
Start-Sleep -s 10
}
问题是,给定目录中的 5 个文件,它会遍历,重命名第一个文件,然后返回文件列表,它在 $fileExists 的“-eq $True”上返回“false”。文件列表在重新开始时是否只是不刷新(即:在“foreach”的开头是否创建了某种数组)?我从来没有真正写过 PowerShell 脚本。我应该使用其他程序来浏览文件吗?我应该执行的列表是否有“刷新”?
【问题讨论】:
标签: powershell csv