elwayisgod
asked on
Move Directory then delete directory on destination
First I need to search a UNC path \\FW18\shared\Essbase\Back ups or %targetdir% for any directory ending in the variable 'dayofweek' and delete that directory. So if %dayofweek%=fri than any folder ending in fri is deleted. No prompts, etc.. force deleted. Non case sensitive.
Second I need to 'move' a whole folder named %shortname% to a UNC path \\FW18\shared\Essbase\Back ups which is a variable too named 'targetdir'. It needs to move it no matter what. No prompting and overwrite if target directory already exists. %shortname% example is '03262010_Fri'
I prefer windows batch as I'm currently trying to modify and existing .bat file to do this. Having issues with UNC path.. Batch doesn't like it? I can xcopy it ok, just cant figure out how to move etc....
Second I need to 'move' a whole folder named %shortname% to a UNC path \\FW18\shared\Essbase\Back
I prefer windows batch as I'm currently trying to modify and existing .bat file to do this. Having issues with UNC path.. Batch doesn't like it? I can xcopy it ok, just cant figure out how to move etc....
Here is an example script of how to do it in vbscript. How it populates the variables is up to you.
arrFrom = array("C:\03252010_Thu",_
"C:\03262010_Fri")
ToFold = "\\FW18\shared\Essbase\Backups"
Set FSO = CreateObject("Scripting.FileSystemObject")
For Cnt = 0 to ubound(arrFrom)
FSO.CopyFolder arrFrom(Cnt), ToFold
FSO.DeleteFolder arrFrom(Cnt)
Next
ASKER
@echo off
setlocal ENABLEDELAYEDEXPANSION
:: Define constants
set BASEDIR=%Base_Dir%
set BACKUPPATH=%Backup_Path%%
echo/
:: Verify directories exist
if not exist "%BASEDIR%" (
echo ERROR - Base directory does NOT exist
echo = '%BASEDIR%
goto :END
)
if not exist "%BACKUPPATH%" (
md "%BACKUPPATH%" 2>nul
if errorlevel 1 (
echo ERROR - unable to create backup directory
echo = '%BACKUPPATH%'
goto :END
)
)
:: Construct target directory name
set tmpSTRING=%DATE:~4%
for /f "tokens=5" %%d in ('echo/^|date^|find "current date"') do set DAYOFWEEK=%%d
set SHORTNAME=%tmpSTRING:/=%_% DAYOFWEEK%
set TARGETDIRECTORY=%BACKUPPAT H%\%SHORTN AME%
:: Verify target directory does not already exist
if exist "%TARGETDIRECTORY%" (
echo ERROR - target directory already exists
echo = '%TARGETDIRECTORY%'
echo/
echo + deleting '%TARGETDIRECTORY%'
rd /s /q "%TARGETDIRECTORY%" >nul
if not errorlevel 1 (
echo - Done
echo/
) else (
echo # ERROR - failed to delete target directory
goto :END
)
)
echo + Moving 'Daily' directory structure
echo - SOURCE: '%BASEDIR%'
echo - TARGET: '%TARGETDIRECTORY%'
:: Move files
move "%BASEDIR%" "%TARGETDIRECTORY%" 1>nul 2>&1
if errorlevel 1 (
echo ERROR - an error occurred while moving the files
goto :END
)
:: Recreate new 'Daily' directories
md "%BASEDIR%" 2>nul
md "%BASEDIR%\Essbase_Bkup"
md "%BASE_DIR%\App_Logs"
REM md "%BASEDIR%\Planning_Bkup"
REM md "%BASEDIR%\BusRules_Bkup"
REM md "%BASEDIR%\SharedSvcs_Bkup "
REM md "%BASEDIR%\WebAnalysis_Bku p"
if errorlevel 1 (
echo ERROR - an error occurred while creating the new 'Daily' directory
echo = '%BASEDIR%'
goto :END
)
:: 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'
cd %BACKUPPATH%
rd /s /q "%%d"
if errorlevel 1 (
echo ERROR - unable to remove previous backup directory
goto :END
)
)
:: Copy the Essbase_Backup_Log.log file from the TARGETDIRECTORY location to 'Backup\MaxL' :: directory. This is needed as the email script is in VB and cannot recognize the TARGETDIRECTORY :: variable. Thus will copy it to where all the scripts reside and in VB script hard code in
:: the location where it needs to attach the log file. Easier than trying to have the VB script :: find the TARGETDIRECTORY.
:: Then the next VB script will attach it to the email that is sent out.
xcopy %TARGETDIRECTORY%\Essbase_ Backup_Log .log %ess_scripts_bkup%\*.* /Y
REM Now move the 'Daily' backup to the remote server
REM xcopy E:\Backup\Weekly\*%DAYOFWE EK% Y:\ /Y /S /E /i
echo / Complete
goto :END
:: Define functions
:END
setlocal ENABLEDELAYEDEXPANSION
:: Define constants
set BASEDIR=%Base_Dir%
set BACKUPPATH=%Backup_Path%%
echo/
:: Verify directories exist
if not exist "%BASEDIR%" (
echo ERROR - Base directory does NOT exist
echo = '%BASEDIR%
goto :END
)
if not exist "%BACKUPPATH%" (
md "%BACKUPPATH%" 2>nul
if errorlevel 1 (
echo ERROR - unable to create backup directory
echo = '%BACKUPPATH%'
goto :END
)
)
:: Construct target directory name
set tmpSTRING=%DATE:~4%
for /f "tokens=5" %%d in ('echo/^|date^|find "current date"') do set DAYOFWEEK=%%d
set SHORTNAME=%tmpSTRING:/=%_%
set TARGETDIRECTORY=%BACKUPPAT
:: Verify target directory does not already exist
if exist "%TARGETDIRECTORY%" (
echo ERROR - target directory already exists
echo = '%TARGETDIRECTORY%'
echo/
echo + deleting '%TARGETDIRECTORY%'
rd /s /q "%TARGETDIRECTORY%" >nul
if not errorlevel 1 (
echo - Done
echo/
) else (
echo # ERROR - failed to delete target directory
goto :END
)
)
echo + Moving 'Daily' directory structure
echo - SOURCE: '%BASEDIR%'
echo - TARGET: '%TARGETDIRECTORY%'
:: Move files
move "%BASEDIR%" "%TARGETDIRECTORY%" 1>nul 2>&1
if errorlevel 1 (
echo ERROR - an error occurred while moving the files
goto :END
)
:: Recreate new 'Daily' directories
md "%BASEDIR%" 2>nul
md "%BASEDIR%\Essbase_Bkup"
md "%BASE_DIR%\App_Logs"
REM md "%BASEDIR%\Planning_Bkup"
REM md "%BASEDIR%\BusRules_Bkup"
REM md "%BASEDIR%\SharedSvcs_Bkup
REM md "%BASEDIR%\WebAnalysis_Bku
if errorlevel 1 (
echo ERROR - an error occurred while creating the new 'Daily' directory
echo = '%BASEDIR%'
goto :END
)
:: Search for previous same-day directory and remove if found
for /f %%d in ('dir /ad /b "%BACKUPPATH%\*%DAYOFWEEK%
echo/
echo + removing same-day backups from previous weeks:
echo = '%%d'
cd %BACKUPPATH%
rd /s /q "%%d"
if errorlevel 1 (
echo ERROR - unable to remove previous backup directory
goto :END
)
)
:: Copy the Essbase_Backup_Log.log file from the TARGETDIRECTORY location to 'Backup\MaxL' :: directory. This is needed as the email script is in VB and cannot recognize the TARGETDIRECTORY :: variable. Thus will copy it to where all the scripts reside and in VB script hard code in
:: the location where it needs to attach the log file. Easier than trying to have the VB script :: find the TARGETDIRECTORY.
:: Then the next VB script will attach it to the email that is sent out.
xcopy %TARGETDIRECTORY%\Essbase_
REM Now move the 'Daily' backup to the remote server
REM xcopy E:\Backup\Weekly\*%DAYOFWE
echo / Complete
goto :END
:: Define functions
:END
ASKER
sr75,
not sure how to read this....You hardcoded in paths there.. I can't have that.. Not a vbscript person....
not sure how to read this....You hardcoded in paths there.. I can't have that.. Not a vbscript person....
ASKER
I can do a remove directory with RMDIR. However how do I say remove a directory where it ends with 'fri' which is a variable named 'dayofweek'. thats what I can't figure out...
rmdir %targetdir%*%dayofweek% /s /q
That does not work.. Can't figure out how to do it...
rmdir %targetdir%*%dayofweek% /s /q
That does not work.. Can't figure out how to do it...
I hardcoded the paths to show you where they go.
for /f "tokens=*" %%a in ('dir /ad /s "%TargetDir%\*%dayofweek%" ^| findstr /e /i /c:"%dayofweek%"') do (
echo Removing "%%a"
rmdir /s /q "%%a"
)
echo Removing "%%a"
rmdir /s /q "%%a"
)
ASKER
Hi,
I tried it but it's not working. Not sure how to decipher as it looks to me like it's trying to remove a directory I created '03192010_Fri'. However is it trying in the \\FW18\shared\Essbase\Back ups directory? It says it cant find it but not sure where it's looking........
E:\Backup\maxl>for /F "tokens=*" %a in ('dir /ad /s "\\FW18\shared\Essbase\Bac ku
ps\*Fri" | findstr /e /i /c:"Fri"') do (
echo Removing "%a"
rmdir /s /q "%a"
)
E:\Backup\maxl>(
echo Removing "03/26/2010 03:29 PM <DIR> 03192010_Fri"
rmdir /s /q "03/26/2010 03:29 PM <DIR> 03192010_Fri"
)
Removing "03/26/2010 03:29 PM <DIR> 03192010_Fri"
The system cannot find the path specified.
E:\Backup\maxl>
I tried it but it's not working. Not sure how to decipher as it looks to me like it's trying to remove a directory I created '03192010_Fri'. However is it trying in the \\FW18\shared\Essbase\Back
E:\Backup\maxl>for /F "tokens=*" %a in ('dir /ad /s "\\FW18\shared\Essbase\Bac
ps\*Fri" | findstr /e /i /c:"Fri"') do (
echo Removing "%a"
rmdir /s /q "%a"
)
E:\Backup\maxl>(
echo Removing "03/26/2010 03:29 PM <DIR> 03192010_Fri"
rmdir /s /q "03/26/2010 03:29 PM <DIR> 03192010_Fri"
)
Removing "03/26/2010 03:29 PM <DIR> 03192010_Fri"
The system cannot find the path specified.
E:\Backup\maxl>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just about everything else you want to do appears to be basic move, del., copy, etc.