View unanswered posts | View active topics It is currently 27 Nov 2014 04:27



Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
BATCH rename files using a criterion txt file? 
Author Message

Joined: 05 Sep 2011 09:15
Posts: 114
Post Re: BATCH rename files using a criterion txt file?
Well... I've been playing around with your scripts:
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 ren  "%%a" "!var!%%~xa"
)
)
pause

And kinda figured out what it means in BATCH:
Code:
@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:
SET var=

not necessary?
Code:
SET /P "var="

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


06 Apr 2012 12:44
Profile

Joined: 10 Feb 2012 02:20
Posts: 4418
Post Re: BATCH rename files using a criterion txt file?
tinfanide wrote:
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?
Code:
< "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.


Quote:
The FOR loop is inside a ()
Code:
(FOR ...
)



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


06 Apr 2012 21:51
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 679
Location: Germany
Post Re: BATCH rename files using a criterion txt file?
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


07 Apr 2012 01:16
Profile

Joined: 05 Sep 2011 09:15
Posts: 114
Post Re: BATCH rename files using a criterion txt file?
Yes, I can what you meant after doing a bit of testing:


Quote:
:: abc.txt

a
b
c


Quote:
:: 123.txt

1
2

3


Code:
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:
Quote:
a, 1
b, 2
c,
c, 3


Code:
SETLOCAL ENABLEDELAYEDEXPANSION

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



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


07 Apr 2012 01:54
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 28 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.