从CSV重命名脚本重命名文件,但trys重复过程 [英] Rename from CSV Script renames files but trys to repeat process
本文介绍了从CSV重命名脚本重命名文件,但trys重复过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脚本,它重命名从CSV取得的文件,但它会在它执行成功的过程后重新命名文件时抛出一个错误
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文件如下:
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有关,但我不知道在哪里错了,我以前有类似的问题。
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.
这是错误的示例。
+ 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
提前感谢,
Craig
thanks in advance, Craig
推荐答案
我认为 ForEach($文件中的$文件)
是多余的,每个文件与 $ 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 | Foreach-Object { //I prefer to print the full commands in scripts
$oldfile = $sourcedir + "\" + $_.old
if (Test-Path $oldfile)
{
Rename-Item $oldfile ($sourcedir + "\" + $_.new)
}
}
这篇关于从CSV重命名脚本重命名文件,但trys重复过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文