AUTOMATED CLEANUP / DEFRAG
NOTE: This next batch code example is very useful but not easilly made into a generic version that will work on every system. It needs customizing to suit your setup and what you want it to do. I will include it with the other example code but do not execute this MAX command until you are certain it only does what you want it to do.
Hard drives and even diskettes become fragmented and we must use a third party DEFRAG program to put these pieces back in one contiguous area. The problem is we often do this before removing temporary files and then when we delete these temporary files at some later time we create an opportunity for DOS to begin scattering parts of the next files we write to the hard drive or diskette by re-using all these little empty spaces from our deleted temporary files.
The Solution, for me, was to automate this process so that I don't forget to remove temporary files and to use either CHKDSK or SCANDISK before attempting to defragment the hard drive ( always a potential disaster if things go badly).
Deleting temporary files that remain from our use of the ctrl-alt-del escape keys or worse, having to turn the machine off / on to reboot, can be risky. If they have crossed clusters deleting them can take out parts of good files. To attempt to avoid this mistake we do CHKDSK or SCANDSK first on all partitions with the MAX command below:
( EXAMPLE4.BAT - part A )
:rem ----------- repair any crossed clusters
:
:@check
:
for %%i in (c: d: e: f:) do chkdsk %%i /f
:
goto done
:
The drive letters ( partitions ) will need to be edited to suit what you have on your system. For newer DOS versions you would replace CHKDSK with SCANDSK and the appropriate switches that you would normally want to use. Typing "max check" after a crash would be a good idea if you don't want to spend time reformatting your hard drive and restoring all of your files using your backups. You DO keep backups, right?
The removal of temporary files using UFIND is next and should be all one line but it won't fit on a smaller 640x480 webpage display as one line so this will require editing to add with cut-n-paste. This next command does the deleting of the temporary files - finally!
( EXAMPLE4.BAT - part B )
:@rem ----------- delete temp files
:
:@clean
ufind %1 -name *.~mp *.gid *.fts *.chk y*.---
*.$$$ *.*$ ~*.* *.~* *.??_ *.??~ *.^* *.syd
*.prv *.bak ip-up.* -rm
:
goto done
:
Remember UFIND should be all one line in your batch file. This scans the entire drive, not just one directory. If files with these extensions and all wildcard variables of these extensions exist anywhere on the specified drive they will be erased.
The "*.??_" will remove W3.x compressed files from your hard drive. Many keep them available on their hard drive rather than use the install diskettes and do not want them deleted. If you are one of those people then definitely remove the "*.??_" from this command. Examine this line carefully for file types that you do not want to delete. I can only say that with DOS and W3.x here no files that I need are deleted using this routine in MAX.
The utility UFIND can be downloaded separately from this server and is included with the MAX archive also available here. UFIND is an older DOS utility and will not work properly with every version of DOS. I do not have every version of DOS and cannot, therefore, be more specific. I suspect that it will stop functioning with hard drive partitions of 2gig and/or a total drive size in the range of 8 gig? Untested but be forewarned.
There are other utilities that will scan entire drives and delete specified file types. I have used UFIND for this but nothing is stopping anyone who has a preference for any other similar utility from substituting their favorite. Be certain that you understand the syntax for any other utility you use to replace UFIND! DETLTREE is not the same thing and might delete every directory and every file on every drive. Be careful with this. If this seems to be overkill to you, an optional format would be to specify directories this way:
:@rem ----------- delete temp files
:
:@clean
if exist %temp%\*.* delete %temp%\*.$$$
if exist %temp%\*.* delete %temp%\*.bak
if exist %temp%\*.* delete %temp%\*.prv
... etc. etc. etc.
The `if exist' is used to avoid file not found messages on your screen when this is executing and delete can't find the file you want to delete. Older versions of DOS would use "del" to replace "delete". This would give you more control to leave certain file types while removing others and only in certain directories. In this example any directory designated as your `temp' directory in your environment using the `set temp={drv}{dir}' command (usually done in autoexec.bat). This would take longer to setup properly but you can surgically remove only certain files and once it's working it's working forever!
Now we can hope to safely remove any temporary files left behind on our hard drive by a multitude of programs that think they own all of your available space! This is done by stepping through all of your drive letters and calling `clean' using:
( EXAMPLE4.BAT - part C )
:@rem ----------- loop for 'clean'
:
:@cleanall
:
echo Next op will delete any
echo Win setup files ending in "*.??_"
pause
for %%i in (c: d: e: f:) do call max clean %%i
:
:@free
for %%i in (c: d: e: f:) do free %%i
:
goto done
:
This has two MAX commands in it, max cleanall and max free . I use max free to keep track of how much space I have on each partition and after removing the temporary files it's interesting to see how much space you have reclaimed. The FREE.COM utility will not work on newer large drives but is included for those with older versions of DOS that did not have this function. With "cleanall" you see that MAX can call itself ( recursion ) which allows us to break up complex tasks or to build complex tasks from smaller ones. I added the "pause" just in case I was in the middle of an install and did not want to lose the compressed files. Press CTRL-BREAK at this point if you are not certain that deleting these files will be OK.
Now we combine these MAX commands into one routine by calling each one from within MAX.
( EXAMPLE4.BAT - part D )
:rem ----------- remove all temp files
:
:@cleanup
:
call max check
call max cleanall
:
goto done
:
DOS versions prior to v3.3 MSDOS did not have the CALL command and should use "command /c" to replace both instances of CALL. The "@echo off" should also be replaced for older versions of DOS with "echo off". Anyone with a version of DOS older than v2.11 cannot read subdirectories and should probably upgrade their DOS to one of the newer free versions such as DRDOS or FreeDOS?
The command "max cleanup" will check for crossed clusters and then remove all temporary files from all partitions on your hard drive. You can now safely and effectively defragment your hard drive. I will leave the automation of the defrag program as an exercise for each user. There are many defrag programs and I would probably pick the one you do not have. If your older DOS version does not have a defrag program you can try one of these . I used DOG for many years with v3.1 and v3.3 MSDOS.
I used the PcTools COMPRESS program after trying several others because it continuously rewrites to the FAT so that interruptions and power failures won't take out my entire partition. I also used to finish with MIRROR.COM to keep a copy of the FAT for emergencies. The defrag included with MSDOS v6.22 is written to function in a similar fashion and I am confident that it is a good choice to defrag my drives with. I have switched to the MSDOS v6.22 defrag now.