HOME
TOPICS
SEARCH
ABOUT ME
MAIL

 
Complicated batch file gets rid of the junk in the TEMP folder while sparing anything created or used on that day.
  technofile
Al Fasoldt's reviews and commentaries, continuously available online since 1983

Batch file project: Batch file that spares current files and folders while deleting contents of the TEMP folder


By Al Fasoldt
Copyright © 1998, Al Fasoldt


   Please read this introduction if you are unfamiliar with batch files. The batch file is listed after this introduction. (Experienced DOS users can skip this section.)

   
   Batch files are texts that contain instructions for the MS-DOS command processor. They are texts that contain the same kind of commands that you could type on the DOS command line, either in a DOS window while running Windows or when the PC is running only DOS.
   Create a batch file using Notepad or any other editor (or, less suitably, a word processor) with word-wrap turned off. Save the file as a text using the name listed above in big type. Make sure the file has a .BAT filename extension, like this: MYFILE.BAT. You can cut and paste from your browser window to create the batch file listed here.
   You run a batch file by typing the first part of its name from a DOS command line or from another batch file. To run MYFILE.BAT, you'd type "MYFILE" at the command line, or you'd run it from a batch file with a line that says the same thing. You should make sure that all your batch files are located in the path that MS-DOS uses. This could be "C:\Windows\Command" under Windows 95 or "C:\DOS" under Windows 3.1 and 3.11, or it could be a special folder (called "BAT," maybe) somewhere else. If you use a special folder, make sure the location of that folder is part of the path statement in your PC's AUTOEXEC.BAT. (Note well: If you have Windows 95, you do not need to do anything fancy; just put the batch folder's name in the AUTOEXEC.BAT this way:
  SET PATH=C:\BAT
and leave out the "C:\Windows; C:\Windows\Command" stuff entirely. Windows 95 ALWAYS puts its own folders in the path.)
   
********** BATCH FILE BEGINS HERE. **********

@echo off

if %4()==() goto top

goto routines



current.bat

Al Fasoldt 2-22-98



With appreciation to Ronny Richardson

for the original idea of using the DOS date command

to create a temporary batch file.





(Please do not put REMs in front of any lines.)



NOTE: This is not your usual batch file.

It creates a temporary batch file, then runs it.

That batch file then runs this batch file.

I like recursiveness (I like recursiveness).



This MUST be called CURRENT.BAT or it will not run.



If you run this from a floppy disk, it will go REAL slow.

Run it from your hard drive.



---------------------------------------------

 DO NOT RUN THIS FROM THE AUTOEXEC.BAT file!

---------------------------------------------



IT MUST RUN FROM WITHIN A WINDOWS 95 DOS WINDOW.

IT WILL DESTROY ALL LONG FILENAMES FOR ANY SETUP PROGRAMS

THAT ARE TEMPORARILY USING THE TEMP FOLDER

IF IT IS RUN FROM THE AUTOEXEC.BAT.





:top

echo. | date | find "Current ">storedat.bat

storedat

goto routines







EXPLANATION OF THE CODE ABOVE

=============================



When CURRENT.BAT is run, it looks for a fourth parameter.

If it can't find one, it creates STOREDAT.BAT and then

runs the batch file it just created. This guarantess

that CURRENT.BAT will be rerun with the proper date params.



STOREDAT.BAT consists of one line,

created by the DOS date function.

It looks like this:

 Current date is Wed 02-18-1998



Since there is an executable file called CURRENT.BAT

(this file), STOREDAT.BAT runs CURRENT.BAT (it's the

first instruction in STOREDAT.BAT) with parameters,

namely DATE IS [DAY] [DATE nn-nn-nnn]. The CALDATE variable

used below is picked up from the fourth parameter.

DAYWEEK is, as can be seen, the third parameter. It is

used merely to show the day. If you're clever, you can

place the DAYWEEK variable into the Windows 95 global

environment using WINSET, a free program from Microsoft

included on the Windows 95 CD. (That's right. A DOS batch

file can insert a global environment variable using WINSET.

See the WINSET documentation.)







:routines

set DAYWEEK=%3

set CALDATE=%4

if %DAYWEEK%==Mon set DAY=Monday

if %DAYWEEK%==Tue set DAY=Tuesday

if %DAYWEEK%==Wed set DAY=Wednesday

if %DAYWEEK%==Thu set DAY=Thursday

if %DAYWEEK%==Fri set DAY=Friday

if %DAYWEEK%==Sat set DAY=Saturday

if %DAYWEEK%==Sun set DAY=Sunday

set DAYWEEK=



echo Today is %DAY%. The date is %CALDATE%.

echo.



if exist c:\temphold\nul deltree /y c:\temphold

:: Gets rid of this folder if it exists.

md c:\temphold

:: Creates it.



xcopy c:\windows\temp c:\temphold /s /h /c /i /r /d:%CALDATE%

:: Copies all files with the current day's date.

:: Xcopy's switches copy all files and folders (even hidden ones)

:: and do other necessary things. See Xcopy's help screen.



deltree /y c:\windows\temp >nul

:: Removes the temp folder. If a file is in use,

:: the folder will NOT be deleted.



xcopy c:\temphold c:\windows\temp /s /h /c /i /r >nul

:: Copies the live stuff back.



deltree /y c:\temphold

:: And now we are at last free of the older temp junk.



del storedat.bat

:: And we've cleaned up the temp batch file that this one uses.

:: This actually isn't needed, since the routines used at top

:: create the new temp batch file anyway, whether the old one

:: exists or not, but I like clean programming.



:end



********** BATCH FILE ENDS HERE. **********