| Author |
Message |
|
tinfanide
Joined: 05 Sep 2011 09:15 Posts: 114
|
 BATCH rename files using a criterion txt file?
log.log Current directory 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: @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
|
| 06 Apr 2012 07:02 |
|
 |
|
!k
Expert
Joined: 17 Oct 2009 08:30 Posts: 378 Location: Russia
|
 Re: BATCH rename files using a criterion txt file?
Code: @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 )
|
| 06 Apr 2012 07:48 |
|
 |
|
abc0502
Joined: 26 Oct 2011 22:38 Posts: 1006 Location: Egypt
|
 Re: BATCH rename files using a criterion txt file?
Try this it worked for me: Code: @ECHO OFF
for /f %%a in ('dir *.txt /b') do ( for /f %%b in (log.log) do ren %%a %%b.txt ) pause
|
| 06 Apr 2012 07:48 |
|
 |
|
tinfanide
Joined: 05 Sep 2011 09:15 Posts: 114
|
 Re: BATCH rename files using a criterion txt file?
abc0502 wrote: Try this it worked for me: Code: @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: Quote: 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.
|
| 06 Apr 2012 07:55 |
|
 |
|
tinfanide
Joined: 05 Sep 2011 09:15 Posts: 114
|
 Re: BATCH rename files using a criterion txt file?
!k wrote: Code: @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.
|
| 06 Apr 2012 08:04 |
|
 |
|
abc0502
Joined: 26 Oct 2011 22:38 Posts: 1006 Location: Egypt
|
 Re: BATCH rename files using a criterion txt file?
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
|
| 06 Apr 2012 08:05 |
|
 |
|
tinfanide
Joined: 05 Sep 2011 09:15 Posts: 114
|
 Re: BATCH rename files using a criterion txt file?
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.
|
| 06 Apr 2012 08:20 |
|
 |
|
abc0502
Joined: 26 Oct 2011 22:38 Posts: 1006 Location: Egypt
|
 Re: BATCH rename files using a criterion txt file?
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
|
| 06 Apr 2012 08:25 |
|
 |
|
abc0502
Joined: 26 Oct 2011 22:38 Posts: 1006 Location: Egypt
|
 Re: BATCH rename files using a criterion txt file?
i know now why is the error when i used this: Code: @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: 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
|
| 06 Apr 2012 08:49 |
|
 |
|
!k
Expert
Joined: 17 Oct 2009 08:30 Posts: 378 Location: Russia
|
 Re: BATCH rename files using a criterion txt file?
tinfanide wrote: It does not work, with the cmd window popping out for half a second and disappearing. remove "echo" in Code: echo ren %1 "%%b.txt"
|
| 06 Apr 2012 08:55 |
|
 |
|
abc0502
Joined: 26 Oct 2011 22:38 Posts: 1006 Location: Egypt
|
 Re: BATCH rename files using a criterion txt file?
!k it work with me here with no errors at all 
|
| 06 Apr 2012 09:05 |
|
 |
|
foxidrive
Joined: 10 Feb 2012 02:20 Posts: 2540
|
 Re: BATCH rename files using a criterion txt file?
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: @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
|
| 06 Apr 2012 09:09 |
|
 |
|
tinfanide
Joined: 05 Sep 2011 09:15 Posts: 114
|
 Re: BATCH rename files using a criterion txt file?
!k wrote: Code: @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: 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?
|
| 06 Apr 2012 10:01 |
|
 |
|
tinfanide
Joined: 05 Sep 2011 09:15 Posts: 114
|
 Re: BATCH rename files using a criterion txt file?
abc0502 wrote: i know now why is the error when i used this: Code: @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: 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.
|
| 06 Apr 2012 10:02 |
|
 |
|
tinfanide
Joined: 05 Sep 2011 09:15 Posts: 114
|
 Re: BATCH rename files using a criterion txt file?
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: @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? And a lot more I cannot make sense of... The FOR loop is inside a () Why set var = nothing? Code: 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.
|
| 06 Apr 2012 10:39 |
|
|