batch file goodness?

xSauronx

Lifer
Jul 14, 2000
19,582
4
81
I...suck at programming in general, and never even really played with batch files and the windows command line much.

Situation: Windows XP Pro systems at the hospital I work at. Some profiles have issues that do not crop up until we upgrade from Office 2000 to 2003 (yeah, you read that correctly)

what I want: a batch file that will *just* copy C:\Documents and Settings\%USERNAME%\Application Data\Microsoft to another location on the drive in case the profile farks up, because we need the custom dictionaries and autocorrect entries that these people have

I can do the basic batch file...but I want to run it as an admin and have it back up that one directory for *each* user and store it as a copy under a subfolder for each user.

I googled a bit (I have very limited programming/scripting experience and knowledge) but Im still not really sure how to write something that will do that.

Can someone tell me what i need to look for or give me an idea of what to do or anything at all? Id ask someone at the hospital...but Im an intern, nobody off my team is going to really care (nobody on my team can do this off the top of their head), and the others teams there are notorious for not helping one another out unless they really have to :-/
 

GeekDrew

Diamond Member
Jun 7, 2000
9,099
19
81
IMO, it would be easier to write something that would be run as a user script (perhaps at login), that copies the folder to another location within the user profile (to prevent permissions issues). For example, copy "Application Data\Microsoft\" to "Application Data\Backup\Microsoft\". That would be, in my opinion, easier to write and execute, and probably more reliable, than having a script run as admin and loop through every profile on each computer to check for the existence of this folder, and then copy it elsewhere.
 

xSauronx

Lifer
Jul 14, 2000
19,582
4
81
The issue with that is three fold:
-getting someone at work to write and apply a login script to a select group of users isnt going to happen. (we just have one serious issue possible with one department at this point)
-having all users with a profile on a given workstation available to login to let that script run *before* their pc is updated is VERY unlikely.
-saving that data to a folder in their profile is an iffy idea, because if their profile goes wonky during or after the update, we're concerned that anything in their profile folder is subject to being borked.

im interested in other ways to do this, but as it stands, even if its done manually, were going to have to backup that folder for every user on workstations in that department to a spot on the hard drive or the network before theyre going to give us the ok to update their machines :-/
 

mvbighead

Diamond Member
Apr 20, 2009
3,793
1
81
Here's where I am at so far:


cd "c:\documents and settings"

dir /s /b Microsoft > backups.txt

for /f "tokens=* delims= " %%a in (backups.txt) do (
REM ECHO xcopy /d /e /c /i /y "%a" C:\backups\%counter%
set value=%%a
echo %value
echo %value:~0,-1%

)


Can seem to generate a unique path for each user, for whatever reason... counters are a bitch in batch files... at least for me. Can PowerShell scripts be used for your deal?
 

xSauronx

Lifer
Jul 14, 2000
19,582
4
81
Here's where I am at so far:


cd "c:\documents and settings"

dir /s /b Microsoft > backups.txt

for /f "tokens=* delims= " %%a in (backups.txt) do (
REM ECHO xcopy /d /e /c /i /y "%a" C:\backups\%counter%
set value=%%a
echo %value
echo %value:~0,-1%

)


Can seem to generate a unique path for each user, for whatever reason... counters are a bitch in batch files... at least for me. Can PowerShell scripts be used for your deal?

not familiar with the details on powershell, is that installed by default with either xp or service pack 2 (theyre not up to sp3 here at this point)? if i have to install someting extra, even someting minor, to make the backup easy its a toss-up as to whether or not itll be approved.
 

mvbighead

Diamond Member
Apr 20, 2009
3,793
1
81
I don't know that PS requires SP3, but it is installable as a MS update, but not natively included. Perhaps a VB script would be ideal...
 

xSauronx

Lifer
Jul 14, 2000
19,582
4
81
I don't know that PS requires SP3, but it is installable as a MS update, but not natively included. Perhaps a VB script would be ideal...

Id take most anything that would work without having to install anything extra, i was hoping to manage a little batch script by myself but *shrug* that doesnt seem likely, and i dont really script/program to start with
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Vbscript would work well for this.
I will give you a start.
This site is the best reference for how to use it: http://msdn.microsoft.com/en-us/library/6kxy1a51(VS.85).aspx

Code:
Dim FSO, FLD
Dim strFolder

	'Change as needed
	strFolder = "C:\Documents and Settings"	'XP
	strBackup = "C:\Backup\"

	'Create the filesystem object
	Set FSO = CreateObject("Scripting.FileSystemObject")
	'Get a reference to the folder you want to search
	set FLD = FSO.GetFolder(strFolder)
	
	'loop through the folder and get all users folders
	For Each folder In FLD.SubFolders
		Dim profile
		profile = strFolder & "\" & folder.Name & "\Application Data\Microsoft"

		If FSO.FolderExists(profile) Then
			
			'copy from %USERNAME%\Application Data\Microsoft to backup\%USERNAME%
			'this copy call may fail if the profile data is in use (user is logged in)
			FSO.CopyFolder profile, strBackup & folder.Name
		End if

	Next
 

xSauronx

Lifer
Jul 14, 2000
19,582
4
81
Ill play with it some at home this weekend and see what I get Monday when Im back at work, thanks :)

if anyone else has any ideas im all ears...though obviously im pretty limited in what i can do or get done
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
what I want: a batch file that will *just* copy C:\Documents and Settings\%USERNAME%\Application Data\Microsoft to another location on the drive in case the profile farks up, because we need the custom dictionaries and autocorrect entries that these people have

I can do the basic batch file...but I want to run it as an admin and have it back up that one directory for *each* user and store it as a copy under a subfolder for each user.

So, some pseudocode for the process you're trying to do would look like this:

FOR each directory that exists under documents and settings\
copy this directory to somewhere else

That's a relatively simple batch file.

Code:
for /f %%A in ('dir /a:d /b "c:\documents and settings\"') DO (
xcopy /e /y "c:\documents and settings\%%A c:\backups\%%A\
)

You weren't that far off in your own attempt. With a bit more practice, you'll get the hang of it.