Avatar of pcgym
pcgym
Flag for United States of America asked on

Backup User Profile in BAT file

I would like to add the "My Documents" folder to the list of files that are backed up in this script.

I would also like to back it up to a UNC path or mapped drive.

Can anyone help with this?
:: ========================================
:: BKUPUSER.BAT
::
:: Copy user data to C_BACKUP\%ComputerName% folder
:: ========================================
@echo off

:: ----------------------------------------
:: Initialise Variables
:: ----------------------------------------
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook
set FavoritesFolder=%userprofile%\Favorites
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures
set BackupFolder=%userprofile%\My Documents\c_backup\%computername%

:: ----------------------------------------
:: Prepare Folders
:: ----------------------------------------
md "%BackupFolder%\pst" 2>nul
md "%BackupFolder%\nk2" 2>nul
md "%BackupFolder%\signatures" 2>nul
md "%BackupFolder%\favorites" 2>nul
 
:: ----------------------------------------
:: Copy PST Files
:: ----------------------------------------
echo Copying PST files....
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1
if %errorlevel%==4 (
   echo.
   echo You must close Outlook before backing up it's PST files....
   echo.
   pause
   exit /b 4
)
 
:: ----------------------------------------
:: Copy NK2 Files
:: ----------------------------------------
echo Copying NK2 files....
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul
 
:: ----------------------------------------
:: Copy Favorites
:: ----------------------------------------
echo Copying Favourites....
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt
 
:: ----------------------------------------
:: Copy Signatures
:: ----------------------------------------
echo Copying Outlook's signatures....
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul

Open in new window

Microsoft DOSWindows BatchWindows XP

Avatar of undefined
Last Comment
Rory de Leur

8/22/2022 - Mon
Indivara

You can use
xcopy sourcedir \\servername\sharename\folder\



Rory de Leur

Something like this?

Ps on line 18; if you set netbackup=0 it won't backup to network


:: ========================================
:: BKUPUSER.BAT
::
:: Copy user data to C_BACKUP\%ComputerName% folder
:: ========================================
@echo off

:: ----------------------------------------
:: Initialise Variables
:: ----------------------------------------
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook
set FavoritesFolder=%userprofile%\Favorites
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures
set BackupFolder=%userprofile%\My Documents\c_backup\%computername%
set DocumentsFolder=%userprofile%\My Documents
set NetBackupFolder=\\fileserver\share\%username%\c_backup\%computername%
set netbackup=1

:Loop
:: ----------------------------------------
:: Prepare Folders
:: ----------------------------------------
md "%BackupFolder%\pst" 2>nul
md "%BackupFolder%\nk2" 2>nul
md "%BackupFolder%\signatures" 2>nul
md "%BackupFolder%\favorites" 2>nul
md "%BackupFolder%\My Documents" 2>nul
 
:: ----------------------------------------
:: Copy PST Files
:: ----------------------------------------
echo Copying PST files....
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1
if %errorlevel%==4 (
   echo.
   echo You must close Outlook before backing up it's PST files....
   echo.
   pause
   exit /b 4
)
 
:: ----------------------------------------
:: Copy NK2 Files
:: ----------------------------------------
echo Copying NK2 files....
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul
 
:: ----------------------------------------
:: Copy Favorites
:: ----------------------------------------
echo Copying Favourites....
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt
 
:: ----------------------------------------
:: Copy Signatures
:: ----------------------------------------
echo Copying Outlook's signatures....
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul

:: ----------------------------------------
:: Copy My Documents
:: ----------------------------------------
echo Copying Favourites....
xcopy "%DocumentsFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /h /EXCLUDE:\c_backup\


:: ----------------------------------------
:: Loop for backup 2 Network
:: ----------------------------------------
if %netbackup%==1 goto Netbackup
goto End

:Netbackup
set BackupFolder=%NetBackupFolder%
set netbackup=0
goto loop

:End
Exit

Open in new window

Indivara

Yes, it should work like a local folder.
(I assume you are creating the computername folder)

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
pcgym

ASKER
I modified the UNC path for the NetBackupFolder, ran the script and I get the result in the attached screenshot.

Outlook is closed.

Any ideas?
:: ======================================== 
:: BKUPUSER.BAT 
:: 
:: Copy user data to C_BACKUP\%ComputerName% folder 
:: ======================================== 
@echo off 
 
:: ---------------------------------------- 
:: Initialise Variables 
:: ---------------------------------------- 
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook 
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook 
set FavoritesFolder=%userprofile%\Favorites 
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures 
set BackupFolder=%userprofile%\My Documents\c_backup\%computername% 
set DocumentsFolder=%userprofile%\My Documents 
set NetBackupFolder=\\it1\it\%username%\c_backup\%computername% 
set netbackup=1 
 
:Loop 
:: ---------------------------------------- 
:: Prepare Folders 
:: ---------------------------------------- 
md "%BackupFolder%\pst" 2>nul 
md "%BackupFolder%\nk2" 2>nul 
md "%BackupFolder%\signatures" 2>nul 
md "%BackupFolder%\favorites" 2>nul 
md "%BackupFolder%\My Documents" 2>nul 
  
:: ---------------------------------------- 
:: Copy PST Files 
:: ---------------------------------------- 
echo Copying PST files.... 
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1 
if %errorlevel%==4 ( 
   echo. 
   echo You must close Outlook before backing up it's PST files.... 
   echo. 
   pause 
   exit /b 4 
) 
  
:: ---------------------------------------- 
:: Copy NK2 Files 
:: ---------------------------------------- 
echo Copying NK2 files.... 
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul 
  
:: ---------------------------------------- 
:: Copy Favorites 
:: ---------------------------------------- 
echo Copying Favourites.... 
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt 
  
:: ---------------------------------------- 
:: Copy Signatures 
:: ---------------------------------------- 
echo Copying Outlook's signatures.... 
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul 
 
:: ---------------------------------------- 
:: Copy My Documents 
:: ---------------------------------------- 
echo Copying Favourites.... 
xcopy "%DocumentsFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /h /EXCLUDE:\c_backup\ 
 
 
:: ---------------------------------------- 
:: Loop for backup 2 Network 
:: ---------------------------------------- 
if %netbackup%==1 goto Netbackup 
goto End 
 
:Netbackup 
set BackupFolder=%NetBackupFolder% 
set netbackup=0 
goto loop 
 
:End 
Exit

Open in new window

error1.bmp
Indivara

Is it running in the background? In the taskbar notification area?
If not, open task manager (Ctrl+Shift+Esc) and see whether the process is still running there (outlook.exe)


Indivara

Try removing the 2>&1 part and see what error is displayed.
I get "File creation error - The process cannot access the file because another process has locked a portion of the file." when Outlook is running

outlook-error.png
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pcgym

ASKER
Outlook.exe is not running.
Rory de Leur

Fixed:
- Errorlevel has a reset at the bottom
- Exclusion switch was not properly used..

:: ========================================
:: BKUPUSER.BAT
::
:: Copy user data to C_BACKUP\%ComputerName% folder
:: ========================================
@echo off

:: ----------------------------------------
:: Initialise Variables
:: ----------------------------------------
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook
set FavoritesFolder=%userprofile%\Favorites
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures
set BackupFolder=%userprofile%\My Documents\c_backup\%computername%
set DocumentsFolder=%userprofile%\My Documents
set NetBackupFolder=\\fileserver\share\%username%\c_backup\%computername%
set netbackup=1
echo \c_backup\ > excludelist.txt

:Loop
:: ----------------------------------------
:: Prepare Folders
:: ----------------------------------------
md "%BackupFolder%\pst" 2>nul
md "%BackupFolder%\nk2" 2>nul
md "%BackupFolder%\signatures" 2>nul
md "%BackupFolder%\favorites" 2>nul
md "%BackupFolder%\My Documents" 2>nul
 
:: ----------------------------------------
:: Copy PST Files
:: ----------------------------------------
echo Copying PST files....
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1
if %errorlevel%==4 (
   echo.
   echo You must close Outlook before backing up it's PST files....
   echo.
   pause
   exit /b 4
)
 
:: ----------------------------------------
:: Copy NK2 Files
:: ----------------------------------------
echo Copying NK2 files....
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul
 
:: ----------------------------------------
:: Copy Favorites
:: ----------------------------------------
echo Copying Favourites....
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt
 
:: ----------------------------------------
:: Copy Signatures
:: ----------------------------------------
echo Copying Outlook's signatures....
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul

:: ----------------------------------------
:: Copy My Documents
:: ----------------------------------------
echo Copying Favourites....
xcopy "%DocumentsFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /h /EXCLUDE:


:: ----------------------------------------
:: Loop for backup 2 Network
:: ----------------------------------------
if %netbackup%==1 goto Netbackup
goto End

:Netbackup
set BackupFolder=%NetBackupFolder%
set netbackup=0
set errorlevel=0
goto loop

:End
del /Q excludelist.txt
Exit

Open in new window

pcgym

ASKER
After removing "2>&1" I get the same error.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Rory de Leur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
pcgym

ASKER
CRIPL, we are very close now.

Everything worked perfectly except the MyDocs folder was not backed up.  The folder was created but there were no files in it.
Rory de Leur

Indeed, line 35 remove "&1" at the end..