Archive for the 'Batch Files' Category

Delete files older than X days

This batch is a direct modification to the ‘copyout.bat’ file I posted here. This file scans a location for files older than a number of specified days and deletes them. The file is also setup to use blat.exe (download from sourceforge) to send email reports.

NOTE: This must not be used on a computer with a ‘forfiles’ version created before 2000 as the syntax has since changed.

loglocation – the location you want your log to be recorded at
olderthan – number of days old the files you want to delete have to be
source – the location the files will start at (dont end this field in a \)
email – the address that will be notified when the operation sends an email report
blat – the path to the blat.exe file (this, and all the others, can be a UNC path). Must be downloaded from souce forge most of the time.
extension – The file extension(s) to copy then delete, just use * for all files.
relayserver – Target mail server with relay enabled. (required)

Use at your own risk, no warranties and stuff.

deleteold.bat


@echo off

set loglocation=C:\Deleteold.txt
set olderthan=31
set source=F:
set email=blog@integrii.net
set blat=c:\blat.exe
set extension=bkf
set relayserver=localhost

color 1f
if exist %loglocation% del %loglocation%

echo blog@integrii.net - deleteold.bat - v1.3
echo Deleting files older than %olderthan% days with the file extension *.%extension% located at %source%.
echo Deleting files older than %olderthan% days with the file extension *.%extension% located at %source%. >> %loglocation%

cd %source%

echo List of files to be removed: >> %loglocation%
echo List of files to be removed:

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE"
FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE" >> %loglocation%

echo Starting delete...
echo Starting delete... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C del %source%\@FILE /q & echo %source%\@FILE deleted." >> %loglocation%

echo Log file located at %loglocation%

echo Delete finished... Sending Report.
echo Delete finished... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

set server=%computername%
%blat% %loglocation% -t %email% -s "%server% Deletion Log" -server %relayserver% -f do-not-reply@%server%.delete.log

echo Report sent!

exit

Copy then delete files older than X days [Updated 8/7/07]

This script simply uses the forfiles command along with xcopy to copy ntbackup files from a source location to a destination location. The script then checks if the file exists in the destionation location, and if it does, deletes the source file. A report is then sent using blat.exe to the specified account in the email variable. The script was written to be VERY verbose for logging purposes, there is only an actual few lines of action taking place. I currently use this to move backups from one location to another to free up space automatically. I am sure it has LOTS more uses.

NOTE: This must not be used on a computer with a ‘forfiles’ version created before 2000 as the syntax has since changed.
If you only want to delete files, check out deleteold.bat.

All you need to do is change the variables at the top of the batch file. Heres what they should be, in case my names aren’t totally self-explanitory.

loglocation – the location you want your logs to be recorded at
olderthan – number of days old the files you want to target have to be
source – the location the files will start at (this field must not end with a \)
destination – the location you want to move the files to before deleting
email – the address that will be notified when the operation sends an email report
blat – the path to the blat.exe file (this, and all the others, can be a UNC path). Must be downloaded from souce forge most of the time.
extension – The file extension(s) to copy then delete, just use * for all files.
relayserver – Target mail server with relay enabled. (required)
subject – The subject of the report email that will be sent.

This is a very multi-purpose file and can be used for all kinds of stuff! Enjoy!

copyout.bat


@echo off

set loglocation=C:\logs\Copyout.txt
set olderthan=10
set source=H:\backups
set destination=K:\
set email=blog@integrii.net
set blat=c:\blat.exe
set extension=*
set relayserver=localhost
set subject="Copyout.bat Report"

if exist %loglocation% del %loglocation%

echo ericgreer@gmail.com - copyout.bat - v1.78
echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%.
echo Copying and deleting files older than %olderthan% days with the file extension *.%extension%. >> %loglocation%
echo Copying from %source% to %destination% and deleting from %source%.
echo Copying from %source% to %destination% and deleting from %source%. >> %loglocation%
echo This file may take an extremely long time to run while it looks unresponsive.
echo Check %loglocation% for copy progress.

cd %source%

echo List of files to be copied and removed: >> %loglocation%
echo List of files to be copied and removed:

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE"
FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C Echo "@FILE" >> %loglocation%

echo Starting copy...
echo Starting copy... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C XCOPY %source%\@FILE %destination%"

echo Starting delete...
echo Starting delete... >> %loglocation%
date /t >> %loglocation%
time /t >> %loglocation%

FORFILES -p %source% /d -%olderthan% -m *.%extension% -c "CMD /C if exist %destination%\@FILE del %source%\@FILE /Q & echo @FILE Deleted." >> %loglocation%

echo Log file located at %loglocation%

echo Copy and Delete finished... Sending Report.
echo Copy and Delete finished... >> %loglocation%

echo Source Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %source% >> %loglocation%

echo. >> %loglocation%
echo. >> %loglocation%
echo. >> %loglocation%

echo Destination Directory Contents: >> %loglocation%
echo. >> %loglocation%
dir %destination% >> %loglocation%

date /t >> %loglocation%
time /t >> %loglocation%

set server=%computername%
%blat% %loglocation% -t %email% -s "%server% %subject%" -server %relayserver% -f do-not-reply@%server%.copyout.bat

echo Report sent!

Error ID command line lookup (EventID.net)

This simple little script just looks up an error code on www.EventID.net when you supply an event. I find this prety useful when I am digging into server logs.

Sample:
id 2000
(looks up errorid 2000 on eventid.net)

id.bat


@echo off
color 1f
start http://www.eventid.net/display.asp?eventid=%1

PsExec Batch Shortcut

This batch file simply saves the trouble of worrying about getting all the context right for psexec and makes it easier to run things on multiple computers with varying usernames.  Help is built into the file as you can see below, so just run the batch without any paramiters for a listing.

runremote.bat

@echo off
color 1f

if "%1" == "" goto help

:runit
psexec @serverlist.txt -c -s -f -n 10 -u %1 %2
goto end

:help
echo ---
echo blog@integrii.net
echo PsExec Batch File Shortcut
echo http://blog.integrii.net
echo ---
echo PURPOSE:
echo Runs a file or command on a list of remote computers with a specified username.
echo ---
echo USEAGE:
echo runstuff [remote username] [command or file]
echo ---
echo NOTE:
echo You MUST CHANGE the serverlist.txt to contain a list of target computers. One computer per line, hostname or IP address.
echo ---
pause

:end


 View more about PsExec here:
http://www.microsoft.com/technet/sysinternals/Security/PsExec.mspx