Link to home
Start Free TrialLog in
Avatar of Simon Allaway
Simon AllawayFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Robocopy script to copy Outlook 2007 PST Files with progress Bar

Hi All

I found a script on the web that copies files using Robocopy and a progress bar but I cant get it to copy the files and I cant see what I'm doing wrong.  The script starts off with closing Outlook.exe then copying two pst files from the users pc to a redirected My doc's directory.  If possible I would like 2 endings one to reopen outlook after the copy and anther ending to shutdown the PC when completed.

Here is the script:

@echo off
echo Microsoft Outlook is shutting down please wait
echo.
echo.
echo Please wait while Microsoft Outlook backs up your Archive Folders to the network
taskkill /IM outlook.exe /t
REM You need to chnage the path to the psf files below




set "rc=robocopy.exe"

set "source=C:\Users\sbalut\AppData\Local\Microsoft\Outlook\archive.pst"
set "target=h:\outlookBK\archive.pst"

call :main_body "Backup 1"

set "source=C:\Users\sbalut\AppData\Local\Microsoft\Outlook\archive2013.pst"
set "target=h:\outlookBK\archive2013.pst"

call :main_body "Backup 2"

rem set "source=e:\test\source3"
rem set "target=e:\test\target3"

rem call :main_body "Backup 3"





pause

goto :EOF

:main_body

set "rc.command=%rc% "%source%." "%target%." /s"

REM make the bar as wide as possible
for /f "usebackq tokens=2" %%a in (`mode con ^| find "Columns:"`) do set /a bar.width=%%a - 3

REM or you can force the width on the next line (no sanity checking is done on this value)
REM set "bar.width=40"
set "bar.char=þ"
set "bar.backchar=ú"
set "bar.size=0"
set "bar.back="
set "bar.del="
set "bar.position=0"
set "bar.position.modifier=0"
set "bar.check=0"
set "loop.delay=3"

set "done=0"
set "total=-1"
set "abort="

set "window.title=%rc%_%date%_%time: =0% %~1"

echo:START %~1

REM spawn the robocopy command with a (hopefully) unique window title that we'll need later
start /min "%window.title%" %rc.command%

REM find the total number of files, so we can shrink the bar to fit the total, if necessary
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "total=%%a,done=%%c"
)

set /a "bar.check=bar.width / total" 2>nul

if %bar.check% EQU 1 set /a bar.width=total

REM draw the empty bar
for /l %%a in (1,1,%bar.width%) do (call set "bar.back=%%bar.back%%%bar.backchar%")
call set "bar.del=%%bar.back:%bar.backchar%=%%"
set /p "out=[%bar.back%]%bar.del%" <nul

set /a loop.delay+=1

title Please stand by...

call :loop

echo:

if defined abort (echo:ABORT %~1) ELSE (echo:END %~1)

REM pause

goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

set bar=

REM if all the files have been copied, draw a full bar (in case it didn't get filled
REM on the previous iteration) and exit
if %done% GEQ %total% (
    title %done% / %total%
    for /l %%a in (1,1,%bar.width%) do call set "bar=%bar.char%%%bar%%"
    call set /p "out=%%bar%%" <nul
    set abort=
    goto :EOF
)

REM if the robocopy child process wasn't running on the previous iteration and there
REM are still files left uncopied then we assume that robocopy died unexpectedly
if defined abort goto :EOF

REM check for the robocopy child process (using out "unique" window title)
tasklist /fi "imagename eq robocopy.exe" /fi "windowtitle eq %window.title%" /fo csv /nh 2>nul | findstr "." >nul

REM if it's not found, set a flag (it'll be dealt with on the next interation)
if errorlevel 1 set abort=1 & set loop.delay=1

REM run a duplicate robocopy process with the "/L" switch to so we can extract
REM the total number of files and those that have been copied from the output
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "remain=%%b,done=%%c"
)

REM figure out (roughly) how many files need to be copied to increase the progress
REM bar by one step
set /a bar.step=(total / bar.width) - 1

REM in case its less than one...
if %bar.step% LEQ 0 set bar.step=1

REM calculate the bar modifier, which takes effect if the total number of files
REM is significantly lower than the bar width.
set /a bar.position.modifier=bar.width / total
if %bar.position.modifier% LEQ 0 set /a bar.position.modifier=1

REM calculate the position using the number of copied files and the step value
set /a bar.position=(done / bar.step) * bar.position.modifier

REM if for some reason the position is greater than the width, fix it
REM (this would occur if the number of files is not much more than
REM the defined bar width)
if %bar.position% GTR %bar.width% set /a bar.position=bar.width

REM draw the bar (we're redrawing the whole thing on each interation)
for /l %%a in (1,1,%bar.position%) do call set "bar=%bar.char%%%bar%%"

set /p "out=%bar%" <nul

title %done% / %total%

REM delay before interating so that the script doesn't thrash the system
ping 127.1 -n %loop.delay% -w 1000 >nul

goto :loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::
echo Backup Complete.......Starting Microsoft Outlook
"C:\Program Files (x86)\Microsoft Office\Office14\outlook.exe"
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Wow that's over complicated!  You do know that Robocopy will show a %age of how much it needs to copy anyway if you just do something like:

robocopy "C:\Users\sbalut\AppData\Local\Microsoft\Outlook" "h:\outlookBK" archive*.pst

At a quick look, robocopy needs folders, not filenames so if those are the only two PST files in that dir then you need to use:

set "source=C:\Users\sbalut\AppData\Local\Microsoft\Outlook"
set "target=h:\outlookBK"

and don't use it twice, just once, also remove the /s off the robocopy line if you don't want subdirs:

set "rc.command=%rc% "%source%." "%target%.""

If  you only want those specific two files then:

set "source=C:\Users\sbalut\AppData\Local\Microsoft\Outlook"
set "target=h:\outlookBK"

set "rc.command=%rc% "%source%." "%target%." archive*.pst"

I haven't tried the script there as it stands so there may be other issues but that should be a starter.

Steve
Avatar of Simon Allaway

ASKER

Hi Steve

Thanks so much for your response above. So is the below correct?

@echo off
echo Microsoft Outlook is shutting down please wait
echo.
echo.
echo Please wait while Microsoft Outlook backs up your Archive Folders to the network
taskkill /IM outlook.exe /t
REM You need to chnage the path to the psf files below




set "rc=robocopy.exe"

set "source=C:\Users\sbalut\AppData\Local\Microsoft\Outlook"
set "target=h:\outlookBK"

call :main_body "Backup 1"
rem set "source=C:\Users\sbalut\AppData\Local\Microsoft\Outlook\archive2013.pst"
rem set "target=h:\outlookBK\archive2013.pst"

call :main_body "Backup 2"

rem set "source=e:\test\source3"
rem set "target=e:\test\target3"

rem call :main_body "Backup 3"

pause

goto :EOF

:main_body

set "rc.command=%rc% "%source%." "%target%." archive*.pst"



REM make the bar as wide as possible
for /f "usebackq tokens=2" %%a in (`mode con ^| find "Columns:"`) do set /a bar.width=%%a - 3

REM or you can force the width on the next line (no sanity checking is done on this value)
REM set "bar.width=40"
set "bar.char=þ"
set "bar.backchar=ú"
set "bar.size=0"
set "bar.back="
set "bar.del="
set "bar.position=0"
set "bar.position.modifier=0"
set "bar.check=0"
set "loop.delay=3"

set "done=0"
set "total=-1"
set "abort="

set "window.title=%rc%_%date%_%time: =0% %~1"

echo:START %~1

REM spawn the robocopy command with a (hopefully) unique window title that we'll need later
start /min "%window.title%" %rc.command%

REM find the total number of files, so we can shrink the bar to fit the total, if necessary
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "total=%%a,done=%%c"
)

set /a "bar.check=bar.width / total" 2>nul

if %bar.check% EQU 1 set /a bar.width=total

REM draw the empty bar
for /l %%a in (1,1,%bar.width%) do (call set "bar.back=%%bar.back%%%bar.backchar%")
call set "bar.del=%%bar.back:%bar.backchar%=%%"
set /p "out=[%bar.back%]%bar.del%" <nul

set /a loop.delay+=1

title Please stand by...

call :loop

echo:

if defined abort (echo:ABORT %~1) ELSE (echo:END %~1)

REM pause

goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::

set bar=

REM if all the files have been copied, draw a full bar (in case it didn't get filled
REM on the previous iteration) and exit
if %done% GEQ %total% (
    title %done% / %total%
    for /l %%a in (1,1,%bar.width%) do call set "bar=%bar.char%%%bar%%"
    call set /p "out=%%bar%%" <nul
    set abort=
    goto :EOF
)

REM if the robocopy child process wasn't running on the previous iteration and there
REM are still files left uncopied then we assume that robocopy died unexpectedly
if defined abort goto :EOF

REM check for the robocopy child process (using out "unique" window title)
tasklist /fi "imagename eq robocopy.exe" /fi "windowtitle eq %window.title%" /fo csv /nh 2>nul | findstr "." >nul

REM if it's not found, set a flag (it'll be dealt with on the next interation)
if errorlevel 1 set abort=1 & set loop.delay=1

REM run a duplicate robocopy process with the "/L" switch to so we can extract
REM the total number of files and those that have been copied from the output
for /f "usebackq tokens=3,4,5 delims= " %%a in (`%rc.command% /l /njh /nfl /ndl ^| find "Files"`) do (
    set /a "remain=%%b,done=%%c"
)

REM figure out (roughly) how many files need to be copied to increase the progress
REM bar by one step
set /a bar.step=(total / bar.width) - 1

REM in case its less than one...
if %bar.step% LEQ 0 set bar.step=1

REM calculate the bar modifier, which takes effect if the total number of files
REM is significantly lower than the bar width.
set /a bar.position.modifier=bar.width / total
if %bar.position.modifier% LEQ 0 set /a bar.position.modifier=1

REM calculate the position using the number of copied files and the step value
set /a bar.position=(done / bar.step) * bar.position.modifier

REM if for some reason the position is greater than the width, fix it
REM (this would occur if the number of files is not much more than
REM the defined bar width)
if %bar.position% GTR %bar.width% set /a bar.position=bar.width

REM draw the bar (we're redrawing the whole thing on each interation)
for /l %%a in (1,1,%bar.position%) do call set "bar=%bar.char%%%bar%%"

set /p "out=%bar%" <nul

title %done% / %total%

REM delay before interating so that the script doesn't thrash the system
ping 127.1 -n %loop.delay% -w 1000 >nul

goto :loop
:::::::::::::::::::::::::::::::::::::::::::::::::::::
echo Backup Complete.......Starting Microsoft Outlook
"C:\Program Files (x86)\Microsoft Office\Office14\outlook.exe"
think I can REM out (call :main_body "Backup 2" ) too.
Sorry steve just one more thing am I right in saying I could use

set "rc.command=%rc% "%source%." "%target%." *.pst"

instead of

set "rc.command=%rc% "%source%." "%target%." archive*.pst"
Regards

Simon
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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