Remove Directory via Batch

elwayisgod
elwayisgod used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Why don't you try to map the drive before the use of the script?
You can accomplish this by executing
net use <drive Letter>: \\path\to\your\network\folder <password> /user:<domain>\username

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!
Steven CarnahanAssistant Vice President\Network Manager

Commented:
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>
IT / Software Engineering Consultant
Top Expert 2016
Commented:
Try this change.  Since BACKUPDIRECTORY is now a network location the CD won't work.  I'd rather see you do it with the fully qualified path as below anyway, rather than the CD approach.

:: Search for previous same-day directory and remove if found
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*%DAYOFWEEK%" ^| find /v /i "%SHORTNAME%"') do (
      echo/
      echo   + removing same-day backups from previous weeks:
      echo    = '%%d'
      rd /s /q "%BACKUPPATH%\%%d"
            if errorlevel 1 (
            echo ERROR - unable to remove previous backup directory
            goto :END
      )
)

~bp
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Commented:
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

Author

Commented:
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_BKUPS\MaxL

Where I need directories deleted:  \\Essbase_Backup\TEST_BKUPS\TEST_Weekly

Is there a way to have it remove dir in different dir where script is run from?

Author

Commented:
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?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
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

Author

Commented:
Perfect.  Thanks....
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Glad to help, thank you for the grade.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial