For loop to hit each user on a machine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

For loop to hit each user on a machine

#1 Post by motanicus » 26 Mar 2013 11:20

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?
Last edited by motanicus on 26 Mar 2013 14:01, edited 1 time in total.

motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

Re: For loop to hit each user on a machine

#2 Post by motanicus » 26 Mar 2013 11:24

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\"

?

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: For loop to hit each user on a machine

#3 Post by mfm4aa » 26 Mar 2013 12:33

What kind of a path is this:

Code: Select all

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

:?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For loop to hit each user on a machine

#4 Post by foxidrive » 26 Mar 2013 13:07

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

motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

Re: For loop to hit each user on a machine

#5 Post by motanicus » 26 Mar 2013 13:52

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.}

motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

Re: For loop to hit each user on a machine

#6 Post by motanicus » 26 Mar 2013 13:57

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\

?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For loop to hit each user on a machine

#7 Post by foxidrive » 26 Mar 2013 14:05

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.

motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

Re: For loop to hit each user on a machine

#8 Post by motanicus » 26 Mar 2013 15:11

OK cool, I think I have enough info to go on here. Thanks!

motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

Re: For loop to hit each user on a machine

#9 Post by motanicus » 26 Mar 2013 15:18

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

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: For loop to hit each user on a machine

#10 Post by mfm4aa » 26 Mar 2013 15:40

The for loop always goes for in () do.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For loop to hit each user on a machine

#11 Post by foxidrive » 26 Mar 2013 15:45

Do you expect your users to have their files in their user profile?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For loop to hit each user on a machine

#12 Post by foxidrive » 26 Mar 2013 15:46

mfm4aa wrote:The for loop always goes for in () do.


The logic is fubar too.

motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

Re: For loop to hit each user on a machine

#13 Post by motanicus » 26 Mar 2013 15:52

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For loop to hit each user on a machine

#14 Post by foxidrive » 26 Mar 2013 15:54

How many PST files and OST file per user will there be? Will they have unique names per users?

motanicus
Posts: 19
Joined: 26 Mar 2013 11:13

Re: For loop to hit each user on a machine

#15 Post by motanicus » 26 Mar 2013 16:00

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.

Post Reply