Batchfiles Index

Batch mismatch

Batch of the Day

Password protection in a Batch File

 

 

Batch mismatch

A couple of months ago you told us how to create a batch file ('Saving Grace', Shopper 156). I have been attempting to do this, but saving only My Documents/Work to my Zip drive (drive F:). This is so I can automatically put my work documents on a zip drive to take to my machine at work. I have followed the instructions using the line in Dos Edit:

XCopy C:\My~Documents\Work\*.* F: /E /Y

When I try to run it I get the message 'file cannot be found -*.*

Will this not work to a Zip drive or am I doing something wrong?

It's those long Windows filenames that are causing the trouble. I see you found that typing a space in a folder name won't work in Dos -you tried replacing it with the tilde character ( ~). That's wrong.

The trick is that if a long filename includes a space you enclose it in quotes:

XCopy "C:\My Documents\Work\*.*" F: /E /Y

An alternative is to use the short name Dos allocates; for example:

XCopy C:\MyDocu~1\Work\*.* F: /E /Y

Another method, perhaps better than Dos batch commands, is to use the Windows Briefcase feature.

 

 

 

Batch of the day

I long ago fell out with Microsoft Backup routines and so wrote myself a suite of Dos batch files that use PKZip and a corresponding PKUnzip to locate, extract and recover any lost files. The backups have been done using the son/father/grandfather system on to floppies. This has served me exceedingly well for the past 10 years. As each backup overwrites the previous one on the floppy, I run the batch file from an icon on my desktop.

I have recently added a CD-RW to the system and find the additional storage space capable of keeping each backup rather than overwriting the old files. I now add a parameter to the batch file to give the resultant Zip file the current date (for example, Dos> FullBack 24092000 runs the batch file FullBack.bat and results in 24092000.zip).

Is it possible to have a desktop icon to run a Dos batch file to which I can add a variable parameter, or will I always have to go to the Dos prompt and type the batch file name plus the parameter as above?

No, an icon won't prompt you for a parameter. You would either have to run a Dos program that asks you to input the parameter or, if you simply want the date, you can do some Dos batch file 'magic' to add the date to the command line.

While Dos didn't provide a simple way to get the current date in a batch file, there are two ways of doing this: either route the output of a DIR command to a file or route the output of the Date command to a file.

Either way you use the output file as a batch file: if the first word on the line containing the date is the name of another batch file, the rest of the line will be supplied as parameters so the date can be used in that batch file. While this is somewhat longwinded, it has to be set up only once and then is easy to use. The English style of dates are separated by a slash (/), which is not legal in Dos filenames. So first you'll need to go into Control Panel, Regional Settings and change the date delimiter to a hyphen (-) instead. This will take effect after you reboot.

The DIR command produces several unwanted header lines, so I'll use the Date command as an example. To prepare, create the following files:

EMPTY.TXT -which contains an empty line (which must have a carriage return code on the end of it -if you do a DIR on this, it should be two bytes long)

SETTODAY.BAT -which contains the following two lines:

date >$l.bat <empty.txt0

$1.bat0

CURRENT.BAT -which has just one line:

SET TODAY=%4%0

Then add the line:

CALL SETTODAY0

to your Autoexec.bat file, so it runs on Windows startup.

The way it works is that the Date command produces output such as:

Current date is Sat 24-02-2001 Enter new date (dd-mm-yy):

The line 'date >$1.bat <empty.txt' tells it to store this output in $1.BAT and take input from EMPTY.TXT, which prevents it waiting for the user to enter a date. The second line executes $1.BAT. When executed, this finds the line:

Current date is Sat 24-02-2001

which causes it to look for a program or batch file called Current. It finds Current.bat and supplies it with each of the other words on the command line as parameters. So %3% will contain the day and %4% the date. The command 'SET TODAY=%4%' will then create a Dos environment variable containing the current date and your batch files can use %TODAY% as part of a filename.

 

 

 

Password protection in a batch file

I want to add a password prompt to a simple batch file. The prompt should ask users for a three-character password before executing the rest of the batch file. Can you tell me how to do this?

Save the following text as Getkey.scr, making sure to include the blank lines before RCX and after Q:

N GETKEY.COM A 100

MOV AH,O INT 16

MOV AH,4C INT 21

RCX

8 W Q

Now enter the command Debug&lt;Getkey.scr to create Getkey.com. Then place Getkey.com in a folder on your path. Get key waits for a keystroke and returns the corresponding ASCII code.

The following batch file uses Get key to detect the password AOK (case-sensitive).

Top

ECHO Enter password.

SET K=

GET KEY

IF ERRORLEVEL 66 GOTO Not1 IF ERRORLEVEL 65 SET K=%K%x :Not1

ECHO *

GET KEY

IF ERRORLEVEL 80 GOTO Not2 IF ERRORLEVEL 79 SET K=%K%q :Not2

ECHO **

GET KEY

IF ERRORLEVEL 108 GOTO Not3 IF ERRORLEVEL 107 SET K=%K%j

:Not3

ECHO ***

IF '%K%.=='xqj' GOTO OK GOTO Top

:OK

ECHO Password OK.

Consult an ASCII table and change the numbers to select a different password.