Solved

Need DOS batch file

Posted on 2004-04-14
8
574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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
 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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