none
Renaming files using text file and powershell

    Question

  • Once again I am trying to find out how to rename files on my pc using the names stored in a text document
    eg. the file on the pc is called "C07_08.dat"
    the text file has the following line "3am Eternal         KLF         03:15 121 BMG         C07.08"
    I want to change the name of the file "C07_08.dat" to "3am Eternal.mpeg"
    The text file has been saved as tab delimited from excel so there is a tab between each column
    Originally Ravikanth wrote a nice simple Powershell script as follows

    GC TestFile.Txt | % { if (test-path "$($_.Split("`t")[3]).dat") { rename-Item "$($_.Split("`t")[3]).dat" "$($_.Split("`t")[0]).mpeg" } }

    this worked perfectly originally but now I cant seem to get it to work at all
    What I am doing at the moment is opening up Powershell that comes with windows 7 then navigating to the drive and directory of the files (currently G:\Video)
    This has both the textfile and the files to be renamed in it
    I then copy the script and right click in powershell which pastes the text into the window but all that happens is the cursor drops down two lines
    sample:
    PS G:\video> GC TestFile.Txt | % { if (test-path "$($_.Split("`t")[3]).dat") { rename-Item "$($_.Split("`t")[3]).dat" "$
    ($_.Split("`t")[0]).mpeg" } }
    PS G:\video>
    PS G:\video>
    Below is a sample of the text file that I am using
    If Your Not In It For Love	Shania Twain	04:22 116 MER	D03.13
    Sway	Shaft	03:27 129 UMA	H38.02
    Reach Up For The Sunrise	Duran Duran	03:22 127 SON	H95.03
    You Want To Make A Memory	Bon Jovi	04:00 074 UMA	HD1.21
    1000 Stars	Natalie Bassingthwaight	03:43 084 SME	HF7.09
    1973	James Blunt	03:05 123 WAR	HD6.09
    2 Hearts	Kylie	02:50 112 WAR	HD6.12
    20 Good Reasons	Thirsty Merc	03:46 093 WAR	HC8.19
    21 Guns	Greenday	04:37 081 WAR	HF9.19
    21st Century Breakdown	Green Day	04:24 122 WAR	HG5.18
    21st Century Life	Sam Sparro	03:44 124 UMA	HE7.05
    22	Lilly Allen	03:00 131 EMI	HF9.05
    22 Steps	Damien Leith	03:29 077 SBE	HD3.17
    25 Miles 2001	The 3 Amigos	03:14 134 UMA	N52.02
    3	Britney Spears	03:33 135 SME	HG4.07
    3 Minutes To Explain	Fedde Le Grand	02:58 123 MOS	HF1.04
    3 Words	Cheryl Cole ft Will I A	04:29 130 UMA	HG8.03
    3am Eternal	KLF	03:15 121 BMG	C07.08
    4 In The Morning	Gwen Stefani	04:22 092 UMA	HD1.10
    4 Minutes	Madonna ft J Timberlak	04:04 113 WAR	HE3.05
    4th July	Kelis	03:39 124 UMA	HH3.05
    5 6 7 8	Steps	03:24 000 BMG	C48.03
    5678	Steps	03:23 140 MUS	H16.02
    6 Of 1 Thing	Craig David	03:15 116 WAR	HE2.11
    60 MPH	New Order	03:51 125 WAR	N57.11
    7 Days	Craig David	04:30 084 SHO	N41.16
    7 Things	Miley Cyrus	03:29 107 EMI	HE7.09
    99 Luft Balloons	Nena	03:58 095 WAR	C27.10
    99 Times	Kate Voegele	03:27 112 UMA	HG6.07
    A Girl Like You	Edwyn Collins	03:47 126 MDS	R06.08
    A Little Bit	Pandora	03:35 132 UMA	H23.01
    A Little Less Conversation	Elvis VS JXL	03:02 115 BMG	H68.03
    A Matter Of Trust	Billy Joel	04:00 110 SON	C59.11
    A New Day Has Come	Celine Dion	04:20 092 SON	H65.20
    A Woman Like You	Mondo Rock	04:03 169 MUS	R06.01
    Also when I first tried this I got the following error message

    Incomplete $(subexpression) sequence in string.

    At line:1 char:50

    + GC TestFile.Txt | % { if (test-path "$($_.Split("` <<<< t")[3]).dat") { renam
    e-Item "$($_.Split("`t")[3]).dat" "$($_.Split("`t")[0]).mpeg" } }

    However this message no longer appears

    Sunday, November 21, 2010 3:30 PM

Answers

  • Okay, let's tell it to replace the . with _ in the input file name.

     

    GC TestFile.Txt | % { if ($_ -match $regex){rename-item $matches[2].replace(".","_") -newname ($matches[1] + '.mpg') -whatif}}


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Vipercat Monday, November 22, 2010 3:26 PM
    Monday, November 22, 2010 11:16 AM
    Moderator

All replies

  • Try this:

    $regex = "^\s*(.+)\s+\d+.+\s+(\S+)$"

    GC TestFile.Txt | % { if ($_ -match $regex){rename-item $matches[2] -newname ($matches[1] + '.mpg') -whatif}

    As long as the -whatif switch is there, it won't actually rename anything, it will just show you what it would have done.  If it looks right, take off the -whatif and re-run it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Sunday, November 21, 2010 3:52 PM
    Moderator
  •  

    I tried your script but nothing has happened

    here is what I did

    Open Powershell

    type e: press enter to move to the dirve with the files on

    type cd video press enter to move to the directory i need

    highlight your script then right click in powershell and this is the result

     

    PS C:\Documents and Settings\User001> e:

    PS E:\> cd video

    PS E:\video> $regex = "^\s*(.+)\s+\d+.+\s+(\S+)$"

    PS E:\video>

    PS E:\video> GC TestFile.Txt | % { if ($_ -match $regex){rename-item $matches[2] -newname ($matches[1] + '.mpg') -whatif}

    If press enter now I end up with a double prompt and nothing else happens

    eg. >>

    Monday, November 22, 2010 3:21 AM
  • I missed the last closing brace.

    Type } at the >> prompt and hit enter.

    If you re-enter the whole thing, add it to the end of the second line, like this:

    GC TestFile.Txt | % { if ($_ -match $regex){rename-item $matches[2] -newname ($matches[1] + '.mpg') -whatif}}


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, November 22, 2010 3:33 AM
    Moderator
  • Ok  I added the last } to the line now I get the following error

    Rename-Item : Cannot rename because item at 'H49.01' does not exist.
    At line:1 char:56
    + GC TestFile.Txt | % { if ($_ -match $regex){rename-item  <<<< $matches[2] -newname ($matches[1] + '.mpg') -whatif}}
    Rename-Item : Cannot rename because item at 'H21.14' does not exist.
    At line:1 char:56
    + GC TestFile.Txt | % { if ($_ -match $regex){rename-item  <<<< $matches[2] -newname ($matches[1] + '.mpg') -whatif}}
    Rename-Item : Cannot rename because item at 'H24.01' does not exist.
    At line:1 char:56
    + GC TestFile.Txt | % { if ($_ -match $regex){rename-item  <<<< $matches[2] -newname ($matches[1] + '.mpg') -whatif}}

     

    is this because the text file is D03.13 but the actual file is D03_13

     

    Monday, November 22, 2010 4:33 AM
  • Okay, let's tell it to replace the . with _ in the input file name.

     

    GC TestFile.Txt | % { if ($_ -match $regex){rename-item $matches[2].replace(".","_") -newname ($matches[1] + '.mpg') -whatif}}


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Vipercat Monday, November 22, 2010 3:26 PM
    Monday, November 22, 2010 11:16 AM
    Moderator
  • I still got a whole mass of scrolling errors telling me that it could not rename file etc but at the same time it worked

    all 1900+ files have been renamed correctly so I guess thats good

    Thanks for your perseverance and for a solution now to back that all up onto a raid so i dont have this problem again

     

    Monday, November 22, 2010 3:30 PM