none
Rename files using a text file in Powershell RRS feed

  • Question

  • I've read the other answers to this problem but my circumstances are slightly different. I have a txt file with comma separated values, this is an example of one line.

    "1,970","07-15-47","00001878.tif",".\images\00001","38,003","TIFF","9/13/2001 2:14:42am","c:\users\ckillion\desktop\city commission minutes\1947\00001878.tif","City Commission Minutes\1947",""

    The name of file I want to change is the third comma separated string, and what I want to change it to is the second string

    Thanks in advance

    Thursday, November 21, 2013 12:56 PM

Answers

  • It looks like the file exists (Test-Path returns true), but the rename does not work. If you run the below (i've added the -force switch, too) you should see what is failing:

    Import-Csv c:\TestFile.Txt | foreach { 
    	if (test-path $_.location) { 
    		try {rename-Item $_.location "$($_.Date).tif" -force}
    		catch{	"Could not rename $($_.location) to $($_.Date).tif" }
    	} 
    }

    If you then try to run rename-item on those from a command prompt do you get the same error?

    • Proposed as answer by Dirk_74 Thursday, November 28, 2013 8:22 AM
    • Marked as answer by Bill_StewartModerator Thursday, December 5, 2013 3:33 AM
    Friday, November 22, 2013 4:05 PM

All replies

  • Do you have any code for this? I mean did you tried any method

    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Thursday, November 21, 2013 1:30 PM
    Thursday, November 21, 2013 1:30 PM
  • I tried using this

    GC TestFile.Txt | % { if (test-path "$($_.Split(",")[2]).tif") { rename-Item "$($_.Split(",")[3]).tif" "$($_.Split(",")[1]).tif" } }

    based off another answer here but then I realized that was also made to change . into _, all it did was comeback with errors

    Thursday, November 21, 2013 2:08 PM
  • I think your test file comma delimited CSV does this file contain any header ?

    Regards Chen V [MCTS SharePoint 2010]

    Thursday, November 21, 2013 2:28 PM
  • No it doesn't contain a header
    Thursday, November 21, 2013 3:33 PM

  • PS C:\> help Import-Csv

    Since your CSV file doesn't have a header, you can define one with the -Header parameter.

    Bill

    Thursday, November 21, 2013 3:46 PM
    Moderator
  • yes, import-csv will make it much easier. You might have to adjust the below to reflect your setup, but this is how it should work:

    $myHeader= echo 1 fileName newName 2 3 4 5 6 7 fullName 8 9
    Import-Csv TestFile.Txt -Header $myHeader | foreach { 
    	if (test-path $_.fullName) { 
    		rename-Item $_.FullName $_.newName
    	} 
    }

    Thursday, November 21, 2013 3:58 PM
  • Thank you, I didn't know the header was necessary. So I should just use the code I posted after defining a header?
    Thursday, November 21, 2013 4:03 PM
  • Thank you, I didn't know the header was necessary. So I should just use the code I posted after defining a header?

    No. Instead of GC (Get-Content) you should be using import-csv as shown in Dirk's suggested code.

    when data is in csv format, import-csv keeps you from having to parse raw csv data in which the field separator character (,) might appear in a quoted data field. The header, whether in the file or supplied by the code with the -Header parameter, help make your code easier to read and maintain by giving meaningful names to the fields rather than remembering that you are dealing with fields 2 and 3 (or was that 3 and 4?)


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Thursday, November 21, 2013 4:12 PM
  • I really appreciate all the help, I have no idea why but it doesn't work, just hangs and displays prompt >>

    Thursday, November 21, 2013 6:21 PM
  • Hi,

    Are you pasting directly into the console? If so, you may need to press Enter one last time.


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,420+ strong and growing)

    Thursday, November 21, 2013 6:32 PM
  • error comes up

    Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
    At line:2 char:15
    + if (test-path $_.fullName) {
    +               ~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Test
       PathCommand



    • Edited by BrazPepper Thursday, November 21, 2013 6:41 PM
    Thursday, November 21, 2013 6:40 PM
  • I got the same error mike !!! Still checking the way to go

    Regards Chen V [MCTS SharePoint 2010]

    Thursday, November 21, 2013 8:07 PM
  • When I run it against this test (I've only changed the order of the headers) it works just fine:

    $txt=@"
    "1,970","07-15-47","00001878.tif",".\images\00001","38,003","TIFF","9/13/2001 2:14:42am","c:\users\ckillion\desktop\city commission minutes\1947\00001878.tif","City Commission Minutes\1947",""
    "1,970","07-15-47","00001878.tif",".\images\00001","38,003","TIFF","9/13/2001 2:14:42am","c:\users\ckillion\desktop\city commission minutes\1947\00001878.tif","City Commission Minutes\1947",""
    "1,970","07-15-47","00001878.tif",".\images\00001","38,003","TIFF","9/13/2001 2:14:42am","c:\users\ckillion\desktop\city commission minutes\1947\00001878.tif","City Commission Minutes\1947",""
    "1,970","07-15-47","00001878.tif",".\images\00001","38,003","TIFF","9/13/2001 2:14:42am","c:\users\ckillion\desktop\city commission minutes\1947\00001878.tif","City Commission Minutes\1947",""
    "@ | Set-Content c:\TestFile.txt
    $myHeader= echo 1 fileName newName 2 3 4 5 fullName 6 7 8 9
    Import-Csv c:\TestFile.Txt -Header $myHeader | foreach { 
    	if (test-path $_.fullName) { 
    		rename-Item $_.FullName $_.newName
    	} 
    }

    Thursday, November 21, 2013 8:57 PM
  • This is exceedingly helpful, I can't thank you enough for your time, it doesn't work. I'm starting to think it's an issue unrelated to the code. I wrote a vb console app to do the same thing and it doesn't work. I triple checked every file to make sure it isn't read only. Not sure how to proceed.
    Friday, November 22, 2013 12:09 PM
  • I would suggest you open the file with excel and add a header row manually. If you then post the first few rows of the file, we should be able to help you.
    Friday, November 22, 2013 12:43 PM
  • Year,Date,File Name,first file,size,type,scane date,location,folder,ocr
    1979,05-14-47,00001887.tif,.\images\00001,20240,TIFF,9/13/2001 2:15:52am,c:\users\ckillion\desktop\city commission minutes\1947\00001887.tif,City Commission Minutes\1947,
    1978,05-20-47,00001886.tif,.\images\00001,31131,TIFF,9/13/2001 2:15:46am,c:\users\ckillion\desktop\city commission minutes\1947\00001886.tif,City Commission Minutes\1947,
    1977,05-27-47,00001885.tif,.\images\00001,24670,TIFF,9/13/2001 2:15:38am,c:\users\ckillion\desktop\city commission minutes\1947\00001885.tif,City Commission Minutes\1947,
    1976,06-03-47,00001884.tif,.\images\00001,37033,TIFF,9/13/2001 2:15:32am,c:\users\ckillion\desktop\city commission minutes\1947\00001884.tif,City Commission Minutes\1947,
    1975,06-10-47,00001883.tif,.\images\00001,43027,TIFF,9/13/2001 2:15:26am,c:\users\ckillion\desktop\city commission minutes\1947\00001883.tif,City Commission Minutes\1947,
    1974,06-19-47,00001882.tif,.\images\00001,33314,TIFF,9/13/2001 2:15:18am,c:\users\ckillion\desktop\city commission minutes\1947\00001882.tif,City Commission Minutes\1947,
    Friday, November 22, 2013 2:18 PM
  • Hopefully that does the job. I'm assuming that you want to rename from the value that is stored in the "File Name" column to the value that is stored in the "Date" column + ".tif" for the file with the FullPath stored in "location"

    Import-Csv c:\TestFile.Txt | foreach { 
    	if (test-path $_.location) { 
    		rename-Item $_.location "$($_.Date).tif"
    	} 
    }



    • Edited by Dirk_74 Friday, November 22, 2013 2:30 PM
    Friday, November 22, 2013 2:29 PM
  • rename-Item : Cannot create a file when that file already exists.

    At line:3 char:1
    + rename-Item $_.location "$($_.Date).tif"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\users\ckilli...es\00001403.tif:String) [Rename-Item], IOException
        + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

    • Edited by BrazPepper Friday, November 22, 2013 3:53 PM
    Friday, November 22, 2013 3:16 PM
  • As the error implies, that file already exists. Either delete it, or use Rename-Item -Force
    Friday, November 22, 2013 4:04 PM
  • It looks like the file exists (Test-Path returns true), but the rename does not work. If you run the below (i've added the -force switch, too) you should see what is failing:

    Import-Csv c:\TestFile.Txt | foreach { 
    	if (test-path $_.location) { 
    		try {rename-Item $_.location "$($_.Date).tif" -force}
    		catch{	"Could not rename $($_.location) to $($_.Date).tif" }
    	} 
    }

    If you then try to run rename-item on those from a command prompt do you get the same error?

    • Proposed as answer by Dirk_74 Thursday, November 28, 2013 8:22 AM
    • Marked as answer by Bill_StewartModerator Thursday, December 5, 2013 3:33 AM
    Friday, November 22, 2013 4:05 PM
  • I'm really sorry for being so much trouble. this error is created for every file it attempts to rename

    rename-Item : Cannot create a file when that file already exists.
    At line:3 char:6
    + try {rename-Item $_.location "$($_.Date).tif" -force}
    +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\users\ckilli...es\00001403.tif:String) [Rename-Item], IOException
        + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

    Friday, November 22, 2013 5:40 PM
  • Maybe you don't have permissions to override those files. Check the Security settings and make sure your account is in Full Control of the folder: c:\users\ckillion\desktop\city commission minutes\1947\

    Dirk's code works just fine for me. You could try replacing Rename-Item with Move-Item like this:

    Import-Csv c:\TestFile.Txt | foreach {
    move-Item -path $_.location `
    -destination (-join("c:\users\ckillion\desktop\city commission minutes\1947\",$_.date,".tif")) `
    -force}
    

    • Proposed as answer by Razvan Rusu Thursday, November 28, 2013 1:46 PM
    Friday, November 22, 2013 10:14 PM
  • This worked, thank you so much. It must've been a permissions issue, I tried to make sure I had full control before. I think the program that exported the tif's might have locked them down.
    Monday, November 25, 2013 12:07 PM