46505

Rename from CSV Script renames files but trys to repeat process

Question:

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.

<pre class="lang-none prettyprint-override">+ 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

Answer1:

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) } }

Answer2:

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:

$sourceDir = read-host "Please enter source Dir:" $csvL = $sourceDir + "\files.csv" $csv = import-csv $csvL $files = get-childitem $sourceDir $csv | % { $oldpath = join-path $sourcedir $_.old $newpath = join-path $sourcedir $_.new rename-item $oldpath $newpath }

Recommend

  • Combine content of several files in folder
  • Powershell script to find all svn working copies
  • self-reflexive n:m relation using composite keys in nhibernate
  • Turn list output into an array of objects or something similar using Powershell
  • How to remove comma or any characters from Python dataframe column name
  • SSH in Bash Script Messing Up File Read
  • (Tcl/Expect) clear screen after exit
  • Scanner nextInt() and hasNextInt() problems
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • abstracting over a collection
  • jQuery: How to AJAXify WordPress Search?
  • Find group of records that match multiple values
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • TFS 2015 - Waiting for an agent to be requested
  • Trying to get the char code of ENTER key
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • List images(01.png) and descriptions(01.txt) from directory
  • Remove changes from one element when event occurs on another element?
  • RxJava debounce by arbitrary value
  • Android Google Maps API v2 start navigation
  • Copy to all folders batch file?
  • how to avoid repetitive constructor in children
  • Textfile Structure (tables)
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • How to test if a URL from an Eclipse bundle is a directory?
  • D3 nodes and links from JSON with nested arrays of children
  • Spark fat jar to run multiple versions on YARN
  • Counter field in MS Access, how to generate?
  • ILMerge & Keep Assembly Name
  • Javascript + PHP Encryption with pidCrypt
  • ActionScript 2 vs ActionScript 3 performance
  • Websockets service method fails during R startup
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Python: how to group similar lists together in a list of lists?
  • json Serialization in asp
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Can't mass-assign protected attributes when import data from csv file
  • Busy indicator not showing up in wpf window [duplicate]