elwayisgod
asked on
Remove Directory via Batch
Hi,
I have a .bat file that needs to remove the previous weeks folder if it exists. Basically I'm storing backups in a directory and each day the backups run it creates a folder in the format:
10112010_Mon
Meaning Oct 11, 2010 and Monday. So the next week it goes and creates a folder:
10182010_Mon
It needs to remove the previous weeks '10112010_Mon' folder. However when we switched all the variables to UNC paths instead of drive letters, it's now not working. Had to switch to UNC paths as the scripts are actually on a mapped drive not a drive on the actual server. Thus they wouldn't run. Attached is my 'setenv.cmd' file which sets all my variables. Also attached is the 'Rolling7Day.bat'' file which needs to perform the desired function. I tried running this script and logged it to a log file 'log.log' to see if it gave me clues but didn't really give me anything. So basically I need to know how to fix the 'Rolling7Day.bat' file to remove the previous weeks directory. It must make use of existing variables and not be hard coded in any way. setenv.cmd log.log Rolling7Day.txt
I have a .bat file that needs to remove the previous weeks folder if it exists. Basically I'm storing backups in a directory and each day the backups run it creates a folder in the format:
10112010_Mon
Meaning Oct 11, 2010 and Monday. So the next week it goes and creates a folder:
10182010_Mon
It needs to remove the previous weeks '10112010_Mon' folder. However when we switched all the variables to UNC paths instead of drive letters, it's now not working. Had to switch to UNC paths as the scripts are actually on a mapped drive not a drive on the actual server. Thus they wouldn't run. Attached is my 'setenv.cmd' file which sets all my variables. Also attached is the 'Rolling7Day.bat'' file which needs to perform the desired function. I tried running this script and logged it to a log file 'log.log' to see if it gave me clues but didn't really give me anything. So basically I need to know how to fix the 'Rolling7Day.bat' file to remove the previous weeks directory. It must make use of existing variables and not be hard coded in any way. setenv.cmd log.log Rolling7Day.txt
Try commenting out the @echo off and send the output to the log again. That may give you more of a clue. Another thing to try is to put a pause after " cd %BACKUPPATH%" to see what directory you actually end up in since UNC paths are not supported with CMD:
C:\Program Files\Windows Resource Kits\Tools>cd \\iib-apps\home
'\\iib-apps\home'
CMD does not support UNC paths as current directories.
C:\Program Files\Windows Resource Kits\Tools>
C:\Program Files\Windows Resource Kits\Tools>cd \\iib-apps\home
'\\iib-apps\home'
CMD does not support UNC paths as current directories.
C:\Program Files\Windows Resource Kits\Tools>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Seems like a pretty easy thing to accomplish. As long as you want to keep only a single directory and it always has Mon in it, something like this should suffice:
for /f %%i in (' dir /s/b /ad ^|findstr Mon ') do rd /q /s %%i
If the new dir and old dir exist simultaneously just exclude the new one:
for /f %%i in (' dir /s/b /ad ^|findstr Mon^|findstr /v %SHORTNAME% ') do rd /q /s %%i
PS you can just use date /t instead of echo/^|date
for /f %%i in (' dir /s/b /ad ^|findstr Mon ') do rd /q /s %%i
If the new dir and old dir exist simultaneously just exclude the new one:
for /f %%i in (' dir /s/b /ad ^|findstr Mon^|findstr /v %SHORTNAME% ') do rd /q /s %%i
PS you can just use date /t instead of echo/^|date
ASKER
xylog,
I really like this idea as I can just remove any directories that contain 'Mon' , 'Tue' etc... which is a variable. However my scripts are stored in a different directory where the script is run from.
Script is run from: \\Essbase_Backup\TEST_BKUP S\MaxL
Where I need directories deleted: \\Essbase_Backup\TEST_BKUP S\TEST_Wee kly
Is there a way to have it remove dir in different dir where script is run from?
I really like this idea as I can just remove any directories that contain 'Mon' , 'Tue' etc... which is a variable. However my scripts are stored in a different directory where the script is run from.
Script is run from: \\Essbase_Backup\TEST_BKUP
Where I need directories deleted: \\Essbase_Backup\TEST_BKUP
Is there a way to have it remove dir in different dir where script is run from?
ASKER
billp,
I'm testing your solution and I created a bunch of directories with 'wed' in then names. The only ones it removed were ones that ended in 'wed'. So a directory I named 'test_wed_test' was not deleted. Is that how it's supposed to work?
I'm testing your solution and I created a bunch of directories with 'wed' in then names. The only ones it removed were ones that ended in 'wed'. So a directory I named 'test_wed_test' was not deleted. Is that how it's supposed to work?
I built off of your original sample script, which had the following line in it:
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*%DAYOFWEEK% " ^| find /v /i "%SHORTNAME%"') do (
in that statement you are doing a DIR for *wed for example, so that is why it is only showing the dirs ending in wed. My feedback was to address your specific problem in the question, after the UNC switch. I saw that you were using CD which would not have worked now, so proposed a change in that area of the script. If your original DIR command was wrong then yes, it won't find dirs that don't end in the weekday.
You could change it to something like one of these depending on exactly the format of the folder names you want:
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*%DAYOFWEEK% *" ^| find /v /i "%SHORTNAME%"') do (
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*_%DAYOFWEEK %_*" ^| find /v /i "%SHORTNAME%"') do (
~bp
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*%DAYOFWEEK%
in that statement you are doing a DIR for *wed for example, so that is why it is only showing the dirs ending in wed. My feedback was to address your specific problem in the question, after the UNC switch. I saw that you were using CD which would not have worked now, so proposed a change in that area of the script. If your original DIR command was wrong then yes, it won't find dirs that don't end in the weekday.
You could change it to something like one of these depending on exactly the format of the folder names you want:
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*%DAYOFWEEK%
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*_%DAYOFWEEK
~bp
ASKER
Perfect. Thanks....
Glad to help, thank you for the grade.
~bp
~bp
You can accomplish this by executing
net use <drive Letter>: \\path\to\your\network\fol
So, if you wanted to map folder Home on server MyServer with username Me on a domain Work with a password Pass to a drive letter z, you'll have the following command:
net use z: \\MyServer\Home Pass /user:Work\Me
After your script completes, simply execute
net use /delete \\MyServer\Home and your mapped folder will be removed!
Hope this helps!