Computing.Net > Forums > Programming > get .bat to call other .bat?

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.

get .bat to call other .bat?

Reply to Message Icon

Name: anon0
Date: February 17, 2009 at 03:56:21 Pacific
OS: Windows Vista
CPU/Ram: C2D, 2GB
Product: N/a / NA
Subcategory: Batch
Comment:

I'm trying to have a main batch file recurse through its subdirectories and call any batch files it encounters there. My problem is limiting the scope each batch to its own subdirectory.

say you have these files
c:\temp\batch.bat

c:\temp\folder1\batch_deletes_txt.bat
c:\temp\folder1\file1.txt
c:\temp\folder1\file2.doc

c:\temp\folder2\batch_deletes_doc.bat
c:\temp\folder2\file3.txt
c:\temp\folder2\file4.doc

So, batch.bat should call batch_deletes_txt.bat which deletes file1.txt only (not file3.txt), and batch.bat should also call batch_deletes_doc.bat which deletes file4.doc only (not file2.doc).

My contents:

batch.bat:
for /r %%X in (*.bat) do if NOT %%X == %0 %%X
REM ^ the "if not" is to prevent infinite loops.

batch_deletes_txt.bat:
for /r %%X in (*.txt) do del "%%X"

batch_deletes_doc.bat:
for /r %%X in (*.doc) do del "%%X"

This doesn't work because the scope of each script is c:\temp, ie. not local. How do I fix this?

(My real goal is not deleting files, this is just an example)



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: February 17, 2009 at 04:10:07 Pacific
Reply:

Replace your first command with this version:

for /r %%X in (*.bat) do if NOT "%%X" == "%~f0" (cd %%~pX && call "%%X")

The for /r command returns all files with fully qualified pathnames, so I am testing against %~f0 which expands the current batch file to its fully qualified pathname.

I am then changing to each batch file's directory before calling it, using the CALL command so that the current batch file doesn't terminate.

I have quotes around pathnames in case they contain spaces.

Caveat: I have not tested this.


0
Reply to Message Icon

Related Posts

See More


reading from file cmd - for loop with ping/...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: get .bat to call other .bat?

batch file to call reg files? www.computing.net/answers/programming/batch-file-to-call-reg-files/8298.html

How to call one batch file from another www.computing.net/answers/programming/how-to-call-one-batch-file-from-another/19151.html

VB6 - syntax to call exec with switch www.computing.net/answers/programming/vb6-syntax-to-call-exec-with-switch/5.html