Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 31st January 2013, 9:02 AM   #1
renren Thread Starter
Member
 
renren's Avatar
 
Join Date: Apr 2011
Location: A magical place
Posts: 677
Default Batch File Help!

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
__________________
MAIN: 4790k @ 4.4Ghz, H100i, No GPU , 16GB Ripjaws X 1600mhz @ 7-8-7-24, Maximus VII Ranger, Crucial M4 128GB, 2TB Black, 1TB Green, Corsair AX-860i, C70 Arctic White
HTPC: i3 2120, Noctua NH-L12, AFOX GT640 Low Profile, 2x2GB G.skill 1333Mhz @ 7-7-7-24, Asus P8B75-M, Kingston 30GB, 2TB Green, Seasonic S12II 430W, Lian Li PC-C37B
renren is offline   Reply With Quote

Join OCAU to remove this ad!
Old 31st January 2013, 12:57 PM   #2
Tekin
Member
 
Tekin's Avatar
 
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,997
Default

Does this txt file:

Quote:
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
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 by Tekin; 31st January 2013 at 1:02 PM.
Tekin is online now   Reply With Quote
Old 31st January 2013, 2:04 PM   #3
renren Thread Starter
Member
 
renren's Avatar
 
Join Date: Apr 2011
Location: A magical place
Posts: 677
Default

Quote:
Originally Posted by Tekin View Post
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.
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
__________________
MAIN: 4790k @ 4.4Ghz, H100i, No GPU , 16GB Ripjaws X 1600mhz @ 7-8-7-24, Maximus VII Ranger, Crucial M4 128GB, 2TB Black, 1TB Green, Corsair AX-860i, C70 Arctic White
HTPC: i3 2120, Noctua NH-L12, AFOX GT640 Low Profile, 2x2GB G.skill 1333Mhz @ 7-7-7-24, Asus P8B75-M, Kingston 30GB, 2TB Green, Seasonic S12II 430W, Lian Li PC-C37B
renren is offline   Reply With Quote
Old 31st January 2013, 2:26 PM   #4
Tekin
Member
 
Tekin's Avatar
 
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,997
Default

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 by Tekin; 31st January 2013 at 2:29 PM.
Tekin is online now   Reply With Quote
Old 31st January 2013, 2:28 PM   #5
renren Thread Starter
Member
 
renren's Avatar
 
Join Date: Apr 2011
Location: A magical place
Posts: 677
Default

Quote:
Originally Posted by Tekin View Post
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?
Yep, that looks spot on! (why couldn't I have been as clear as that?! :P)
__________________
MAIN: 4790k @ 4.4Ghz, H100i, No GPU , 16GB Ripjaws X 1600mhz @ 7-8-7-24, Maximus VII Ranger, Crucial M4 128GB, 2TB Black, 1TB Green, Corsair AX-860i, C70 Arctic White
HTPC: i3 2120, Noctua NH-L12, AFOX GT640 Low Profile, 2x2GB G.skill 1333Mhz @ 7-7-7-24, Asus P8B75-M, Kingston 30GB, 2TB Green, Seasonic S12II 430W, Lian Li PC-C37B
renren is offline   Reply With Quote
Old 31st January 2013, 6:54 PM   #6
Tekin
Member
 
Tekin's Avatar
 
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,997
Default

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!
Tekin is online now   Reply With Quote
Old 1st February 2013, 8:39 AM   #7
renren Thread Starter
Member
 
renren's Avatar
 
Join Date: Apr 2011
Location: A magical place
Posts: 677
Default

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
__________________
MAIN: 4790k @ 4.4Ghz, H100i, No GPU , 16GB Ripjaws X 1600mhz @ 7-8-7-24, Maximus VII Ranger, Crucial M4 128GB, 2TB Black, 1TB Green, Corsair AX-860i, C70 Arctic White
HTPC: i3 2120, Noctua NH-L12, AFOX GT640 Low Profile, 2x2GB G.skill 1333Mhz @ 7-7-7-24, Asus P8B75-M, Kingston 30GB, 2TB Green, Seasonic S12II 430W, Lian Li PC-C37B
renren is offline   Reply With Quote
Old 1st February 2013, 8:54 AM   #8
Tekin
Member
 
Tekin's Avatar
 
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,997
Default

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
Tekin is online now   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 6:59 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!