Link to home
Start Free TrialLog in
Avatar of elwayisgod
elwayisgodFlag for United States of America

asked on

Move Directory then delete directory on destination

First I need to search a UNC path \\FW18\shared\Essbase\Backups 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\Backups 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....
Avatar of thompsonwireless
thompsonwireless
Flag of United States of America image

I would be interested in seeing your batch file.  You can't use a UNC path as a directory name unless it is a mapped drive as far as I know.  I do know I can't CD to \\whatever\

Just about everything else you want to do appears to be basic move, del., copy, 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

Open in new window

Avatar of elwayisgod

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=%BACKUPPATH%\%SHORTNAME%

:: 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_Bkup"



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\*%DAYOFWEEK% Y:\ /Y /S /E /i

echo / Complete

goto :END

:: Define functions
:END
sr75,

not sure how to read this....You hardcoded in paths there.. I can't have that.. Not a vbscript person....
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...
I hardcoded the paths to show you where they go.
Avatar of AmazingTech
AmazingTech

for /f "tokens=*" %%a in ('dir /ad /s "%TargetDir%\*%dayofweek%" ^| findstr /e /i /c:"%dayofweek%"') do (
    echo Removing "%%a"
    rmdir /s /q "%%a"
)
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\Backups 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\Backu
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
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial