Solved

Robocopy script to copy Outlook 2007 PST Files with progress Bar

Posted on 2014-01-27
5
1,812 Views
Last Modified: 2014-01-30
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"
0
Comment
Question by:cybersi
  • 3
  • 2
5 Comments
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
0
 

Author Comment

by:cybersi
Comment Utility
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"
0
 

Author Comment

by:cybersi
Comment Utility
think I can REM out (call :main_body "Backup 2" ) too.
0
 

Author Comment

by:cybersi
Comment Utility
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
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
Comment Utility
Looks like it, thought so much waffle there for all the percent bar gizmo difficult to see!

Agred you don't need the extra CALL

and yes re: the archve*.pst.  You just need robocopy sourcedir destdir filespec so filespec can be a*.pst *.pst archive*.* *.* etc.

From the original comment about shutting down too, you could instead of doing the Outlook command at the end use:

shutdown -s -t 30

which will start a shutdown in 30 secs.

Steve
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now