Page 1 of 2

BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 07:02
by tinfanide
log.log
a
b


Current directory
1.txt
2.txt


I want to rename 1.txt a.txt and 2.txt b.txt. I've tried using two loops below
but just produced results like

1.txt a.txt
1.txt b.txt
2.txt a.txt
2.txt b.txt

How to combine the loops together?

Code: Select all

@ECHO OFF

FOR /F "tokens=*" %%A IN ('DIR *.txt /B /S') DO SET "old=%%A" & CALL :old
GOTO :end

:old
FOR /F "tokens=1" %%B IN (log.log) DO SET "new=%%B" & CALL :new
PAUSE
GOTO :EOF

:new
ECHO %old%, %new%
PAUSE
GOTO :EOF

:end
EXIT

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 07:48
by !k

Code: Select all

@echo off &setlocal enableextensions

set /a cnt=0
for /f "delims=" %%a in ('dir /b /on *.txt') do call :ren "%%a"
goto :eof

:ren
for /f "tokens=1" %%b in ('more +%cnt% log.log') do (
   echo ren %1 "%%b.txt"
   set /a cnt+=1 &goto :eof
)

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 07:48
by abc0502
Try this it worked for me:

Code: Select all

@ECHO OFF

for /f %%a in ('dir *.txt /b') do (
for /f %%b in (log.log) do ren %%a %%b.txt
)
pause

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 07:55
by tinfanide
abc0502 wrote:Try this it worked for me:

Code: Select all

@ECHO OFF

for /f %%a in ('dir *.txt /b') do (
for /f %%b in (log.log) do ren %%a %%b.txt
)
pause


This produces a CMD error:
The system cannot find the file specified.
A duplicate file name exists, or the file
cannot be found.
Press any key to continue . . .

Yet it works.

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 08:04
by tinfanide
!k wrote:

Code: Select all

@echo off &setlocal enableextensions

set /a cnt=0
for /f "delims=" %%a in ('dir /b /on *.txt') do call :ren "%%a"
goto :eof

:ren
for /f "tokens=1" %%b in ('more +%cnt% log.log') do (
   echo ren %1 "%%b.txt"
   set /a cnt+=1 &goto :eof
)


It does not work, with the cmd window popping out for half a second and disappearing.

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 08:05
by abc0502
i tried on my desktop and there was no other txt files maybe there is more txt file in your directory so the duplicat name will be found
to avoid that put more letters in the log.log file

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 08:20
by tinfanide
Don't know why.
Under the directory, there are only four files:

1.txt
2.txt
log.log
t.bat (where the script is stored)

And I put

a
b
c
d

in log.log

More errors are produced but the renaming still works.

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 08:25
by abc0502
what os are u use 7 or xp
i use xp now but i will test on 7 and tell u
i tried again on xp i have the same errors now
i don't know what happened but i fix some thing and i get one error now i fix it and post it here just few minutes

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 08:49
by abc0502
i know now why is the error
when i used this:

Code: Select all

@ECHO OFF
setlocal
for /f %%a in ('dir *.txt /a:-d /o:n /b') do (
echo %%a
for /f %%b in (log.log) do echo %%b
)
pause

it give this:

Code: Select all

1.txt
a
b
2.txt
a
b

the batch rename 1.txt to 1.txt then try to rename the 1.txt to b.txt but 1.txt is not there it is already renamed so it give the error

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 08:55
by !k
tinfanide wrote:It does not work, with the cmd window popping out for half a second and disappearing.
remove "echo" in

Code: Select all

   echo ren %1 "%%b.txt"

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 09:05
by abc0502
!k it work with me here
with no errors at all :)

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 09:09
by foxidrive
This sort of technique was shown recently.

It will work if the number of lines in log.log is equal to the number of *.txt files.
The renaming is in alphabetical order as the DIR command returns the filenames.

remove the echo command to make it work.

Code: Select all

@echo off
setlocal enabledelayedexpansion

< "log.log" (for /f "delims=" %%a in ('dir *.txt /b /o:n') do (
set var=
set /p "var="
if defined var echo ren  "%%a" "!var!%%~xa"
)
)
pause

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 10:01
by tinfanide
!k wrote:

Code: Select all

@echo off &setlocal enableextensions

set /a cnt=0
for /f "delims=" %%a in ('dir /b /on *.txt') do call :ren "%%a"
goto :eof

:ren
for /f "tokens=1" %%b in ('more +%cnt% log.log') do (
   echo ren %1 "%%b.txt"
   set /a cnt+=1 &goto :eof
)


I hope I won't be too fussy if I ask further about the MORE command.
I didn't know the use of MORE and tried testing it:

Code: Select all

FOR /F "tokens=1" %%A IN ('MORE +1 log.log') DO ECHO %%A

It works like skip in FOR. The bit I don't understand why in your codes it only shows the text in that line?
b
c
d

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 10:02
by tinfanide
abc0502 wrote:i know now why is the error
when i used this:

Code: Select all

@ECHO OFF
setlocal
for /f %%a in ('dir *.txt /a:-d /o:n /b') do (
echo %%a
for /f %%b in (log.log) do echo %%b
)
pause

it give this:

Code: Select all

1.txt
a
b
2.txt
a
b

the batch rename 1.txt to 1.txt then try to rename the 1.txt to b.txt but 1.txt is not there it is already renamed so it give the error


Yes, exactly.
Me using Win 7, but I presume there should be no difference.

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 10:39
by tinfanide
foxidrive wrote:This sort of technique was shown recently.

It will work if the number of lines in log.log is equal to the number of *.txt files.
The renaming is in alphabetical order as the DIR command returns the filenames.

remove the echo command to make it work.

Code: Select all

@echo off
setlocal enabledelayedexpansion

< "log.log" (for /f "delims=" %%a in ('dir *.txt /b /o:n') do (
set var=
set /p "var="
if defined var echo ren  "%%a" "!var!%%~xa"
)
)
pause


First, what's the use of the redirect output operator?

Code: Select all

< "log.log"

And a lot more I cannot make sense of...

The FOR loop is inside a ()

Code: Select all

(FOR ...
)


Why set var = nothing?

Code: Select all

SET var=
SET /P "var="


SET var=
to unset the variable %var%?
Is it to reset the variable with the delayed expansion?

SET /P "var="
It waits for users to enter a value for %var%.
If the user does not enter anything, the variable is still empty. Then what's the use of it in this case?

If SET var=nothing, why var is defined?


Sorry if I've asked too much.
I couldn't figure out most of them online on my own.