|
Answer» If I try to use for %%f in (*.txt) do(...), it doesnt seems to process all the text files in the folder, rather it repeats the same file to do the processing.
for e.g if I have 2 text files(One.txt, Two.txt) in a folder, it repeats 2 times since it received 2 text entries, but the funniest part is it ALWAYS look for either One or Two. Even I tried to rename the first file it is processing, but the program doesnt go into 2 text file. instead it stopped the whole process.
Any idea how to get rid of this problem?
Alternatly I've an idea but not sure how to code it:
Simply create a text file by re-directing the output of dir /s command to a text file: then read the text file and process the entry one by one.
Any one can help how to read a text file line by line until EOF.
Thanks a lot for any help
Cheers/RajaI USED these PIECE of code and found bug with For loop:
for %%f in (*.txt) do ( REM get the filemname SET var= set var=%%f
REM echo.%var%
REM so the rename ren %var% xxx
REM I wanted to check which file is modified, hence I paused before I proceed to next text file pause
move xxx archived/%var%
rem reset var before go to next file rem set var= )
I've tested with One.txt and Two.txt, the processing is unpredictable, either it takes only One.txt or Two.txt. Scary man.
Thanks for your assistance.Code: [Select]@echo off REM example of for loop in batch file
REM write 4 lines to a file echo line 1 > readme.txt echo line 2 >> readme.txt echo line 3 >> readme.txt echo line 4 >> readme.txt
REM read them back in a loop for /F "delims==" %%a in (readme.txt) do ( echo %%a )
You need to put FOR /F. It indicates to FOR that whatever is between the parentheses is the source of a series of lines of data to be read and processed.
without the "/F", what is between the parentheses is interpreted as a series of ITEMS.
Code: [Select]for "delims==" %%a in (Apples Pears Oranges) do ( echo %%a )
Output...
Apples Pears Oranges
single-quotes within the parentheses indicates that the data lines are sourced from the output of a command. Without the single-quotes, FOR /F tries to read a (text-)file named (whatever is within the parentheses)
To use wild cards like *.txt use double quotes.
Sometimes I use the output of
dir /b *.txt
My example:
Code: [Select]@echo off REM example of for loop in batch file
REM write 4 lines to a file echo line 1 > readme.txt echo line 2 >> readme.txt echo line 3 >> readme.txt echo line 4 >> readme.txt
REM read them back in a loop
REM FOR EACH ITEM FOUND BY dir /b.... for /F "delims==" %%a in ('dir /b *.txt') do (
REM OR THIS LINE DOES SAME THING REM note double quotes REM for /F "delims==" %%a in ("*.txt") do (
REM echo file NAME echo %%a REM show file CONTENTS type %%a
)
See
FOR /?
from the prompt for more info.
Hope this helps.
I suspect that as you are iterating around all files and you are changing the files, it will get confused (ie changing the order it encounters the files) . Your idea of putting the filelist into a text file is a good one as it will isolate you from the effect of these changes
GrahamHi Folks.
Thanks for helping me:
Here is the piece of code that is working(to benefit to others who require such a piece of code)
REM============================================= @echo off REM example of for loop in batch file
REM write all *.txt into readme(in sorting order by date) dir *.txt /W/B/OD > readme
REM read them back in a loop for /F "delims==" %%a in (readme) do (
echo %%a rename %%a xxx.txt pause REM You can call other batch to process the file then rename back to original file rename xxx.txt %%a ) README ================================
|