Batch Files: What The Hell Am I Doing Wrong?

Okay. So, with the advent of GUIs being all over the place, I may have become a little bit too dependent on them. And, since it has been a while since I actually sat down and wrote a simple batch file, I may be a little rusty. But what the hell am I doing wrong in the following scenario:

Picture this. Two computers, and only two computers, connected via a simple crossover cable. They are in the same workgroup and can see each other fine. Their names are respectively Sugarloaf and Poindexter. One of the two computers, Sugarloaf, by the way, has a Zip drive that is properly installed and working fine.

The computer with the Zip drive, Sugarloaf, has a directory. Let’s say the path is: C:\Program Files\Horse Fever\Melancholy. Inside the Melancholy folder is several other folders and files. There are only two folders (and the files within) that I want to deal with. Those folders are respectively called Nasal Drip and Cholera.

So, the full paths of the two directories that I want the batch files to address are:

C:\Program Files\Horse Fever\Melancholy\Nasal Drip*.*

and

C:\Program Files\Horse Fever\Melancholy\Cholera*.*

and to ignore all everything else in the Melancholy directory.

I want to create two different batch files. The first batch file, will be on the desktop of the computer with the Zip drive, Sugarloaf. When it is double clicked, all it will do is copy those two directories, and all of the files inside them, to the Zip drive, which is designated by M:. Also, since these directories already exist on the Zip drive, I want the two newer directories to overwrite the older directories without receiving any error messages about folders and files that have the same name.

The second batch file will copy the very same two directories across the workgroup from Sugarloaf to Poindexter, overwriting the very same two directories on Poindexter, without any error messages or prompts as well. I may want to schedule this second batch file in the future, but for right now I just want it to work properly, once.

I understand about starting with a Text file, stacking the commands in order, and then renaming it to a Batch file. But I am doing something wrong. Please help me.

For openers, I’ve found that batch files that run from the command prompt (i.e., the DOS emulator in WinXP) don’t understand long file names. So, your source will have to be:

c:\progra~1\horsef~1\melanc~1\cholera*.*

Of course, if there are other directories that “resolve” to the same shortened names, you might have to fiddle around with ~1, changing it to ~2, or ~3, to get the right directory.

So, the final command might look like this:

copy c:\progra~1\horsef~1\melanc~1\cholera*.* m:\ /Y

where the “/Y” tells it to overwrite existing files without prompting you.

Hope this at least gets you on the right track - there may be other quirks you’ll have to deal with!

Assuming you have Windows XP or 2000:

First batch file:
xcopy “C:\Program Files\Horse Fever\Melancholy\Nasal Drip” “m:\Nasal Drip” /yei
xcopy “C:\Program Files\Horse Fever\Melancholy\Cholera” “m:\Cholera” /yei

(/y means force overwrite, /e means include subdirectories, even empty ones, /i means create the directories if they don’t exist)

Second batch file (assuming the share you want to copy to on Poindexter is called “C”) :
xcopy “C:\Program Files\Horse Fever\Melancholy\Nasal Drip” “\Poindexter\C\Nasal Drip” /yei
xcopy “C:\Program Files\Horse Fever\Melancholy\Cholera” “\Poindexter\C\Cholera” /yei

(quotation marks required because you have spaces in the names)
Personally, I would use the freeware utility XXCOPY rather than standard XCOPY, because, among other things, it allows you to keep the source and destination directories synchronised.

Great! You both have pointed out serious mistakes I was making. Thank you very much.

To elaborate, one of the machines is running XP, and the other one is running 98.

I see how the spaces in the names are a problem. I began to take those for granted.

Usram, so I can use the quotation marks to get around the spaces in the file names? And this XXCOPY, it is good stuff?

Not on the 98 machine, but that should work on the XP machine, I believe. 98 is actually DOS-powered, and DOS didn’t like long filenames. XP’s “DOS” prompt isn’t really running DOS at all.

No, Windows 98 and ME can handle long file names, with quotation marks as necessary.

XXCOPY can do clever stuff like incremental backups (i.e. only copying files that have changed) and synchronisation (making sure source and destination directories are identical). Trés useful for backups, but regular XCOPY is probably good enough for most purposes.

Hmmm. Interesting.

By the way, even though the standard installation doesn’t include it, there is, lurking on the WinXP Home CD, the NTBackup routine. That also has capabilities for doing incremental backups, differential backups, and so on. To install it, pop in the WinXP CD, navigate to VALUEADD/MSFT/NTBACKUP, and run the ntbackup.msi program to install it.

The readme file that’s on there points out that it’s no good for doing a complete system restore after a disaster (you have to reinstall WinXP manually, then use the backup archive you’ve created to bring back everything else). It also says that if you’re backing up to a CDRW, you need to create the backup archive file somewhere on your hard drive, then copy it to the CDRW - you can’t just point the backup routine to the CDRW directly. Of course, you can’t create a backup archive file that’s bigger than the CDRW capacity, either, since you won’t be able to copy it to the CD.

Early Out, I had a suspicion that NTBackup was lurking somewhere on the XP CD. It didn’t make sense that they wouldn’t atleast provide it for you, if you could find it. I just didn’t have the patience to go looking for it. That is good to know. But really, if you use your head and use the System Restore feature correctly, you really only have to backup a few important files. I think that this was Microsoft’s plan with XP - to make it more user friendly. NTBackup was a little buggy, atleast the copy I have on 2000 Pro.

So, this XXCOPY. I searched for it on download.com, with no luck. How would I get my hands on a copy of it?

From here.

The trouble with System Restore is that it does not backup your own data files, like Word docs, Excel spreadsheets, images you’ve saved, and so on. It only deals with system files, so that if you install a bad driver, for example, you can go back to the prior state. You still have to make your own backups of your own stuff - basically, the things you’d normally keep in the My Documents folder. At least it’s easy to find where they all are, with the exception of the files that Outlook keeps (either full or Express versions), which are buried deep.

Just so it’s clear, any Windows version from 95 on can handle long filenames just fine.

BUT, for use in bacth files or the command interpreter (the DOS-style interface) they have to be enclosed in quotes if they contain spaces.

So the smart batch writer always includes all filenames in spaces, especially if teh filename is coming frmoa paramter or environment variable. That way you don’t end up with a batch which works for some filenames and not others.

As a good practice, DON’T EVER use filenames like

c:\progra~1\horsef~1\melanc~1\cholera*.*

The problem is that if you copy the files out of a folder and back in, like the OP is doing, the 8.3 names may be different next time. so the same name ends up refering to two differtn files at two differetn times. Bummer if that means you end up with two copies of one backed up item and zero copies of another. Oops

The 8.3 names were a necessary workaround when Win95 first came out, but there;s no good reason to ever use them now unless you’re still running 16-bit software. And there’s damn little good reason to be doing that.

And Usram, plain old XCOPY frm Win2K and forward has those same options you mentioned for xxcopy. XCOPY (and a lot of other old “DOS” commands) have gained a lot of functionality over the years.

If you haven’t read the help on them recently, you may be missing some pretty powerful features you always wished you had in the bad old days. Path parsing, SET statements that do math, FOR-NEXT iterative structures, the list is pretty extensive.

Right you are! I used to run a network in which the PCs were running Win95, but the servers were running an old version of Netware (3.11), which could only handle 8.3 file names. Not pretty, to say the least.

Of course, that’s why I had completely missed the “enclose the names in quote marks” approach - I retired just after overseeing the conversion from Netware to NT Server 4.0, so my hands-on experience all dates from those dark, Netware times, when enclosing the file names in quote marks didn’t help anything!

Thanks for the infinitely better input.

XCOPY doesn’t preserve generated short file names correctly, which can render backups useless. XXCOPY does. And I’m not aware of any switch for XCOPY which would allow you to synchronise directories (i.e. delete files in the destination directory if they have disappeared from the source).

Allow me to retract part of that: XCOPY does preserve generated short file names if you use the /N switch. But the point about XXCOPY’s directory cloning abilities stands.

And allow me to retract my retraction :smack:. XCOPY /N uses the short file name only, so that’s no good.