Page 1 of 3

For loop to hit each user on a machine

Posted: 26 Mar 2013 11:20
by motanicus
I am writing a script that backs up PST files for our users prior to a migration where we'll need to reimport the data later into a new account.

What I'm trying to figure out is how to write a 'for' loop to apply the backup script to each user profile where PST files exist:

I know that this code will not work due to the multiple wildcards -- it will not recurse through each user simply because I use a wildcard for the profile name -- so I'm looking for some code logic assistance getting each copy to work properly.

In essence we want to check each profile on a computer to see if a PST exists, and then run the following copy commands. For the sake of clarification, I originally used %homepath% rather than \users\*\ to delimit the path to the files, this alteration of the script is merely to interpret what I'm trying to have happen (prior to realizing I need to create a loop somewhere):

Code: Select all

xcopy /y /e /f "\users\*\AppData\Local\Microsoft\Outlook\*.*ST" "\users\*\AppData\Local\Microsoft\Outlook\backup\*.*" >> "\\server\emailbackup\Log.TXT"
xcopy /y /e /f "\users\*\AppData\Roaming\Microsoft\Templates\*.*" "\users\*\AppData\Local\Microsoft\Outlook\backup\Templates\*.*" >> "\\server\emailbackup\Log.TXT"
xcopy /y /e /f "\users\*\AppData\Roaming\Microsoft\Stationery\*.*" "\users\*\AppData\Local\Microsoft\Outlook\backup\Stationery\*.*" >> "\\server\emailbackup\Log.TXT"
xcopy /y /e /f "\users\*\AppData\Roaming\Microsoft\Signatures\*.*" "\users\*\AppData\Local\Microsoft\Outlook\backup\Signatures\*.*" >> "\\server\emailbackup\Log.TXT"
PAUSE


Any suggestions?

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 11:24
by motanicus
Just to add to this, I found this code while browsing the forum and it appears to do part of what I need:

Code: Select all

@echo off
for /f "delims=" %%a in ('dir "%systemdrive%\users" /ad /b') do xcopy /d /y "c:\folder\files\*.*" "%systemdrive%\users\%%a\AppData\Local\Microsoft\Outlook\Backup\"
pause


But is there a way to check for the PST file prior to applying the copy?

Also, will %homepath% being used only copy the files from the %homepath% of the user running the script?

I.E.:

Code: Select all

for /f "delims=" %%a in ('dir "%systemdrive%\users" /ad /b') do xcopy /d /y "%homepath%\filepathtofilestobackup\*.*" "%systemdrive%\users\%%a\AppData\Local\Microsoft\Outlook\Backup\"


OR should it be more like:

Code: Select all

for /f "delims=" %%a in ('dir "%systemdrive%\users" /ad /b') do xcopy /d /y "%systemdrive%\users\%%a\pathtobackupfiles\*.*" "%systemdrive%\users\%%a\AppData\Local\Microsoft\Outlook\Backup\"

?

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 12:33
by mfm4aa
What kind of a path is this:

Code: Select all

"\users\*\AppData\Local\Microsoft\Outlook\*.*ST"

:?:

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 13:07
by foxidrive
This should copy every PST file on c: (assuming they aren't in use) to a network location and create a log file to copy them back later.

Code: Select all

@echo off
set "target=\\server\share\pst"
for /f "delims=" %%a in ('dir c:\*.pst /b /s /a-d') do call :next "%%a"
goto :EOF

:next
set c=
:loop
if exist "%target%\%~n1%c%%~x1" set /a c=c+1 & goto :loop
echo copying %1 to "%target%\%~n1%c%%~x1"
copy /b "%~1" "%target%\%~n1%c%%~x1"
>>"%target%\copy.bat.log" echo copy /b "%target%\%~n1%c%%~x1" "%~1"
goto :EOF

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 13:52
by motanicus
mfm4aa wrote:What kind of a path is this:

Code: Select all

"\users\*\AppData\Local\Microsoft\Outlook\*.*ST"

:?:

\users\{each user}\AppData\Local\Microsoft\Outlook\{All .*st files, PST; OST; etc.}

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 13:57
by motanicus
foxidrive wrote:This should copy every PST file on c: (assuming they aren't in use) to a network location and create a log file to copy them back later.

Code: Select all

@echo off
set "target=\\server\share\pst"
for /f "delims=" %%a in ('dir c:\*.pst /b /s /a-d') do call :next "%%a"
goto :EOF

:next
set c=
:loop
if exist "%target%\%~n1%c%%~x1" set /a c=c+1 & goto :loop
echo copying %1 to "%target%\%~n1%c%%~x1"
copy /b "%~1" "%target%\%~n1%c%%~x1"
>>"%target%\copy.bat.log" echo copy /b "%target%\%~n1%c%%~x1" "%~1"
goto :EOF


Thanks, this works great for finding the PST files, but I also need to keep in mind that if there are multiple users on the machine that it's copying the file to THEIR homepath directory for the backup file (it needs to be a local path, which should be easy enough to change).

For 'target' could I :

Code: Select all

for /f "delims=" %%z in ('dir "%systemdrive%\users\" /ad /b')
SET "target=%systemdrive%\users\%%z\pathtobackupfolder\

?

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 14:05
by foxidrive
motanicus wrote:Thanks, this works great for finding the PST files, but I also need to keep in mind that if there are multiple users on the machine that it's copying the file to THEIR homepath directory for the backup file (it needs to be a local path, which should be easy enough to change).


The code copies the files to a network drive into one folder, renaming them with a number. The log file keeps track of where they came from and can be used to copy them back, to the original location and name, later. You can use *.?st in the search term too.

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 15:11
by motanicus
OK cool, I think I have enough info to go on here. Thanks!

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 15:18
by motanicus
Here's what I've come up with, need syntax check if anyone is good with it:

Code: Select all


for /f "delims=" %%z in ('dir "%systemdrive%\users\" /ad /b')
SET "target=%systemdrive%\users\%%z\AppData\Local\Microsoft\Outlook\Backup\"

for /f "delims=" %%a in ('dir c:\*.pst /b /s /a-d') do (
call :next "%%a"
goto :EOF
xcopy /y /i /f %systemdrive%\users\%%z\AppData\Roaming\Microsoft\Templates\*.* %target%\Templates\ >> "%target%\backup.log"
xcopy /y /i /f %systemdrive%\users\%%z\AppData\Roaming\Microsoft\Stationery\*.* %target%\Stationery\ >> "%target%\backup.log"
xcopy /y /i /f %systemdrive%\users\%%a\AppData\Roaming\Microsoft\Signatures\*.* %target%\Signatures\ >> "%target%\backup.log"
)

:next
set c=
:loop
if exist "%target%\%~n1%c%%~x1" set /a c=c+1 & goto :loop
echo copying %1 to "%target%\%~n1%c%%~x1"
copy /b "%~1" "%target%\%~n1%c%%~x1"
>>"%target%\backup.log" echo copy /b "%target%\%~n1%c%%~x1" "%~1"
goto :EOF

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 15:40
by mfm4aa
The for loop always goes for in () do.

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 15:45
by foxidrive
Do you expect your users to have their files in their user profile?

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 15:46
by foxidrive
mfm4aa wrote:The for loop always goes for in () do.


The logic is fubar too.

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 15:52
by motanicus
foxidrive wrote:Do you expect your users to have their files in their user profile?


Yes - all users will have their files in user profile under the specific path - and we're creating the backup within the default file location for their PST/OST file.

I actually plan on changing the script to MOVE the files rather than copying them so that it forces the user to get a new account prompt for our new service, but until I get the logic to work I'm having it copy for now.

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 15:54
by foxidrive
How many PST files and OST file per user will there be? Will they have unique names per users?

Re: For loop to hit each user on a machine

Posted: 26 Mar 2013 16:00
by motanicus
Each user has a unique name. Most users only use 1 pst or ost file, but some have archive.pst files to save as well.