Shared TempDrive Cleanup Script – Automated
TweetSometimes it's not possible to remember a specific weekly job to perform. So if we can make it as scheduled job to do the same, it will be better. There will be not such risk. Just take my example, I have to clean our office common shared temporary folder on every Sunday, but I am not in office on Sunday. So, I have two options to do the same, first, I have to call our DCO Support and instruct them to get the job done, and if I go with this option there will be lots of dependencies, or else I have to create a script which will do the job automatically.
So I decided to go for second option. With second option I have to make sure following things should be perform.
- I just can't delete all files & folder from temp drive, coz there might came some requirement to recover files from last week temp drive(Yes, everyone aware of the temp drive policy). So, I have to copy all the content of the Temp Drive to a different location.
- I have to create a folder with date stamp, eg. Folder_date (tempbkp_23-09-2009). So, I have to create a specific date variable.
- After copy all content of the temp drive to other location, I have to give Everyone – Read only permission to that folder and then have to share that folder as well.
- Delete all files and folder from temp drive. Yea, as per my knowledge, there is no such command available on windows by which I can delete all files and folder from a specific folder or a specific map drive. (So…..time to think some alternatives)
- Now the critical part, on next scheduled date, this script will automatically delete last backup folder, and create a new folder for latest backup.
And finally I made it with considering all above requirement without using any third part toolsJ. And as of now it's working properly, check out the script bellow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | @echo off @echo off MODE CON: COLS=100 LINES=10 color 0c title ~: Auto TempFile Cleanup By TechnoChat.IN :~ ::Set Destination and source drive set localdrive=D:\ set tempdrive=Z:\ :: get the date and time and then into single variable for /F "tokens=1-4 delims=/ " %%i in ('date /t') do ( set MM=%%j set DD=%%k set YYYY=%%l set date=%%k-%%j-%%l set dirdate=%%j%%k%%l ) set fullpath=%localdrive%TempBKP_%date% set sname=TempBKP ::Get the old Backup path from registry. FOR /F "TOKENS=3*" %%i in ('reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft" /v Path ^| FINDSTR "REG_SZ" ') do set outpath="%%i%%j" ::Removing the old backup share if exist %outpath% net share %sname% /delete ::Removing old backup folder if exist %outpath% rmdir %outpath% /S /Q ::Creatiing the the New backup folder mkdir %fullpath% ::Adding the New backup path into registry REG ADD "HKEY_CURRENT_USER\Software\Microsoft" /v Path /t REG_SZ /d %fullpath% /f >nul echo Copying files from Source %tempdrive% to Destination [%fullpath%] ::Copying the files from TEMP Drive to Local Drive location xcopy /e /h /r /y "%tempdrive%" "%fullpath%" ::Setting the permission to readonly for hole folder cacls %fullpath% /e /t /g Everyone:r ::Sharing the folder net share %sname%=%fullpath% /remark:"TempDrive Backup Share" ::Removing the temp drive contents for /f "delims=" %%a in ('dir /b /ad "%tempdrive%" 2^>NUL') do rmdir /S /Q "%tempdrive%\%%a" del %tempdrive%*.* /S /Q /F |

The Shared TempDrive Cleanup Script – Automated by Techno Chat|Tech Blog!!, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Enjoy this article?
Quote of the Day
Every morning get up & look at the list of richest people in the world.
— BillGates
If your name is not there PLEASE GO TO WORK
Calendar
Polls
Categories
Archives
- January 2012
- November 2011
- July 2011
- May 2011
- March 2011
- January 2011
- November 2010
- August 2010
- May 2010
- April 2010
- December 2009
- November 2009
- September 2009
- August 2009
- July 2009
- May 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
Recent Posts
- USB/CDRom/Floppy/ZIP Drive Blocker (MBlock v1.0)
- Nokia Music & Mix Radio – Working in India–Experimental on Nokia Lumia 800
- Nokia Lumia 800 Review – Amazing Everyday
- How to stop extracting/viewing users details from AD
- WINDOWS 2008 R2 FAILOVER CLUSTER on VMWARE

