Folder renaming with info from ini

March 25, 2011 at 17:56:08
Specs: Windows 7
First of all hi!
I have a directory with the following directories inside it:
BLES00319
BLES00604
BLES00917
BLES00659
BLUS30442
BLES00759
BLUS30517
BLES00135
BLES00949
BLES00673
BLES00887
BLES01031
BCAS20107
BLES00914
BLUS30527
BLES00682
BLES00517
BLES00176
BLUS30192
BCUS98229
BLES00697
BLES00820
BLES00875
BLES00683
BLES00464
BLES00962
BLES00237
BLES00773
BLUS30631
BLES01134
BLES00073

And i have enumerated all the names of the games inside a ini file (sample.ini) like this:

[Section1]
BLES00319 = Brothers in Arms - Hells Highway
BLES00604 = Kane and Lynch 2 - Dog Days
BLES00917 = F1 2010
BLES00659 = Army of TWO - The 40th Day
BLUS30442 = Midnight Club - Los Angeles
BLES00759 = Blur
BLUS30517 = BATTLEFIELD - Bad Company 2
BLES00135 = Stuntman - Ignition
BLES00949 = Need for Speed - Hot Pursuit
BLES00673 = Colin McRae - DiRT 2
BLES00887 = Grand Theft Auto - Episodes from Liberty City
BLES01031 = Call of Duty - Black Ops
BCAS20107 = HEAVY RAIN
BLES00914 = nail-d 
BLUS30527 = Test Drive Unlimited 2
BLES00682 = Need for Speed SHIFT
BLES00517 = Just Cause 2
BLES00176 = Need for Speed ProStreet
BLUS30192 = Call of Duty - World at War
BCUS98229 = God Of War Collection
BLES00697 = Borderlands
BLES00820 = BLAZBLUE
BLES00875 = 3D DOT GAME HEROES
BLES00683 = Modern Warfare 2
BLES00464 = FEAR 2 - Project Origin
BLES00962 = Homefront
BLES00237 = Tom Clancys Rainbow Six Vegas 2
BLES00773 = BATTLEFIELD - Bad Company 2
BLUS30631 = Crysis 2
BLES01134 = Bulletstorm
BLES00073 = Burnout Paradise

And i've made a batch to read the names and rename the folders to for example like this:

BCAS20107_HEAVY RAIN
BCUS98229_God Of War Collection

Here is the batch:

@echo off

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BCUS98229" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BCUS98229" "BCUS98229_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BCAS20107" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BCAS20107" "BCAS20107_%ID%"

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00604" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00604" "BLES00604_%ID%"

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00073" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00073" "BLES00073_%ID%"

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLUS30442" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLUS30442" "BLUS30442_%ID%"

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00135" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00135" "BLES00135_%ID%"

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00176" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00176" "BLES00176_%ID%"

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00237" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00237" "BLES00237_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00319" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00319" "BLES00319_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00464" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00464" "BLES00464_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00517" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00517" "BLES00517_%ID%"

setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00659" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00659" "BLES00659_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00673" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00673" "BLES00673_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00682" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00682" "BLES00682_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00683" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00683" "BLES00683_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00697" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00697" "BLES00697_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00759" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00759" "BLES00759_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00773" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00773" "BLES00773_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00820" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00820" "BLES00820_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00875" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00875" "BLES00875_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00887" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00887" "BLES00887_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00914" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00914" "BLES00914_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00917" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00917" "BLES00917_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00949" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00949" "BLES00949_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES00962" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES00962" "BLES00962_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES01031" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES01031" "BLES01031_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLES01134" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLES01134" "BLES01134_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLUS30192" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLUS30192" "BLUS30192_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLUS30442" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLUS30442" "BLUS30442_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLUS30517" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLUS30517" "BLUS30517_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLUS30527" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLUS30527" "BLUS30527_%ID%"


setlocal enableextensions
for /f "tokens=2 delims==" %%? in (
'find /i "BLUS30631" ^< sample.ini') do set ID=%%?
if "%ID:~0,1%" equ " " set ID=%ID:~1%
ren "BLUS30631" "BLUS30631_%ID%"

I wanted to know if it is possible to make a single batch read from ini without making a single line reader for every line inside the ini?

Thanks in advance!!!


See More: Folder renaming with info from ini

Report •


#1
March 25, 2011 at 18:51:22
Maybe something like this:

I have prefixed ren with echo so you can check the accuracy with a dry run.

Also untested....

pushd t:\he\dir
for /d %%a in (*) do (
    for /f "tokens=1* delims== " %%b in (' find "%~na" ^< "sample.ini" ') do (
        if "%%b"=="%%~na" (
            echo ren "%%~na" "%%~na_%%c"
        )
    )
)
popd
pause


Report •

#2
March 25, 2011 at 19:04:51
Here's a Ruby for Windows script

hash=Hash.new
File.open("file").each do|line|
  d,name = line.chomp.split(/\s+=\s+/,2)
  hash[d] = name
end
Dir["*"].each do|x|
  if File.directory?(x) && hash.has_key?(x)
    File.rename(x , "#{x}_#{hash[x]}")
  end
end


Report •

#3
March 25, 2011 at 19:05:44
Putted the code inside a file with the following code:
@echo off
pushd C:\Users\zylor\Desktop\testar renomear
for /d %%a in (*) do (
    for /f "tokens=1* delims== " %%b in (' find "%~na" ^< "sample.ini" ') do (
        if "%%b"=="%%~na" (
            echo ren "%%~na" "%%~na_%%c"
        )
    )
)
popd
pause

I won't see nothing... the window pops up and closes..

Kurumi: Never worked with Ruby lol


Report •

Related Solutions

#4
March 25, 2011 at 19:40:01
Ok, I made one tiny mistake(missed a single %):

@echo off
pushd "C:\Users\zylor\Desktop\testar renomear"
for /d %%a in (*) do (
    for /f "tokens=1* delims== " %%b in (' find "%%~na" ^< "sample.ini" ') do (
        if "%%b"=="%%~na" (
            echo ren "%%~na" "%%~na_%%c"
        )
    )
)
popd
pause


Report •

#5
March 25, 2011 at 19:46:52
Thanks this worked! Thanks mate

Report •

#6
March 25, 2011 at 19:51:20
No problem.

Now that I think of it it might also be a good idea to make it case-insensitive:

@echo off
pushd "C:\Users\zylor\Desktop\testar renomear"
for /d %%a in (*) do (
    for /f "tokens=1* delims== " %%b in (' find "%%~na" ^< "sample.ini" ') do (
        if /i "%%b"=="%%~na" (
            echo ren "%%~na" "%%~na_%%c"
        )
    )
)
popd
pause


Report •

#7
March 25, 2011 at 20:01:11
As usual, although it works, a batch like that will certainly create a performance hit. for every folder found in the directory, a call to external find command is executed, which in turn iterates over the sample ini file for each line ....
also what if the names contain more than one "=" as well?

GNU win32 packages | Gawk


Report •

#8
March 25, 2011 at 20:08:14
i know it has limitations and sometimes games names includes special chars like these:
©®:;
and so on so it would be great to build something in visual basic but i never programmed in visual basic so i can't do nothing :(

Report •

#9
March 25, 2011 at 20:19:38
True it's a performance hit, but that's the price of quick and dirty scripting. The post asked for batch so I gave batch.

also what if the names contain more than one "=" as well?

As I understand it it's the file names in this case don't contain "=". It wouldn't matter anyway unless the name started with "=" ("tokens=1*"), but if you really want to deal with that:

@echo off
pushd "C:\Users\zylor\Desktop\testar renomear"
for /d %%a in (*) do (
    for /f "tokens=1,2* delims= " %%b in (' find "%%~na" ^< "sample.ini" ') do (
        if /i "%%b"=="%%~na" (
            echo ren "%%~na" "%%~na_%%d"
        )
    )
)
popd
pause


Report •

#10
March 25, 2011 at 20:27:54
I need to open a new topic to ask about visual basic? Judago? Thanks for your help mate! :D

Report •

#11
March 25, 2011 at 20:37:18
It might be a good idea to start a new thread if you want VB, that way people that are good with VB and ignore the batch topics are more likely to jump in.

Personally I'm not great with VB so I probably won't answer ;).


Report •


Ask Question