DosTips.com

A Forum all about DOS Batch
It is currently 30 Apr 2016 18:25

All times are UTC-06:00




Post new topic  Reply to topic  [ 19 posts ]  Go to page Previous 1 2
Author Message
PostPosted: 06 Apr 2012 12:44 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
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?


Top
   
PostPosted: 06 Apr 2012 21:51 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5567
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.


Top
   
PostPosted: 07 Apr 2012 01:16 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 782
Location: Germany
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


Top
   
PostPosted: 07 Apr 2012 01:54 
Offline

Joined: 05 Sep 2011 09:15
Posts: 114
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,


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 19 posts ]  Go to page Previous 1 2

All times are UTC-06:00


Who is online

Users browsing this forum: Yahoo [Bot] and 5 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® Forum Software © phpBB Limited