Computing.Net > Forums > Windows XP > nested loop in batch file not working

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

nested loop in batch file not working

Reply to Message Icon

Name: maya_zakry
Date: November 3, 2009 at 01:06:31 Pacific
OS: Windows XP
Subcategory: General
Tags: nested for, batch
Comment:

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
)



Sponsored Link
Ads by Google

Response Number 1
Name: ace_omega
Date: November 3, 2009 at 07:25:41 Pacific
Reply:

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
  )
)


0

Response Number 2
Name: maya_zakry
Date: November 3, 2009 at 16:33:58 Pacific
Reply:

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.



0

Response Number 3
Name: ace_omega
Date: November 4, 2009 at 07:11:49 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Firefox favourites can't use my keyboard to ...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: nested loop in batch file not working

Hosts file not working www.computing.net/answers/windows-xp/hosts-file-not-working/143274.html

links in OE 6 not working (the fix) www.computing.net/answers/windows-xp/links-in-oe-6-not-working-the-fix/24229.html

difference b/w %%g and %g% in batch file www.computing.net/answers/windows-xp/difference-bw-g-and-g-in-batch-file/176113.html