• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

Need DOS batch file

I need a DOS script to delete all the files in a specified subdirectory that have not been changed today.  The purpose is to delete processed web logs from a Windows 2000 server, and I thought rather than spend hours trying to figure out how to do this in a batch file I should just ask for help.

The batch file will have to live in the directory above the folder to be cleaned out, that way I can just pass it the name of that folder, rather than an absolute path which could obviously be dangerous. Passing the batch file nothing would ideally give the usage although this might be asking a bit much.

For example if the batch file was called clearDirectory

C:\>clearDirectory.bat -foo

would for each file in subdirectory foo, delete the file if it was last modified before today. That way the batch file won't try to delete web logs that are currently in use (the batch file will actually be called by the hit reporting program).
0
tsongas
Asked:
tsongas
  • 4
  • 4
1 Solution
 
sirbountyCommented:
Try this...(remove the ECHO in front of DEL if it displays what you want it to...)

clearDirectory.bat:
===========
@echo off
if [%1]==[] goto err
set dName=%1
for /f "tokens=1,5" %%a in ('dir foo\*.txt') do call :process %%a %%b
goto :eof

:err
CLS
echo. Error: No folder specified.
echo.
echo. USAGE: CLEARDIRECTORY FolderName
goto :eof
         
:process
set fDate=%1
set fName=%2
if [%fDate%]==[Volume] goto :eof
if [%fName%]==[] goto :eof
if %fDate% NEQ %date% ECHO DEL .\%dName%\%fName%
0
 
tsongasAuthor Commented:
Thanks for getting me started. It looks like the subdirectory is hard-coded as "foo" which was just an example, can we make the batch file use the specified subdirectory?  Also when I created a subdirectory named foo with a few files older than today inside, I got an error for each file saying

'04' is not recognized as an internal or external command, operable program or batch file.

I'm upping the point value in consideration of additional help.
0
 
sirbountyCommented:
replace this:
 for /f "tokens=1,5" %%a in ('dir foo\*.txt') do call :process %%a %%b

with this:
 for /f "tokens=1,5" %%a in ('dir %dName%\*.txt') do call :process %%a %%b

>>Also when I created a subdirectory named foo with a few files older than today inside, I got an error for each file saying
'04' is not recognized as an internal or external command, operable program or batch file.<<

Not sure I follow you here...Can you elaborate a bit?  Thanx
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
tsongasAuthor Commented:
I just meant you get that error when the batch file does find the folder.  I substituted %dName% for foo and get that same error when I run the batch file, passing it the name of an existing folder:

'04' is not recognized as an internal or external command, operable program or batch file.

The only place I can see '04' coming from is the end of the year of the date last modified....
0
 
sirbountyCommented:
Has to do with your 'date'...

Try start->run->cmd
echo %date%
or
date/t

Your OS list the 'day' as well?  Then try this:

Add this line:
  set date=%date:~4%
before this one:
 if [%1]==[] goto err
0
 
tsongasAuthor Commented:
O.K. I had to change "tokens=1,5" to "tokens=1,4" to get the filename on my system.  I also had to add lines to the process routine saying

if [%fDate%]==[Directory] goto :eof

and

if [%fName%]==[bytes] goto :eof

so it would ignore the "Directory of C:\whatever" and byte count output of the dir command.

Now the batch file works as expected the first time I run it, but if I hit the up arrow in my command shell and run it again, the second and third times it echoes all files regardless of their modified date, the fourth time it gives me that

'04' is not recognized as an internal or external command, operable program or batch file.

error, then the next time I run it works, then two more echoing all files, then the error, then it works, on and on.  This is repeatable every time I open a new command shell.  Strange, is some value like the date persisting somehow and causing problems?

Thanks, I'm upping the points again.
0
 
sirbountyCommented:
Okay - a little cha-cha here and...the revised script.
Excludes a lot of the nonsense-checking from before...

@echo off
set cDate=%date:~4%
if [%1]==[] goto err
set dName=%1
pushd %dName%
for /f "tokens=1,5" %%x in ('for %%a in ^(*.*^) do dir %%a^|find /i "%%~nxa"') do call :process %%x %%y
popd
set cDate=
set fDate=
set fName=
goto :eof

:err
CLS
echo. Error: No folder specified.
echo.
echo. USAGE: CLEARDIRECTORY FolderName
goto :eof
         
:process
set fDate=%1
set fName=%2
if [%fName%]==[] goto :eof
if %fDate% NEQ %cDate% ECHO ABOUT TO DEL %fName%
0
 
tsongasAuthor Commented:
Thanks, this will work for me. Note I did have to change "tokens=1,5" to "tokens=1,4" for this to work on my system, as well as change

if %fDate% NEQ %cDate% DEL .\%dName%\%fName%

to use an absolute path i.e.

if %fDate% NEQ %cDate% DEL C:\Test\%dName%\%fName%

otherwise I would get a file not found error, even though I could delete files using the relative path syntax via the command line while in the directory containing the batch file.

Also I would warn others wanting to use this that it would need further modification to work with paths or filenames containing spaces.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now