Solved

Need DOS batch file

Posted on 2004-04-14
8
567 Views
Last Modified: 2013-12-03
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
Comment
Question by:tsongas
  • 4
  • 4
8 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 10825383
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
 

Author Comment

by:tsongas
ID: 10826441
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 10827286
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
 

Author Comment

by:tsongas
ID: 10827731
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 67

Expert Comment

by:sirbounty
ID: 10828324
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
 

Author Comment

by:tsongas
ID: 10828930
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 350 total points
ID: 10829773
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
 

Author Comment

by:tsongas
ID: 10956392
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now