![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Apr 2011
Location: Wodalbunga
Posts: 460
|
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 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: i5 2500k @ 4.2Ghz, H100, MSI GTX 670 Power Ed SLI, 16GB Ripjaws X 1600mhz @ 7-8-7-24, Sabertooth P67, Crucial M4 128GB, Antec TP blue 750w, C70 Arctic White, 27" BENQ, G510, R.A.T 7 and loving it! MEDIA: i3 2120, 4GB 7-7-7-7 1333Mhz, 30GB SSD, 2TB green, bluray, ComproDTV tuner, GT640, LianLi PC-C37B |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 | |
|
Member
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,628
|
Does this txt file:
Quote:
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 2:02 PM. |
|
|
|
|
|
|
#3 | |
|
Member
Join Date: Apr 2011
Location: Wodalbunga
Posts: 460
|
Quote:
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: i5 2500k @ 4.2Ghz, H100, MSI GTX 670 Power Ed SLI, 16GB Ripjaws X 1600mhz @ 7-8-7-24, Sabertooth P67, Crucial M4 128GB, Antec TP blue 750w, C70 Arctic White, 27" BENQ, G510, R.A.T 7 and loving it! MEDIA: i3 2120, 4GB 7-7-7-7 1333Mhz, 30GB SSD, 2TB green, bluray, ComproDTV tuner, GT640, LianLi PC-C37B |
|
|
|
|
|
|
#4 |
|
Member
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,628
|
Right.
So you've got a folder structure as follows:
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:
Last edited by Tekin; 31st January 2013 at 3:29 PM. |
|
|
|
|
|
#5 | |
|
Member
Join Date: Apr 2011
Location: Wodalbunga
Posts: 460
|
Quote:
__________________
MAIN: i5 2500k @ 4.2Ghz, H100, MSI GTX 670 Power Ed SLI, 16GB Ripjaws X 1600mhz @ 7-8-7-24, Sabertooth P67, Crucial M4 128GB, Antec TP blue 750w, C70 Arctic White, 27" BENQ, G510, R.A.T 7 and loving it! MEDIA: i3 2120, 4GB 7-7-7-7 1333Mhz, 30GB SSD, 2TB green, bluray, ComproDTV tuner, GT640, LianLi PC-C37B |
|
|
|
|
|
|
#6 |
|
Member
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,628
|
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
Before Click to view full size! After Click to view full size! |
|
|
|
|
|
#7 |
|
Member
Join Date: Apr 2011
Location: Wodalbunga
Posts: 460
|
You are a legend mate! The only thing I needed to change was
Code:
cd \%Rootfolder% Code:
cd /d %~dp0 Thanks once again! ren
__________________
MAIN: i5 2500k @ 4.2Ghz, H100, MSI GTX 670 Power Ed SLI, 16GB Ripjaws X 1600mhz @ 7-8-7-24, Sabertooth P67, Crucial M4 128GB, Antec TP blue 750w, C70 Arctic White, 27" BENQ, G510, R.A.T 7 and loving it! MEDIA: i3 2120, 4GB 7-7-7-7 1333Mhz, 30GB SSD, 2TB green, bluray, ComproDTV tuner, GT640, LianLi PC-C37B |
|
|
|
|
|
#8 |
|
Member
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,628
|
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
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|