BATCH rename files using a criterion txt file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

BATCH rename files using a criterion txt file?

#1 Post by tinfanide » 06 Apr 2012 07:02

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: BATCH rename files using a criterion txt file?

#2 Post by !k » 06 Apr 2012 07:48

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
)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: BATCH rename files using a criterion txt file?

#3 Post by abc0502 » 06 Apr 2012 07:48

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

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: BATCH rename files using a criterion txt file?

#4 Post by tinfanide » 06 Apr 2012 07:55

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.

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: BATCH rename files using a criterion txt file?

#5 Post by tinfanide » 06 Apr 2012 08:04

!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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: BATCH rename files using a criterion txt file?

#6 Post by abc0502 » 06 Apr 2012 08:05

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

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: BATCH rename files using a criterion txt file?

#7 Post by tinfanide » 06 Apr 2012 08:20

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: BATCH rename files using a criterion txt file?

#8 Post by abc0502 » 06 Apr 2012 08:25

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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: BATCH rename files using a criterion txt file?

#9 Post by abc0502 » 06 Apr 2012 08:49

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: BATCH rename files using a criterion txt file?

#10 Post by !k » 06 Apr 2012 08:55

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"

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: BATCH rename files using a criterion txt file?

#11 Post by abc0502 » 06 Apr 2012 09:05

!k it work with me here
with no errors at all :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: BATCH rename files using a criterion txt file?

#12 Post by foxidrive » 06 Apr 2012 09:09

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

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: BATCH rename files using a criterion txt file?

#13 Post by tinfanide » 06 Apr 2012 10:01

!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

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: BATCH rename files using a criterion txt file?

#14 Post by tinfanide » 06 Apr 2012 10:02

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.

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: BATCH rename files using a criterion txt file?

#15 Post by tinfanide » 06 Apr 2012 10:39

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.

Post Reply