Batch File Help!

Discussion in 'Programming & Software Development' started by renren, Jan 31, 2013.

  1. renren

    renren Member

    Joined:
    Apr 8, 2011
    Messages:
    819
    Location:
    A magical place
    Hi guys,

    I'm trying to make a batch file that renames a folder. I have achieved this, but I'm looking for an easier way to do it.

    This is what I've got so far: -

    Code:
    cd /d %~dp0
    pause
    
    ren ABC0001 Persons_name_ABC0001
    ren ABC0002 Persons_name_ABC0002
    
    This method is slow and messy for the amount I want to do. It also adds underscores to the names, which I also don't want.

    Here is what I actually want: -

    There are 600ish odd folders already in existence of names like ABC0001, ABC0002, ABC0003 and so on, and I want to add a persons name in front of it, so that it looks like this: Franken Burger ABC0001

    The name at the front of that is in a .txt document in this directory, with a new name on each line like this: -
    Franken Burger
    Doogle McMuffin
    Spanky Sparkplug
    Woggy Del Pasta

    The 3 digit / 4 number folder name comes from another .txt document in this directory, with a new name on each line as well:
    ABC0001
    ABC0002
    ABC0003
    ABC0004

    The end result would look at the 3 digit / 4 number folder name, and if it exists, add the name to it. (Like, if dir ABC0001 exists ren ABC0001 Franken Burger ABC0001 else go to next).

    It might be easier to have both the names and the folder names in the single batch file (reads one line, reads the other and concatenates?) but I have no idea how to achieve that.

    Thankyou
     
  2. Tekin

    Tekin Member

    Joined:
    Nov 16, 2002
    Messages:
    4,039
    Location:
    Elsewhere.
    Does this txt file:

    Have both the Persons name and the name of the folder in it?

    Basically you want to use this function:

    for /f "tokens=* delims= " %%a in (Textfilewithnames.txt) do ( "x"

    That will look up each line - parse the variables and perform an action

    You could do something like.

    for /f "tokens=2 delims= " %%a in (Textfileswithnames.txt) do (Ren Folder %%a %%b Folder)

    Can you give me exactly what is in the Textfileswithnames file on a single line?

    *edit* - Read your post a few more times, still not 100% on what you're trying to do. Put a bit more detail in there and I'll be happy to help.
     
    Last edited: Jan 31, 2013
  3. OP
    OP
    renren

    renren Member

    Joined:
    Apr 8, 2011
    Messages:
    819
    Location:
    A magical place
    Thanks for that mate.

    Yeah looking back at what I've written above I wasn't very clear... :( Also I think my goals have changed throughout the day (only a little bit though!)

    Ok, I currently have a folder with 600ish folders in.
    Each one currently has unique a name like ABC0001
    I need them renamed so that the names of the root folders look like this: -
    Franken Burger - ABC0001
    The name comes from one .txt file and the ABC0001 comes from another.

    The .txt files are set up like this: -
    Name Lastname
    Name Lastname
    Name Lastname
    and the second is set up like this: -
    ABC0001
    ABC0002
    ABC0003

    (The ABC0001 is the current name of the folder, so if there is a way of using that instead of the second .txt file, that would be really cool <and probably easier?!>).

    Thanks so much for this

    ren
     
  4. Tekin

    Tekin Member

    Joined:
    Nov 16, 2002
    Messages:
    4,039
    Location:
    Elsewhere.
    Right.

    So you've got a folder structure as follows:

    • c:
      [*]Master Folder (folder)​
      [*]Text file with Peoples Names.txt (text File)
      [*]Text file with Folder Names.txt (text File)
      [*]ABC0001 (folder)
      [*]ABC0002 (folder)
      [*]ABC0003 (folder)​
    And you just want to rename the folders based upon the order of the names in the text file?

    So
    rename ABC0001 to "First name in the Text file + ABC0001"
    rename ABC0002 to "Second name in the Text file + ABC0002"

    So if the text file is:
    Franken Burger
    Joe Bloggs
    Who Cares

    you'll end up with:

    • c:
      [*]Master Folder (folder)​
      [*]Text file with Peoples Names.txt (text File)
      [*]Text file with Folder Names.txt (text File)
      [*]Franken Burger - ABC0001 (folder)
      [*]Joe Blogs - ABC0002 (folder)
      [*]Who Cares ABC0003 (folder)​
    Does that look right?
     
    Last edited: Jan 31, 2013
  5. OP
    OP
    renren

    renren Member

    Joined:
    Apr 8, 2011
    Messages:
    819
    Location:
    A magical place
    Yep, that looks spot on! (why couldn't I have been as clear as that?! :p)
     
  6. Tekin

    Tekin Member

    Joined:
    Nov 16, 2002
    Messages:
    4,039
    Location:
    Elsewhere.
    Hey

    Had to have a bit of a play with it - hit a stupid bug with windows 7 and findstr. You'll need to update your variables up the top for the text file name and the rootfolder. Create this as a batch file and put it in the same location as the text file with the names.

    (Note: it may error out on the last loop because I didn't put an exception in, but it should finish correctly first).

    Obviously please test this and understand it before running it on a production system!

    Code:
    :: Script to Rename folders - prefixing from a text file
    
    setlocal ENABLEDELAYEDEXPANSION
    
    Set Rootfolder=Test
    Set Names=Names.txt
    
    :: Goto Root Folder
    cd \%Rootfolder%
    
    
    :: Start Line Counter
    Set LineCount=0
    
    	:: For Every folder in the directory
    	For /d /r %%g in (*) DO (
    		:: Increment the line counter by 1 (see the use of "!" instead of "%" due to delayed expansion)
    		Set /a LineCount=!Linecount!+1
    		:: Call the Rename Folder sub - passing through the variables of folder name and line counter
    		Call:RenameFolder %%g !LineCount!)
    
    :RenameFolder
    	:: For all of the tokens in the findstr on the names file
    	for /f "Tokens=1* delims=:" %%a in ('findstr /n "^"  "%Names%"') DO (
    		:: If the line counter matches the line number
    		If %%a==%~2 (
    			:: Rename the Folder
    			Move "%~1" "%~1 %%b")
    			)
    ::Return to the Primary		
    Goto:EOF
    
    Set Rootfolder=
    Set Names=
    Set linecount=
    Set Drive=
    
    Endlocal
    I've documented the code in detail, and the output looks like this:

    Before

    Click to view full size!


    After

    Click to view full size!
     
  7. OP
    OP
    renren

    renren Member

    Joined:
    Apr 8, 2011
    Messages:
    819
    Location:
    A magical place
    You are a legend mate! The only thing I needed to change was
    Code:
    cd \%Rootfolder%
    to
    Code:
    cd /d %~dp0
    and magic happened!

    Thanks once again!

    ren
     
  8. Tekin

    Tekin Member

    Joined:
    Nov 16, 2002
    Messages:
    4,039
    Location:
    Elsewhere.
    Hey!

    Good to hear it worked - I actually had another look at what you wrote earlier on and realised I'd put the people names / folder names in the wrong order.

    For future Reference - this changes the order!

    Code:
    :: Script to Rename folders - prefixing from a text file
    setlocal ENABLEDELAYEDEXPANSION
    
    Set Rootfolder=c:\Test
    Set Names=Names.txt
    
    :: Goto Root Folder
    cd /d %~dp0
    
    
    :: Start Line Counter
    Set LineCount=0
    
    	:: For Every folder in the directory
    	for /f "tokens=1-3 delims=\" %%g in ('dir /b /s /a:d *') do (
    		:: Increment the line counter by 1 (see the use of "!" instead of "%" due to delayed expansion)
    		Set /a LineCount=!Linecount!+1
    		:: Call the Rename Folder sub - passing through the variables of folder name and line counter
    		Call:RenameFolder %%g %%h %%i !LineCount!)
    
    :RenameFolder
    	:: For all of the tokens in the findstr on the names file
    	IF "%~1"=="" Goto Cleanup
    	for /f "Tokens=1* delims=:" %%a in ('findstr /n "^"  "%Names%"') DO (
    		:: If the line counter matches the line number
    		If %%a==%~4 (
    			:: Rename the Folder
    			Move "%~1\%~2\%~3" "%~1\%~2\%%b %~3")
    			)
    ::Return to the Primary		
    Goto:EOF
    
    :Cleanup
    Set Rootfolder=
    Set Names=
    Set linecount=
    Set Drive=
    
    Endlocal
     

Share This Page