delete files on date basis with forfiles


M

Marc

Hello,

I was asked to write a little Batch-Job to delete Files on a Windows 2000
Server.

Files, which are older than a specified date, should be deleted in a
directorystructure called c:\temp\test.
In this directory a number of subdirectories are located, so that the
scripts has to work recurse folders.

Example:
c:\temp\test\1\*.*
c:\temp\test\2\*.*
c:\temp\test\dir3\*.*
....

For this I found the below statement with the MS tool forfiles:

forfiles -pC:\temp\test\ -m*.* -d-%1 -s -c"cmd /c del @FILE| echo @FILE"

Unfortunately the Scripts tries to delete the directories as well. Only
files with the mask *.* or other definitions should be deleted.

Many thanks for help,
Marc



------------------------------------------
Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand]
[-d<+|-><DDMMYYYY|DD>] [-s]

-pPath Path where to start searching
-mSearch Mask Search files according to <Search Mask>
-cCommand Command to execute on each file(s)
-d<+|-><DDMMYYYY|DD> Select files with date >= or <=DDMMYYYY (UTC)
or files having date >= or <= (current date - DD days)
-s Recurse directories
-v Verbose mode

The following variables can be used in Command :
@FILE, @FNAME_WITHOUT_EXT, @EXT, @PATH, @RELPATH, @ISDIR, @FSIZE, @FDATE,
@FTIME

To include special hexa characters in the command line : use 0xHH

Default : <Directory : .> <Search Mask : *.*> <Command : "CMD /C Echo
@FILE">
Examples :
FORFILES -pc:\ -s -m*.BAT -c"CMD /C Echo @FILE is a batch file"
FORFILES -pc:\ -s -m*.* -c"CMD /C if @ISDIR==TRUE echo @FILE is a directory"
FORFILES -pc:\ -s -m*.* -d-100 -c"CMD /C Echo @FILE : date >= 100 days"
FORFILES -pc:\ -s -m*.* -d-01011993 -c"CMD /C Echo @FILE is quite old!"
FORFILES -pc:\ -s -m*.* -c"CMD /c echo extension of @FILE is [email protected]"
 
Ad

Advertisements

M

Marty List

Marc said:
Hello,

I was asked to write a little Batch-Job to delete Files on a Windows 2000
Server.

Files, which are older than a specified date, should be deleted in a
directorystructure called c:\temp\test.
In this directory a number of subdirectories are located, so that the
scripts has to work recurse folders.

Example:
c:\temp\test\1\*.*
c:\temp\test\2\*.*
c:\temp\test\dir3\*.*
...

For this I found the below statement with the MS tool forfiles:

forfiles -pC:\temp\test\ -m*.* -d-%1 -s -c"cmd /c del @FILE| echo @FILE"

Unfortunately the Scripts tries to delete the directories as well. Only
files with the mask *.* or other definitions should be deleted.

Many thanks for help,
Marc



------------------------------------------
Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand]
[-d<+|-><DDMMYYYY|DD>] [-s]

-pPath Path where to start searching
-mSearch Mask Search files according to <Search Mask>
-cCommand Command to execute on each file(s)
-d<+|-><DDMMYYYY|DD> Select files with date >= or <=DDMMYYYY (UTC)
or files having date >= or <= (current date - DD days)
-s Recurse directories
-v Verbose mode

The following variables can be used in Command :
@FILE, @FNAME_WITHOUT_EXT, @EXT, @PATH, @RELPATH, @ISDIR, @FSIZE, @FDATE,
@FTIME

To include special hexa characters in the command line : use 0xHH

Default : <Directory : .> <Search Mask : *.*> <Command : "CMD /C Echo
@FILE">
Examples :
FORFILES -pc:\ -s -m*.BAT -c"CMD /C Echo @FILE is a batch file"
FORFILES -pc:\ -s -m*.* -c"CMD /C if @ISDIR==TRUE echo @FILE is a directory"
FORFILES -pc:\ -s -m*.* -d-100 -c"CMD /C Echo @FILE : date >= 100 days"
FORFILES -pc:\ -s -m*.* -d-01011993 -c"CMD /C Echo @FILE is quite old!"
FORFILES -pc:\ -s -m*.* -c"CMD /c echo extension of @FILE is [email protected]"

I haven't tested this, but one solution would be to test for a directory
first:

forfiles -pC:\temp\test\ -m*.* -d-%1 -s -c"cmd /c if @ISDIR==FALSE del
@FILE|echo @FILE"

or

forfiles -pC:\temp\test\ -m*.* -d-%1 -s -c"cmd /c if not exist @FILE\* del
@FILE|echo @FILE"


Also, if any paths might have spaces in the name you will need to surround
them with double quotes.
 
Ad

Advertisements

M

Marc

Many thanks,

it works fine with the first statement.

I've tested the statement and have seen no error. There are no directory
names with spaces included, so this may be interesting only if I have to do
an expert version with free choice of directories.

Regards,
Marc





Marty List said:
Marc said:
Hello,

I was asked to write a little Batch-Job to delete Files on a Windows 2000
Server.

Files, which are older than a specified date, should be deleted in a
directorystructure called c:\temp\test.
In this directory a number of subdirectories are located, so that the
scripts has to work recurse folders.

Example:
c:\temp\test\1\*.*
c:\temp\test\2\*.*
c:\temp\test\dir3\*.*
...

For this I found the below statement with the MS tool forfiles:

forfiles -pC:\temp\test\ -m*.* -d-%1 -s -c"cmd /c del @FILE| echo @FILE"

Unfortunately the Scripts tries to delete the directories as well. Only
files with the mask *.* or other definitions should be deleted.

Many thanks for help,
Marc



------------------------------------------
Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand]
[-d<+|-><DDMMYYYY|DD>] [-s]

-pPath Path where to start searching
-mSearch Mask Search files according to <Search Mask>
-cCommand Command to execute on each file(s)
-d<+|-><DDMMYYYY|DD> Select files with date >= or <=DDMMYYYY (UTC)
or files having date >= or <= (current date - DD days)
-s Recurse directories
-v Verbose mode

The following variables can be used in Command :
@FILE, @FNAME_WITHOUT_EXT, @EXT, @PATH, @RELPATH, @ISDIR, @FSIZE, @FDATE,
@FTIME

To include special hexa characters in the command line : use 0xHH

Default : <Directory : .> <Search Mask : *.*> <Command : "CMD /C Echo
@FILE">
Examples :
FORFILES -pc:\ -s -m*.BAT -c"CMD /C Echo @FILE is a batch file"
FORFILES -pc:\ -s -m*.* -c"CMD /C if @ISDIR==TRUE echo @FILE is a directory"
FORFILES -pc:\ -s -m*.* -d-100 -c"CMD /C Echo @FILE : date >= 100 days"
FORFILES -pc:\ -s -m*.* -d-01011993 -c"CMD /C Echo @FILE is quite old!"
FORFILES -pc:\ -s -m*.* -c"CMD /c echo extension of @FILE is [email protected]"

I haven't tested this, but one solution would be to test for a directory
first:

forfiles -pC:\temp\test\ -m*.* -d-%1 -s -c"cmd /c if @ISDIR==FALSE del
@FILE|echo @FILE"

or

forfiles -pC:\temp\test\ -m*.* -d-%1 -s -c"cmd /c if not exist @FILE\* del
@FILE|echo @FILE"


Also, if any paths might have spaces in the name you will need to surround
them with double quotes.
 

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