recursive batch file

S

Sid Knee

I have a simple batch file (propurge.bat) which is constructed as follows:

cd %1 %2 %3 %4 %5
purge.bat

It takes a directory name which is passed in the variables %1, %2 ...
etc (takes care of spaces in the directory name) and runs the second
batch file, purge.bat, on the contents of that directory (deletes any
cad backup files).

As written, of course, it only works at a single directory level. Can
anyone advise me how to go about constructing a similar batch files that
would carry out the purge in the indicated directory and all
subdirectories (and sub-subdirectories etc)?
 
P

Pegasus \(MVP\)

Sid Knee said:
I have a simple batch file (propurge.bat) which is constructed as follows:

cd %1 %2 %3 %4 %5
purge.bat

It takes a directory name which is passed in the variables %1, %2 ...
etc (takes care of spaces in the directory name) and runs the second
batch file, purge.bat, on the contents of that directory (deletes any
cad backup files).

As written, of course, it only works at a single directory level. Can
anyone advise me how to go about constructing a similar batch files that
would carry out the purge in the indicated directory and all
subdirectories (and sub-subdirectories etc)?

You don't need to do any recursion for this. The command

del *.* /s /q

will do it quite nicely, without destroying your folder structure.
 
S

Sid Knee

Pegasus said:
You don't need to do any recursion for this. The command

del *.* /s /q

will do it quite nicely, without destroying your folder structure.

I'm afraid that in my attempt to keep it short and reduce obfuscation, I
perhaps didn't explain too well. It isn't a simple question of deleting
all the files in the directories (if it were, the second batch file -
purge.bat - wouldn't be needed).

The cad system creates a new file with every save - earlier files
serving as backup or undo points. The first file is saved as say
xxx.prt.1, the next as xxx.prt.2, then xxx.prt.3 and so on.

The purpose of the purge operation is to remove all the earlier versions
leaving only the latest (highest numbered) version. That is what the
second batch file does .... it does a bunch of housekeeping establishing
operating system and actual installed program directory structure and
then calls an executable - purge.exe - to do the actual file deletion in
the directory to which it is pointed (it also handles a bunch of other
possible base file extensions such as asm.x, dwg.x etc as well as other
miscellaneous files left by the cad program). Unfortunately, purge.exe
doesn't accept any command-line parameters similar to del.exe's /s etc

The main command calling sequence is'

propurge.bat ---> purge.bat ----> purge.exe

Propurge.bat is added as a right-click command to <file directory> and
passes that filename to purge.bat via the batch variables.

This only works at one directory level at a time but it's a nuisance
with a complex directory structure to have to manually go to each
level/branch and purge them individually. It would be nice (albeit
perhaps a bit dangerous) to be able to purge from the top level and have
it propagate down.
 
P

Pegasus \(MVP\)

Sid Knee said:
I'm afraid that in my attempt to keep it short and reduce obfuscation, I
perhaps didn't explain too well. It isn't a simple question of deleting
all the files in the directories (if it were, the second batch file -
purge.bat - wouldn't be needed).

The cad system creates a new file with every save - earlier files
serving as backup or undo points. The first file is saved as say
xxx.prt.1, the next as xxx.prt.2, then xxx.prt.3 and so on.

The purpose of the purge operation is to remove all the earlier versions
leaving only the latest (highest numbered) version. That is what the
second batch file does .... it does a bunch of housekeeping establishing
operating system and actual installed program directory structure and
then calls an executable - purge.exe - to do the actual file deletion in
the directory to which it is pointed (it also handles a bunch of other
possible base file extensions such as asm.x, dwg.x etc as well as other
miscellaneous files left by the cad program). Unfortunately, purge.exe
doesn't accept any command-line parameters similar to del.exe's /s etc

The main command calling sequence is'

propurge.bat ---> purge.bat ----> purge.exe

Propurge.bat is added as a right-click command to <file directory> and
passes that filename to purge.bat via the batch variables.

This only works at one directory level at a time but it's a nuisance
with a complex directory structure to have to manually go to each
level/branch and purge them individually. It would be nice (albeit
perhaps a bit dangerous) to be able to purge from the top level and have
it propagate down.

Maybe this will help:

@echo off
setlocal enabledelayedexpansion
for /r %%a in (.) do (
set folder=%%a
echo The current folder is !folder:~0,-2!
)
endlocal

Post again if you need further explanations!
 
S

Sid Knee

Pegasus said:
Maybe this will help:

@echo off
setlocal enabledelayedexpansion
for /r %%a in (.) do (
set folder=%%a
echo The current folder is !folder:~0,-2!
)
endlocal

Post again if you need further explanations!

Thanks, Pegasus. I don't profess to understand that at first glance, but
let me run with it and see what I can do ... it'll be a good learning
experience (haven't messed with anything other than very basic batch
files in ages). If I run into trouble, I'll get back to you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top