从 CSV 脚本重命名会重命名文件,但会尝试重复该过程
我有一个脚本,可以重命名从 CSV 中获取的文件,但是当它在已经执行成功的过程后尝试重新命名文件时,它会抛出错误
CSV 文件如下:
old new
AC100 DC100
AC101 DC102
代码尝试:
$sourceDir = read-host "Please enter source Dir:"
$csvL = $sourceDir + "\files.csv"
$csv = import-csv $csvL
$files = get-childitem $sourceDir
$csv | % {
ForEach( $file in $files){
if($file = $_.old){
$old = $sourceDir + "\" + $_.old
Rename-Item $old $_.new
}
}
}
我相信这与循环有关和 csv,但我不确定哪里出了问题,我以前也遇到过类似的问题。
这是错误的示例。
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
提前致谢, 克雷格
I have a script which renames files taken from a CSV but it throws an error when it trys to re-rename files after it has already carried out the successful proceedure
CSV file is like:
old new
AC100 DC100
AC101 DC102
Code tried:
$sourceDir = read-host "Please enter source Dir:"
$csvL = $sourceDir + "\files.csv"
$csv = import-csv $csvL
$files = get-childitem $sourceDir
$csv | % {
ForEach( $file in $files){
if($file = $_.old){
$old = $sourceDir + "\" + $_.old
Rename-Item $old $_.new
}
}
}
I beleive it is something to do with looping and the csv but im not sure where im going wrong, i have had similar issue before.
Here is a sample of the error.
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+ Rename-Item <<<< $old $_.new
thanks in advance,
Craig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
ForEach( $file in $files)
是多余的,因为您已经使用$csv | 遍历了每个“文件” %{
命令。我会尝试这样的事情:I think the
ForEach( $file in $files)
is redundant, since you already traverse each "file" with the$csv | % {
command. I'd try something like:您有一个多余的嵌套循环。 $csv | <代码> %{ ... } 将已经循环遍历 csv 中的文件,然后 foreach 循环遍历文件夹中的每个文件。
试试这个:
You have a redundant nested loop.
$csv | %{ ... }
will already loop through file in the csv, and then the foreach loops through each file in the folder.Try this: