In part 1 of “Scripting Your Own Computer Repair Tools”, I showed you how to create scripts that make use of certain applications. In this article, I’ll show you how to make scripts to manipulate files. In order to continue on with our backup example from part 1, I am going to show you how to make a script that will backup the main areas of a system that a client would want to have backed up.
Disclaimer: This article is for people with a good understanding of Windows only. I highly, HIGHLY recommend that you do scripting in either a Virtual Machine environment or a dedicated, unimportant machine in case you make a mistake with one of your commands and do some damage to the system. Technibble or I cannot be held responsible if you do damage to the operating system or lose any data.
In this article I am going to make use of a very useful command called xcopy. Some of the older technicians who were working with computers in the DOS days should know this command fairly well. XCOPY is a powerful version of the DOS copy command with some additional features. It has the ability to move single files, directories or even whole drives from one location to another. For our backup script, this command will come in very handy when we use the appropriate “switches”.
So what are “switches?” Most command line applications have “switches” that allow you to alter how the application will work. In Part 1’s example where we used ProduKey to export the systems CD Keys, we used the switch /stext that will output the information to a text file. We could have used another switch like /shtml and that would have exported the information to a html page. We are still using the same ProduKey file, but are manipulating how it works by using switches.
The XCOPY command has switches as well, in fact, many of them. There are too many to list here but this is the command I am going to be using to backup the clients data (in his case, their favorites):
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "Favorites"
Unlike ProduKey were we only used a single switch after typing the application name, we are going to use 8 different ones for xcopy. This is the breakdown of what each switch we are using and what it tells xcopy to do:
In short, we have told xcopy to continue copying if an error occurs. If the file already exists, only copy newer versions. Copy all directories and subdirectories even if they are empty. Copy hidden and system files too. If the file already exists and it is read-only, overwrite it anyway and don’t ask us for permission to overwrite existing files. For a full list of the xcopy switches, check out this page.
Lets start making our script. To begin, Create a folder somewhere other than our Desktop or My Documents. In that folder, create a new text file and add the following line to it.
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "Favorites"
Now, save this text file as a .BAT file (or save as a TXT, then change the extension to .BAT). This command is going to copy the currently logged-on users “Favorites” folder and copy it to whereever this script is located. If you remember the %USERPROFILE% variable from Part 1, this will be filled out with something like “C:\Documents and Settings\Bryce” in Windows XP or “C:\Users\Bryce” in Windows Vista and 7.
Close your text editor and run this BAT file, if you did it correctly you should see the current users Favorites folder appear next to the script. We wont need to have the xcopy application next to the script like we did with ProduKey because the xcopy command is built into Windows.
While we are at it, lets grab the content of the current users Desktop as well.
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "Favorites"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Desktop" "Desktop"
If you save and run this script, you’ll see the Desktop folder appear in the same folder as the script.
Now, if you run this script on multiple computers, the all of the different clients data is going to end up in the same folders and we obviously don’t want that. We need to separate them.
In Part 1, we separated our CDKeys and Mail settings using the %COMPUTERNAME% variable which will put them in a folder that has the same name as the computer (eg. Office-PC). The problem with that is the users Desktop and Favorite folders are username specific so we need to sort the files by computer name AND user account. To do this we can use the %USERNAME% variable and change our script to:
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "%COMPUTERNAME%\%USERNAME%\Favorites"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Desktop" "%COMPUTERNAME%\%USERNAME%\Desktop"
If my computers name was “BrycePC” and my user account was “Admin”. The Favorites backup would be located in “BrycePC\Admin\Favorites”. If I ran it on another account on the same PC (lets say “Bryce”), the folder structure would be:
We can also get away with not first creating the Computer Name and Username folder as we did in the last lesson using the mkdir command because if the folder doesn’t exist, the script will create it.
Now lets add the “My Documents” directory to the script. To make this work in Windows XP we would do something like this:
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\My Documents" "%COMPUTERNAME%\%USERNAME%\My Documents"
However, since Windows Vista came along, there is no “My Documents” folder, it is now just “Documents”. To have this line work in Windows Vista and Windows 7, it would need to be:
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Documents" "%COMPUTERNAME%\%USERNAME%\Documents"
Even though the %USERPROFILE% variable used to become “C:\Documents and Settings\Admin” in Windows XP, it is still valid in Windows Vista and 7 as it just replaces %USERPROFILE% with the newer value of “C:\Users\Admin” and it will still work.
To get over the Documents vs My Documents problem. You would need one copy of this script for Windows XP and another for Windows Vista/7. It is possible let the user choose the operating system (Press 1 for XP, Press 2 for Vista/7), but that is a bit advanced for a simple tutorial like this. If you are feeling adventurous, check out the script I made previously that lets the user choose.
To finish this script off, I want to make it so that it doesnt close as soon as it finishes. The reason why I want to do this is so I can see the files it has copied over. To prevent the script closing we can add the following at the very bottom:
@pause
Now, if I have some employees using this script, they may not know why the script has stopped. So I am going to get the script to tell us that the backup is complete by adding the following command just before the pause:
echo Backup Complete!
Anything starting with “echo” will be printed on the screen and wont be run as code.
In the end, our entire script should look like this:
Windows XP version:
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "%COMPUTERNAME%\%USERNAME%\Favorites"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Desktop" "%COMPUTERNAME%\%USERNAME%\Desktop"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\My Documents" "%COMPUTERNAME%\%USERNAME%\My Documents"
echo Backup Complete!
@pause
Windows Vista/7 version:
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "%COMPUTERNAME%\%USERNAME%\Favorites"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Desktop" "%COMPUTERNAME%\%USERNAME%\Desktop"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Documents" "%COMPUTERNAME%\%USERNAME%\Documents"
echo Backup Complete!
@pause
You could also pair this up with what we learned in Part 1 and grab the CD Keys and Email settings too (for XP):
mkdir "%COMPUTERNAME%"
produkey.exe /stext "%COMPUTERNAME%\Keys.txt"
mailpv.exe /stext "%COMPUTERNAME%\Mail.txt"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "%COMPUTERNAME%\%USERNAME%\Favorites"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\Desktop" "%COMPUTERNAME%\%USERNAME%\Desktop"
xcopy /c /d /e /h /i /r /y "%USERPROFILE%\My Documents" "%COMPUTERNAME%\%USERNAME%\My Documents"
echo Backup Complete!
@pause
Of course, this isn’t a limit of what you would want to back up, you would still need to backup emails, address books etc.. but this is just an example.
If you are having problems, Here is a sample of what we have talked about in this article.
Note: This script is for educational purposes only. If you use a script like this in a production environment, always ALWAYS check to make sure that the files actually copied. This script assumes that the client has not moved any of their folders from the default locations. I cannot be held responsible for any losses.
As mentioned in the last article, you aren’t limited to just copying files like this from one place to another. This was just an example. There are plenty of other file management commands such as the move command, the delete command and the select all command. A good list of them can be found on this site including their usage/switches.
Thats all for this lesson, Part 3 will be available soon.
Session expired
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.
Hello!
Can’t you add both “My Documents” and “Documents” in the same script? If the folder doesn’t exist, xcopy will just ignore it.
There’s a catch with the %COMPUTERNAME% directory: I usually reinstall windows with unattended CD’s, so all my clients have the same computer and username.
The script should prompt for a “Client ID” and create a directory with that ID.
Greetings from Portugal! :)
You could use this to determine O/S..
REM Determine Operating System
SET Version=Unknown
VER | FINDSTR /IL "5.0" > NUL
IF %ERRORLEVEL% EQU 0 SET Version=2000
VER | FINDSTR /IL "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=XP
VER | FINDSTR /IL "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=2003
VER | FINDSTR /IL "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=Vista
VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=7
Then using simple ‘if’ statements (which I’m sure Bryce will cover in the next tutorial) you could send it to certain ‘functions’ (or labels as they were called)
if %Version%==Vista goto DoForVista
if %Version%==XP goto DoForXP
:DoForVista
REM Vista Functions Go Here
:DoForXP
REM XP Functions Go Here
Valuable post, much thanks. I have taken the command-line applications you demonstrated and added them to the script I’ve been using for awhile. (Windows XP only)
:: *** Change “set drive= ” to the backup path drive letter or UNC path.
@echo off
:: variables
set folder=%date:~10,4%-%date:~4,2%-%date:~7,2%
set drive=e:%username% from %computername% on %folder%
set backupcmd=xcopy /s /c /d /h /i /r /y
echo ### Backing up My Documents…
%backupcmd% “%USERPROFILE%My Documents” “%drive%My Documents”
echo ### Backing up Desktop…
%backupcmd% “%USERPROFILE%Desktop” “%drive%Desktop”
echo ### Backing up Favorites…
%backupcmd% “%USERPROFILE%Favorites” “%drive%Favorites”
echo ### Backing up email and address book (Outlook Express)…
%backupcmd% “%USERPROFILE%Application DataMicrosoftAddress Book” “%drive%Address Book”
%backupcmd% “%USERPROFILE%Local SettingsApplication DataIdentities” “%drive%Outlook Express”
echo ### Backing up Outlook AutoComplete (NK2) file…
%backupcmd% “%USERPROFILE%Application DataMicrosoftOutlook*.nk2” “%drive%Outlook”
echo ### Backing up Outlook Signature…
%backupcmd% “%USERPROFILE%Application DataMicrosoftSignatures” “%drive%OutlookSignatures”
echo ### Backing up Outlook Archive PSTs…
%backupcmd% “%USERPROFILE%Local SettingsApplication DataMicrosoftOutlook*.pst” “%drive%Outlook”
echo ### Backing up Office/Windows Keys…
produkey.exe /stext “%DRIVE%Keys.txt”
echo ### Backing up the Registry…
if not exist “%drive%Registry” mkdir “%drive%Registry”
if exist “%drive%Registryregbackup.reg” del “%drive%Registryregbackup.reg”
regedit /e “%drive%Registryregbackup.reg”
echo ### Backing up Wireless Keys…
WirelessKeyView.exe /stext “%DRIVE%Wireless-Keys.txt”
echo ### Backing up Internet Explorer Cached Passwords…
iepv.exe /stext “%DRIVE%IE Passwords.txt”
echo ### Backing up Firefox Cached Passwords…
PasswordFox.exe /stext “%DRIVE%Firefox Passwords.txt”
echo ### Backing up Outlook/Outlook Express Passwords…
mailpv.exe /stext “%DRIVE%Outlook Passwords.txt”
Where you have..
XCOPY /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "%COMPUTERNAME%\%USERNAME%\Favorites"
XCOPY /c /d /e /h /i /r /y "%USERPROFILE%\Desktop" "%COMPUTERNAME%\%USERNAME%\Desktop"
XCOPY /c /d /e /h /i /r /y "%USERPROFILE%\Documents" "%COMPUTERNAME%\%USERNAME%\Documents"
You should be able to chain these together –
(the next is run as all one line – note the semicolons: “;” as I made it more readable for this site)..
XCOPY /c /d /e /h /i /r /y "%USERPROFILE%\Favorites" "%COMPUTERNAME%\%USERNAME%\Favorites";
"%USERPROFILE%\Desktop" "%COMPUTERNAME%\%USERNAME%\Desktop";
"%USERPROFILE%\Documents" "%COMPUTERNAME%\%USERNAME%\Documents"
For a simpler example, try this:
DIR C:\SOMEDIR\*.TXT;C:\SOMEDIR\*.EXE
Like the Windows Search, you can use the ; to chain or add to the command you are trying to do.
HTH,
Kent
How would you modify this to do multiple users on the same machine? Also how to remote backup user files across a domain.
I really like your simple unassuming precise instructions in this series.
@echo off
Good tutorial on this is at: http://www.robvanderwoude.com/ntfortokens.php
Note: This is for 2000/XP/2003 – Vista/7 are different for the user profile locations
FOR /F "TOKENS=* DELIMS= " %%A IN ('DIR "C:\DOCUMENTS AND SETTINGS\*.*" /B') DO echo %%A
So.. Expanding this, you would do something like (The XCOPY is one line):
FOR /F "TOKENS=* DELIMS= " %%A IN ('DIR "C:\DOCUMENTS AND SETTINGS\*.*" /B') DO
(
XCOPY /c /d /e /h /i /r /y "C:\DOCUMENTS AND SETTINGS\%%A\Favorites" "%COMPUTERNAME%\%%A\Favorites";
"C:\DOCUMENTS AND SETTINGS\%%A\Desktop" "%COMPUTERNAME%\%%A\Desktop";
"C:\DOCUMENTS AND SETTINGS\%%A\Documents" "%COMPUTERNAME%\%%A\Documents"
)
HTH,
Kent
Actually Fab’s AutoBackup (in your great computer repair program) does all of this.
BTW your emails are great, short, to the point, and with good info. many could learn from you.
Keep it up.
I think that a “canned solution” can be helpful, the scripted solution gives you more control over what you get for results. Also, it gives you a sense of pride that you created something that works and does what you want.
Thanks,
Kent
XCopy is a very powerful command. I use it at work often on a few of our servers. We get a lot of use out of being able to write to virtual directories. This allows us to copy files across multiple servers via a script.
Thanks for the suggestion Kent, however I can’t get it to work. It looks like the script runs but there are no results in the folder MkDir created.
@breadtrk – let’s take this to the “Automation and Scripting” forum.. I have started a new post there for us to continue this discussion on..
Everybody – Please disregard my “one-liner” for XCOPY.. While it does work for DIR, it does not work or XCOPY.. Sorry about that.
Thanks,
Kent
(1) Correction for the multiple profile question/concern
Note: This is for 2000/XP/2003 – Vista/7 are different for the user profile locations (no change)
FOR /F "TOKENS=* DELIMS= " %%A IN ('DIR "C:\DOCUMENTS AND SETTINGS\*.*" /B') DO echo %%A
(2) So.. Expanding this, you would do something like (The XCOPY is no longer one line):
FOR /F "TOKENS=* DELIMS= " %%A IN ('DIR "C:\DOCUMENTS AND SETTINGS\*.*" /B') DO
(
XCOPY /c /d /e /h /i /r /y "C:\DOCUMENTS AND SETTINGS\%%A\Favorites" "%COMPUTERNAME%\%%A\Favorites"
XCOPY /c /d /e /h /i /r /y "C:\DOCUMENTS AND SETTINGS\%%A\Desktop" "%COMPUTERNAME%\%%A\Desktop"
XCOPY /c /d /e /h /i /r /y "C:\DOCUMENTS AND SETTINGS\%%A\Documents" "%COMPUTERNAME%\%%A\Documents"
)
(3) I did read about 3-4 years ago, there was a method using XCOPY with the “richkey” switches to copy your XP-installed machine with Office, etc. to a new volume and then resetting the jumpers for the new drive, it would boot right up with a say a much larger volume..
For example:
XCOPY /r /i /c /h /k /e /y C:\ D:\
(4) There is yet an even more powerful tool than XCOPY and that is either ROBOCOPY (Part of the Resource Kit for server 2003 – available free from Microsoft, you will have to extract ROBOCOPY as well as its Help file out of the download and should work just fine with XP+) or XXCOPY (free for personal use, shareware in a commercial environment).
Thanks,
Kent