BATCH rename files using a criterion txt file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: BATCH rename files using a criterion txt file?

#16 Post by tinfanide » 06 Apr 2012 12:44

Well... I've been playing around with your scripts:

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 ren  "%%a" "!var!%%~xa"
)
)
pause

And kinda figured out what it means in BATCH:

Code: Select all

@echo off
setlocal enabledelayedexpansion

(for /f "delims=" %%a in ('dir *.txt /b /o:n') do (

set /p "var="
if defined var ren  "%%a" "!var!%%~xa"
)
) < "log.log"
pause

It does the same job.
Two things:

Code: Select all

SET var=

not necessary?

Code: Select all

SET /P "var="

Not for the user's input? But waits for input redirected from the file log.log?

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

Re: BATCH rename files using a criterion txt file?

#17 Post by foxidrive » 06 Apr 2012 21:51

tinfanide wrote:

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"




It is redirecting INPUT into the for loop. For each iteration of the for loop a line of the log.log text file is provided as input in the for loop.

The set /p "var=" command accepts this unusual form of input and you can use it inside the loop.

the set var= command initialises the variable for various reasons. If there are more *.txt files than lines in log.log then the variable would otherwise remember the last line of input.


The FOR loop is inside a ()

Code: Select all

(FOR ...
)



This enables the use of redirecting input into the for in do loop.

jeb
Expert
Posts: 849
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: BATCH rename files using a criterion txt file?

#18 Post by jeb » 07 Apr 2012 01:16

foxidrive wrote:the set var= command initialises the variable for various reasons. If there are more *.txt files than lines in log.log then the variable would otherwise remember the last line of input.


And it's neccessary, as the variable will never be deleted, even if there are empty lines in the file.
In this case the old content will be unchanged.

jeb

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

Re: BATCH rename files using a criterion txt file?

#19 Post by tinfanide » 07 Apr 2012 01:54

Yes, I can what you meant after doing a bit of testing:


:: abc.txt

a
b
c


:: 123.txt

1
2

3


Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

< "abc.txt" (FOR /F "delims=" %%A IN (123.txt) DO (
   REM SET var=
   SET /P "var="
   IF DEFINED var ECHO !var!, %%A
)
)


Result:
a, 1
b, 2
c,
c, 3


Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

< "abc.txt" (FOR /F "delims=" %%A IN (123.txt) DO (
   SET var=
   SET /P "var="
   IF DEFINED var ECHO !var!, %%A
)
)



Result:
a, 1
b, 2
c,

Post Reply