Jump to content

Email System Backup using FOR, Reg Query, FindStr, Robocopy-Need help!


Recommended Posts

Posted

I am trying to write a batch file to backup users emails. We have a mix of Express and Outlook 2007 users. Most of which have changed their data store folders.

I have written a batch file that checks if Outlook 2007 and Express are installed, searches the registry for the location of their personal folders and robocopies them to a folder on their home drive (H:\).

Robocopy is set to a variable at the top of the file to account for some machines not having it installed.

set RoboCopy=\\beaufort.local\NETLOGON\robocopy.exe

I have never used the FOR F/ loop, or REG QUERY or FINDSTR so I have been walking blind for the past few days.

The first question I have is;

With Outlook 2007, there are 2 registry entries for the File Store location, When using the FOR /F loop, how can I assign each key it finds to a different variable.

I have this so far;

FOR /F "TOKENS=1* DELIMS=" %%G IN ('REG.exe QUERY "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Catalog" ^|findstr /v /C:"HKEY"') DO (
%RoboCopy% "%%G" "h:\%username%'s Outlook Backup" "*.pst" /E /ZB /R:5 /W:1 /PURGE)

This does nothing at the moment.

Also, when I call the %%G string it seems to add the location of the batch file before the string, this is from the Source : section of the robocopy it runs

Source : C:\Documents and Settings\Administrator.BEAUFORT\Desktop\    C:\Outlook\backup.pst\

There are also 3 spaces before the path in the registry file, is there a way of removing this?

 

I hope this makes some sense...

 

 

Here is a copy of the entire script, I have added in loads of pauses to see where it fails;


TITLE Backup %username%'s Outlook Files
cls
COLOR 1F

set RoboCopy=\\beaufort.local\NETLOGON\robocopy.exe
md "H:\%username%'s Outlook Backup"

TITLE Checking for Outlook 2007
pause
IF EXIST "C:\Documents and Settings\%USERNAME%.Beaufort\Local Settings\Application Data\Microsoft\Outlook\*.*" (echo This user is using Office Outlook, backing up Outlook folder
pause
GOTO OUTLOOK)
echo User not using Outlook 2007
IF ERRORLEVEL=1 (GOTO ERROR)
pause
cls
GOTO EXPRESS

:OUTLOOK
TITLE  Backing up %username%'s Outlook 2007 Files
ECHO Backing up files, This may take some time
FOR /F "TOKENS=1* DELIMS=    " %%G IN ('REG.exe QUERY "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Catalog" ^|findstr /v /C:"HKEY"') DO (
%RoboCopy% "%%G" "h:\%username%'s Outlook Backup" "*.pst" /E /ZB /R:5 /W:1 /PURGE)
pause
GOTO EXPRESS

:EXPRESS
TITLE Checking for Outlook Express
IF EXIST "C:\Documents and Settings\%USERNAME%\Application Data\Identities\" (echo This user is using Outlook Express, backing up Outlook Express folder
pause
IF ERRORLEVEL=1 (GOTO ERROR)
GOTO EXPRESSBACKUP)
echo User not using Outlook Express
pause
IF ERRORLEVEL=1 (GOTO ERROR)
cls
GOTO FINISH

:EXPRESSBACKUP
TITLE Backing up %username%'s Outlook Express Files
md "H:\%username%'s Outlook Backup\Outlook Express"
md "H:\%username%'s Outlook Backup\Outlook Express Addressbook"
for /f "tokens=5" %%i in ('reg.exe query HKCU\Identities\ ^|findstr /C:"Default User ID"') do SET ID=%%i
for /f "tokens=4" %%g in ('reg.exe query "HKEY_CURRENT_USER\Identities\%ID%\Software\Microsoft\Outlook Express\5.0\" ^|findstr /C:"    Store Root"') do (
%RoboCopy% "%%g" "h:\%username%'s Outlook Backup\Outlook Express" "*.dbx" /E /ZB /R:5 /W:1 /PURGE)
pause
%RoboCopy% "C:\Documents and Settings\%USERNAME%\Application Data\Microsoft\Address Book" "h:\%username%'s Outlook Backup\Outlook Express Addressbook" /E /ZB /R:5 /W:1 /PURGE
pause
cls
IF ERRORLEVEL=1 (GOTO ERROR)
GOTO FINISH

:FINISH
COLOR 2F
CLS
TITLE Backed up %username%'s Outlook Files
echo %USERNAME%'s Outlook Backed Up
pause
EXIT

:ERROR
CLS
COLOR 4E
ECHO %USERNAME% Error during Backup
EXIT

Posted

Hey

Thanks for the reply. Ill try using the n+, I had seen this before but it looked a bit complicated. Makes more sense now someone explains it :)

If not I'll have to dust off my VB knowledge!

Posted

Cool, That seems to work percectly.

Next problem is...

I need to remove from the %RegKey!N!% whatever is afer the final \

For example, one of the strings is pulls out is ; C:\Outlook\pudsey.pst

If you present this to robocopy is sets the source as C:\Outlook\pudsey.pst\

which obviously doesnt exist, is there a way of shortening this to display C:\Outlook\

These files arent all in the same place, or called the same thing, so it needs to be specificaly vague...

Also... there are 3 spaces at the beginning of the string. I can remove these by changing the DELIMS= to DELIMS= but one of the strings is in the documents and settings folder and it thinks that the space in that is a delimiter and shortens it to; C:\Documents, fairly useless.... any ideas?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...