Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script for deleting *.tmp files! HELP!!!

Status
Not open for further replies.

noodle786

Technical User
Apr 7, 2003
22
0
0
US
Hello,

I need to script a batch file that deletes all .*tmp files from all directories and volumes.

In addition, I would like to write another script that archives files from one folder, and then deletes those files.

I have no clue how to even begin scripting and help or direction would be appreciated.

Thanks,
Nadya
 
Not absolutely clear if this script is to run on a single unit (and so why not simply run a manual search and delete) or across several computers. Let us know...


As regards your second request, a batch script could use XCOPY (watch the switches, namely use /E /Y /X /H /I /V) then simply the DEL command.
 
If you want to delete the *.tmp files on just one machine where you know the partition configuration, the following will work and is extremely simple.

Create a batch file with the following line for each partition that is on the system:

del drive:\ *.tmp /S /Q

The /S switch tells the command to include all subdirectories.
The /Q switch tells the command to delete files without prompting the user.

For example, if you have 3 partitions known as drives C:, D: and F: your batch file will look like this:

DEL C:\ *.tmp /S /Q
DEL D:\ *.tmp /S /Q
DEL F:\ *.tmp /S /Q

You can add the batch file to your Startup folder so that it runs when you log in. You can also create a shortcut on your desktop to allow you to run it manually.

If you need to get fancier than that, you may need some scripting help.
 
Thanks for the help.

This script is going to run on different servers, which have varying number of drives. Ideally, I wanted one script that figures out the drives and deletes the temp files.

I was going to use the script and schedule to run on a weekly basis.

Please let me know your input and suggestions.

Thanks again.
 
You could write a script using error handling but would probably take you longer than simply writing a script for each as given by our friend above. In fact, the benefit of his (and I presume it's a he) script is that it is short and sweet and therefore most likely to work. The problem with most scripts is that they become too complex and too long for a system to handle as expected (that's my experience anyhow!).

 
HI.

You can use my DELTEMP.CMD script as a template and edit it for your own needs.
It works in another way (deleting temp folders, not specific files):

REM DELTEMP.CMD is designed to delete temporary files on a multiuser system,
REM Such as Terminal Server.
REM
REM - WARNING!!!
REM - This batch file DELETES FILES!!!
REM
REM - Please make sure that you know exactly what is going to be done.
REM - It is recommended to test in a lab first.
REM
REM - The idea here is to clean up files from temp folders of all users on a machine.
REM - This can be most useful in a multi user system like Terminal Server.
REM
REM - Remove the following lines to let it work...
PAUSE
GOTO END

IF "%OS%"=="" GOTO WIN9X
@REM - FIND "DOCUMENTS AND SETTINGS" FOLDER:
%SYSTEMDRIVE%
CD %USERPROFILE%\..
FOR /D %%F IN (*.*) DO DEL /F/S/Q "%%F\LOCAL SETTINGS\TEMP\*.*"
FOR /D %%F IN (*.*) DO RD /S/Q "%%F\LOCAL SETTINGS\TEMPORARY INTERNET FILES\*.*"
DEL /F/S/Q %WINDIR%\TEMP\*.*
DEL /F/S/Q %WINDIR%\*.TMP

:GOTO END

:WIN9X
REM - WIN9X systems are currently not supported.

:END




Yizhar Hurwitz
 
If you really want to focus on deleting just one extension of files, such as *.tmp then I would suggest searching for just that instead of certain directories. I would follow the suggestion of justcurious, but with this modification:
(make sure you have win2k resource kit installed, or at least this program installed. The path to the listspace.vbs will vary) You can even set this up to make a loop for every server name you have, or to make it read a file of servernames... is all pretty easy, just need to nest the for loops for that.
__________________________________________

FOR /F &quot;tokens=1 delims=:&quot; %%D IN ('cscript c:\progra~1\resour~1\listspace.vbs /s <servername>') DO (
DEL /S /Q %%D:\*.tmp
ECHO %%D Drive Temp Files Deleted!!)

__________________________________________
give it a go, but try it out on your system with the del commented out first just to make sure it will work for you. hehe. Alternatively, you can sample it with a name of a test file that you set up as a harmless deletable file. When testing deletes, the hardest part is making sure you don't accidently delete the wrong thing. ;-).
Good luck, and post up your results!
 
I found this script at Microsoft TechNet that produces a listing of local partitions and their associated drive letters:

ComputerName = &quot;.&quot;
Set wmiServices = GetObject _
(&quot;winmgmts:{impersonationLevel=Impersonate}!//&quot; & ComputerName)
Set wmiDiskDrives = wmiServices.ExecQuery _
(&quot;SELECT Caption, DeviceID FROM Win32_DiskDrive&quot;)
For Each wmiDiskDrive In wmiDiskDrives
WScript.Echo wmiDiskDrive.Caption & &quot; (&quot; & wmiDiskDrive.DeviceID & &quot;)&quot;
strEscapedDeviceID = Replace(wmiDiskDrive.DeviceID, &quot;\&quot;, &quot;\\&quot;, 1, -1, vbTextCompare)
Set wmiDiskPartitions = wmiServices.ExecQuery _
(&quot;ASSOCIATORS OF {Win32_DiskDrive.DeviceID=&quot;&quot;&quot; & _
strEscapedDeviceID & &quot;&quot;&quot;} WHERE AssocClass = Win32_DiskDriveToDiskPartition&quot;)
For Each wmiDiskPartition In wmiDiskPartitions
WScript.Echo vbTab & wmiDiskPartition.DeviceID
Set wmiLogicalDisks = wmiServices.ExecQuery _
(&quot;ASSOCIATORS OF {Win32_DiskPartition.DeviceID=&quot;&quot;&quot; & _
wmiDiskPartition.DeviceID & &quot;&quot;&quot;} WHERE AssocClass = Win32_LogicalDiskToPartition&quot;)
For Each wmiLogicalDisk In wmiLogicalDisks
WScript.Echo vbTab & vbTab & wmiLogicalDisk.DeviceID
Next
Next
Next

A sample of the output (from my local workstation) looks like this:

HITACHI_DK23BA-20B (\\.\PHYSICALDRIVE0)
Disk #0, Partition #0
C:
Disk #0, Partition #1
D:
Disk #0, Partition #2
E:
F:

It's quite handy because it ignores mapped drives, CDs and removable drives.

If you pipe this output to FIND &quot;:&quot; then it will produce a list containing just the drive letters.

You will need to make sure that latest versions of WSH, VBScript and WMI are installed on your systems. You can get more information here:


I saved this script as locldrvs.vbs, then created the following batch file, saving it in the same folder as the vbs file:

cscript locldrvs.vbs | find &quot;:&quot; > locldrvs.txt
del deletemp.log
for /F &quot;eol=; tokens=1 delims=,&quot; %%i in (locldrvs.txt) do del %%i\*.tmp /Q /S >> deletemp.log
del locldrvs.txt

The batch file uses the list created in the first line to delete all *.tmp files on your local drives, creates a log file and cleans up it's input file (locldrvs.txt).

I tested it on NT4, Win2K Pro and Win2K Server. The servers use SCSI drives and the workstations use IDE.
 
justcurious you are awesome, this works awesome!!!!

I can't thank you enough.

Nadya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top