Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey guys, I've been working on a backup batch file to backup data on some machines and now I would like to add some command line switches for my batch file.
Couple of questions:
1) Is there anything you can do about the order of how they add the switches?
c:> example.bat -nodocs -nofavs
c:> example.bat -nofavs -nodocs
----------------------ex---
if %1== nodocs set docs=0
if %2== nofavs set favs=0
.
.
.
----------------------
This is just a crude example. but you see my point. Obviously this would
work for the first example, but would fail on the second because the switches
are in the wrong order. Is there a way you can get around this? I would hate
to tell the other guys that you HAVE to enter the switches in this exact
order.
2) How do you avoid if the user doesn't supply any switches?
c:> example.bat
---------------------
if %1== nodocs set docs=0
if %2== nofavs set favs=0
.
.
----------------------
I tried using "if %1== goto noswitch" and "if %1=="" goto noswitch" but both came back with the unexpected return error. This is going to give me an error and terminate because %1 is not defined.
I would also like to add some other switches like a -help so they can see what switches are avaliable. Thanks guys.

I'm not so good with batch files, but wouldn't this work?
if %1== nodocs set docs=0
if %1== nofavs set favs=0
if %2== nodocs set docs=0
if %2== nofavs set favs=0
Michael J

First of all I would use the /I switch format for the If statement to overide the Low/Uppercase duality so that you don't force your users to type a specific format, i.e.
if /I "%1"=="nodocs" set docs=0
works either nodocs or NODOCS or NoDOcS is entered.
Then to check for nul switches again embrace the variables and values with " so
If "%1"=="" GoTo :NOSWITCH
There are variants to that depending on your flavour, e.g.
If %1.==. GoTo :NOSWITCH
The trick is to set a first member that becomes equal to the second one when the variable disappears.
About the switches' order that is left to your (and your users) feeling.

The first two pretty much nailed it.
One nit-picky detail. If you parameter is:
-nofavs
Then you need to test for that string, INCLUDING the dash [-]. A BAT will not treat a dash or slash as a switchchar like an executable usually does.
If at first you don't succeed, you're about average.M2

Thanks for the reply guys. After reading your replies and thinking about what I want to do, I'm going to only have one switch avaliable. We use file synchronization on a lot of machines lately to backup the users' data to our servers, so that's why I wanted to use switches just in case the users' data was already off the drive.
After thinking about it, everything I want to backup should always be backed up(favorites, desktop, Lotus Notes files, etc..), except for the My Documents folder on occassion. So, I'm only going to use the -nodocs switch which should take out the worry of order.Now I'm asking for some more help. Is there a way I can compare 2 directories to make sure their the same size? I know you can use FC for files, but what about directories? I just want to make sure that the copy process didn't error. I don't want to think I backed up all the data of the user to find that it errored out after 1 meg of his 6 gigs of data. I would imagine I could pipe a dir into a file and than pipe a dir of the destination directory and compare the 2 with FC. Can you think of a better way to check for that? Thanks.

Well I just tested my FC idea and I don't think it's going to work because of the first line and the directory line of the dir command output. One is the root drive while the other is a mapped drive so their is going to be a difference in the drive letter. If I could strip those first 3 lines out than all would be good.
Volume in drive C has no label.
Volume Serial Number is 9436-4DF1Directory of C:\notes
comapared with
Volume in drive x has no label.
Volume Serial Number is 9686-d764Directory of x:\backup\notes
I sometimes write things like this assuming everyone knows what I'm talking about, so I'll try to clarify. I'm creating this batch file to backup a users' data on their current machine to a folder on their new machine in my office that I'm currently setting up. The user runs the batch file and it asks for the new computer name(that I supply them and check for with ping command) and than copies such data as their desktop, favs, docs, lotus notes data and such over the network to their new machine.
Suggesting ideas to change the large operation procedures that this company has in place is useless. Trust me the techs here talk how behind the times this company is everyday. I have to deal with what they have in place, so this is why I'm creating this script. Any ideas how to strip thos lines from the files or some other way to compare directories? Thanks.

I think you're on the right track with comparing directories.
Keep in mind that you will want to use the same 'order' for both DIRs.
Like:
dir /on c:\src > src
dir /on x:\dest > destOtherwise the FC will find differences in SRC and DEST even if the contents are really the same.
[A plain vanille DIR lists the files in 'DOS order', ie the order in which they were written to disk.]
Moving right along.
::== compdirs.bat
@echo offdir /on src > srcdir
dir /on dest > destdirfor %%F in (srcdir destdir) do call :chop %%F
fc srcdir destdir>nul
if errorlevel 1 echo they're not the same
goto :eof:chop
find /i /v "volume in " <%1>#
find /i /v "volume serial " <#>##
find /i /v "Directory of " <##>###
find /i /v "<DIR>" <###>####
find /i /v "File(s)" <####>#####
find /i /v "Dir(s)" <#####>%1goto :eof
:: DONENote that a little judicious testing goes a long way. The verbage spit out by DIR is a bit different in evry version of winders; just to keep everybody off balance. Not to mention language/regional differences.
=====================================
If at first you don't succeed, you're about average.M2

Wow, thanks Mechanix. I just ran a little test and it seems that this will do exactly what I need. I have to admit that I don't quite understand the syntax on some of the lines and was hoping you could clarify what is actually going on here.
I've never seen this:
<#>##
I see a few temp files are created for each chop, but what is this actually saying?

The first two lines of the :chop subroutine:
find /i /v "volume in " <%1>#
find /i /v "volume serial " <#>##The first one takes the filename passed to it by the CALLing for and FINDs all line not containing "volume in " and puts the result in a file by the name of #. The choice of filenames # ## etc is arbitrary and we could just as well use any "non-reserved" characters. Like temp1 temp2 or aa bb ccc. I think you'll get the idea.
Does that help?
=====================================
If at first you don't succeed, you're about average.M2

![]() |
.exe to java
|
Variables in Batch Files
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |