Sometimes 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.
MODE CON: COLS=100 LINES=10
title ~: Auto TempFile Cleanup By TechnoChat.IN :~
::Set Destination and source drive
:: get the date and time and then into single variable
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
::Get the old Backup path from registry.
FOR /F "TOKENS=3*" %%i in ('reg query "HKEY_CURRENT_USERSOFTWAREMicrosoft" /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
::Adding the New backup path into registry
REG ADD "HKEY_CURRENT_USERSoftwareMicrosoft" /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
Disclaimer: All posts and opinions on this site are provided AS IS with no warranties. These are our own personal opinions and do not represent our employer’s view in any way.
This article currently have 4,868 views
I am an IT Professional with 12+ years of experience in Windows, Storage, Backup, AWS and Azure. I love writing scripts using PowerShell. I loved to share my experience with rest of the world via this blog. I love my Echo Dot (3G). I love playing PUBG on my mobile.
This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License.