Articles

copy *.pst from users profile (backup)

July 9, 2010 at 12:03:42
Specs: Windows Vista

Trying to build a batch script and need some assistance. The script is to search a users profile for *.PST files and copy them over to another location (a backup). Well the issue is when there are duplicate PST files with the same name. I don’t want to overwrite the file, I want to still be able to copy that file over as well. Is there a way for it to give me the option to rename the file instead of overwriting the file? Or automatically changes the file name slightly without completely changing the file name maybe add a digit at the end of the file (ex: file.txt change file1.txt automatically)?
This is part of the script I’m using to search for the files and copy over.

FOR /r C:\users\%UNAME% %%d in (.) do xcopy %%d\*.PST E:\%UName%\PST

For now I use this line to make a doc of all the *.PST I find and look it over for duplicates. Then go through the folders and copy the duplicate file names over manually.

DIR /b /s /d c:\users\%UNAME%\*.PST >E:\%UName%\PST\%UNAME%_Profile.txt

Not sure if it could be done, thanks in advance for your help.


See More: copy *.pst from users profile (backup)

Report •


#1
July 9, 2010 at 12:15:45

You can use my backup script...

@echo off
taskkill /im outlook.exe

Set Monthfolder=Monthly\%Date:~-4,4%-%Date:~-10,2%
if not exist "\\10.100.100.7\backups\userfiles\%username%" call :createdir 

Cls

::echo Backing up local files...
::Call \\10.100.100.7\scripts\robocopy "%userprofile%" "\\10.100.100.7\backups\userfiles\%username%\Daily" *.do* /if *.xl* /if *.pdf /if *.pp* /if *.txt /XD %userprofile%\AppData /XD "%userprofile%\local settings" /XD "%userprofile%\Application Data" /s /COPY:DAT /r:1 /w:1 /np /xo /eta >> h:\BackupLog.txt
:: This only backups Word, Excell, Power Point, Adobe and TXT files to eliminate personal pictures and using up unneeded disk space.

echo Backing up Email...
Call \\10.100.100.7\scripts\robocopy "%userprofile%" "\\10.100.100.7\backups\userfiles\%username%\%monthfolder%" *.pst* /s /COPY:DAT /r:1 /w:1 /np /xo /eta >> h:\BackupLog.txt
goto :eof

:createdir
md "\\10.100.100.7\backups\userfiles\%username%"
goto :eof

I create a folder for each month and label it with the date. Then I copy the file into the folder. I use to keep daily backups but did not have enough space on the drive so resorted to monthlies. Hope it helps you. I also backup their desktop files to so you may want to remove that part. Also, I use ROBOCOPY in stead of XCOPY. It is more powerful so you will need to install the Windows Resource Kit so you can get ROBOCOPY...

http://theos.in/windows-server/free...


Report •

#2
July 16, 2010 at 07:24:01

I got the script working thanks to markls. I found a comment he posted on what I was looking for. But im still new to this scripting and trying to learn as I go. Now the issues is still the same I search the users profile for PST files. When it finds a duplicate name example archive.pst it copies archive.pst and a archive(1).pst. but if it finds a 3rd or fourth pst file with the same same it overwrites the ones it just copied over. Is their a way to edit the script I have to increase the value for each existing file with the same name. Right now it only goes to (1).

MKDIR I:\%UName%\%DeskFolder%\%DeskFolder%_Profile
DIR /b /s /d c:\users\%UNAME%\*.pst >I:\%UName%\%DeskFolder%\PST_Profile\%UNAME%_Profile.TXT

FOR /r C:\users\%UNAME% %%f in (*.PST) DO (XCOPY %%f\*PST I:\%UName%\%DeskFolder%\%DeskFolder%_Profile
SET CanCopy=false
SET folder=%%~pt
FOR %%x in ("!folder:~0,-1!") DO SET folder=%%~nx
IF /i "!folder!" neq "I:\%UName%\%DeskFolder%\%DeskFolder%_Profile" IF /i "%%f" neq "%~dpnx0" (
SET CanCopy=true
)
IF "!CanCopy!" equ "true" (
IF EXIST "I:\%UName%\%DeskFolder%\%DeskFolder%_Profile\%%~nxf" (
CALL :GetNextFilename "I:\%UName%\%DeskFolder%\%DeskFolder%_Profile\%%~nxf" newfile
COPY "%%f" "I:\%UName%\%DeskFolder%\%DeskFolder%_Profile\!newfile!"
) ELSE (
COPY "%%f" "I:\%UName%\%DeskFolder%\%DeskFolder%_Profile"
)
)
)

:GetNextFilename
SET n=0
:loop
SET /a n+=1
SET %2=%~n1(%n%)%~x1
IF EXIST "%~p1!%2!" goto loop


Report •

#3
July 16, 2010 at 08:04:36

It looks like some of my code but it's been quite heavily
modified. Start by putting
@echo off
setlocal EnableDelayedExpansion

at the top and
goto :eof

before
:GetNextFilename



Report •

Related Solutions

#4
July 16, 2010 at 14:00:33

no offense meant, but I don't see how the posted script (#2) could possibly be working. If it is, i would be amazed.
for one thing, i don't think you can assign values to positional vars (%1, %2 etc). It would be neat if you could, but i haven't found a way: SET %2=%~n1(%n%)%~x1

If you just want all the .pst dumped to one directory, this might be simpler and less problematical:

@echo off & setlocal enabledelayedexpansion
:: enable foll. line if you want incremental backup
set bkup=\%date:~4,2%%date:~7,2%%date:~10,4%\
:: or substitute whichever dir you are targeting in foll
pushd %userprofile%
:: destination base/root
set dest=c:\backup
for /r %%a in (*.pst) do (
set tag=
set /a nn=-1
set name=%%~na
set ext=%%~xa
call :cpy
)
goto :eof

:cpy
if not exist "%dest%%%bkup%%name%%tag%%ext%" (
copy "%%a" "%dest%%bkup%%name%tag%%ext%"
) else (
set /a nn+=1
set tag=_%nn%
goto :cpy)
::-------- end
not tested.


Report •

#5
July 16, 2010 at 14:50:29

It works I am using it right now for my User Logoff Scripts. Works great. It makes a Month folder on the server under each user and copy the PST file and names it with the moth the backup ran. In fact I just cut and pasted it from the running script that is why it still has the UNC paths for my server in the scripts and I remarked out the local file backups because he did not want that part.

By the way where do you see me setting the %1 and %2?

Maybe you never used ROBOCOPY before. It does every thing for you. Much more powerful than XCOPY it was written to replace XCOPY.

If yu want to do things OLD SCHOOL its fine with me never hurt my feelings.


Report •

#6
July 16, 2010 at 16:03:52

@Ace: No, not yours - that one's find. I was referring to resp #2 mutation. I percieve lots of potential for errors in it.

Report •

#7
July 16, 2010 at 16:10:29

oops, sorry I thought you ment the second post, my bad.

Report •

#8
July 17, 2010 at 04:16:33

nbrane, SET %2=%~n1(%n%)%~x1 is actually one of
the few bits of code that's still intact. It's quite valid though -
run this simplified example for proof:

@echo off

setlocal

set SomeVar=before
call :sub SomeVar
echo %SomeVar%
goto :eof


:sub
set %1=after



Report •

#9
July 17, 2010 at 18:41:08

hey, by golly you're right. I was confusing subroutine %1 with batchscript %1, which, i'm pretty sure, can NOT be changed?
set test=hello
call change1.bat %1 %test%
echo %test%
echo %1

change1.bat:
set %1=ONEgoodbye
set %test%=goodbyeTEST

so the %1 can't be altered, but var.s can be altered and sent back.


Report •

#10
July 18, 2010 at 06:19:46

Er, yeah, you lost me a bit there, TBH, nbrane, but
basically my usage is changing the value of what %1
points to not the value of %1 itself.



Report •

#11
July 19, 2010 at 12:36:16

Thanks for all your comments guys. (mutation) lol yea i know like I said learning as I go. But this isnt for a incremental backup. I go out to different locations working on a users PC their pst files are never in the correct location. So I search thier profile, second check local disk C:\ (not the sub dir), third scan their secondary drive if they have one installed. Most the time they have more then one pst file with the same name but different file sizes. Is their a simple script to just check for /r c:\users\%profil% %%f in (*.PST) do (xcopy %%f\*.PST I:\%profil%\pst. then If the .pst file exist just add a (1),(2),(3),(4) etc. to the end of the filename? Thanks

"Success consists of going from failure to failure without loss of enthusiasm".


Report •

#12
July 20, 2010 at 08:17:38

Try the following (there's comment lines that start
with '::' which should help you to expand it to include
other dirs in the search):

@echo off

setlocal EnableDelayedExpansion

set DestDir=i:\%userprofile:~3%\PST
md "%DestDir%" 2>nul
if not exist "%DestDir%\" (
  echo Cannot create backup directory.
  goto :eof
)

set BaseDir=%userprofile%
for /f "delims=" %%f in ('dir /b /s "%BaseDir%\*.pst"') do (
  if exist "%DestDir%\%%~nxf" (
    call :GetNextFilename "%DestDir%\%%~nxf" newfile
    copy "%%f" "%DestDir%\!newfile!"
  ) else (
    copy "%%f" "%DestDir%\"
  )
)

:: To add more directories, copy the block above, upto and including
:: 'set BaseDir=...', to here and set BaseDir to equal the new root
:: without trailing backslash. So if it's 'c:\', just set it to
:: 'c:'. If you don't want to include sub-dirs remove /s switch from
:: dir command.

goto :eof


:GetNextFilename
set n=0
:loop
set /a n+=1
set %2=%~n1(%n%)%~x1
if exist "%~dp1!%2!" goto loop


Report •

#13
July 21, 2010 at 06:35:31

It worked like a champ. Thank you sir..

Report •


Ask Question