nested loop in batch file not working

November 3, 2009 at 01:06:31
Specs: Windows XP
Hi,
Im trying to do nested for below :-
1. loop out thru sharepoint
2. for every sharepoint, copy only recent files, for testing i just echo the most recent filename.

here what i came out and didnot work as i expected for 2 loop..

@echo off

For /F "tokens=1,2,3,4" %%a in (Tools.txt) Do (

echo %%a,%%b,%%c,%%d >> extract.txt

for /f "delims=" %%m in ('dir /od /a-d /b %%a\*.txt') do set recent=%%m

echo %%m >> recent.txt
)


See More: nested loop in batch file not working

Report •


#1
November 3, 2009 at 07:25:41
Your missing a set of ()....

@echo off

For /F "tokens=1,2,3,4" %%a in (Tools.txt) Do (
  echo %%a,%%b,%%c,%%d >> extract.txt
  for /f "delims=" %%m in ('dir /od /a-d /b %%a\*.txt') do (
    set recent=%%m REM NOT SURE WHAT YOU ARE DOING WITH THIS LINE BUT IT WILL NOT WORK.
    echo %%m >> recent.txt
  )
)


Report •

#2
November 3, 2009 at 16:33:58
thanks ace, but it didnot work either.

what im trying to do in 1st 'for' loop is to get pathname from file below (the \\P2P...)

Tools.txt
TLDS15 \\P2P\LogFile\TLDS CCP cps26

and when i found the path from that file, i would like to copy the most recent file in the path, sort descending and use for loop again :-
for /f "delims=" %%m in ('dir /od /a-d /b %%a\*.txt')

but this 2nd loop is not working. If i take it out from 1st loop, it will work, but i cannot use variable for the path..

do u have any idea or any different way to do this?
get path from file, and copy most recent file in that path.



Report •

#3
November 4, 2009 at 07:11:49
I have no idea what you are doing so it is very hard to help. I can only point things out. The code you are using seems a little to complex. Also %%A is going to to return the first column and your path seems to be in the second column so wouldn't %%B be a better variable to use in your dir? Again, I don't know what this is doing so. Also why loop through your dir when you could just redirect it to the output file? Try this code out...

@echo off
For /F "tokens=1,2,3,4" %%a in (Tools.txt) Do (
  echo %%a,%%b,%%c,%%d >> extract.txt
  dir "%%b\*.txt" /od /a-d /b >> recent.txt)

Also, this will fail if you have any spaces in the directory because it is using a <space> for the delimiter in your loop and it will cut off the directory names with spaces in them. Is there any way to modify your tools.txt to put the directory at the end like...

TLDS15 CCP cps26 \\P2P\LogFile\TLDS

then you could do...

@echo off
For /F "tokens=1,2,3,*" %%a in (Tools.txt) Do (
  echo %%a,%%b,%%c,%%d >> extract.txt
  dir "%d\*.txt" /od /a-d /b % >> recent.txt)

If you have no control over tools.txt and you are sure the path will never have a space in it then the code should work.


Report •

Related Solutions


Ask Question