Page 2 of 2

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 12:44
by tinfanide
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?

Re: BATCH rename files using a criterion txt file?

Posted: 06 Apr 2012 21:51
by foxidrive
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.

Re: BATCH rename files using a criterion txt file?

Posted: 07 Apr 2012 01:16
by jeb
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

Re: BATCH rename files using a criterion txt file?

Posted: 07 Apr 2012 01:54
by tinfanide
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,