cmd files

D

Dond13

I would appreciate some help with a problem I am having. I have some cmd
files that I have written to backup specific folders to a flash drive. I
want to be able to change the physical drive using either an entry calling
the cmd file or imbed a command within the file to be able to change the
physical address of the drive. Sometimes I use F: G: or H: and don't feel
it is necessary to have separate cmd files for each drive. I have been a
programmer in another life, but am an old fart now and can't get it done as
I could in the past. I just need help. :).
 
S

Sunny Bard

Dond13 said:
I would appreciate some help with a problem I am having. I have some
cmd files that I have written to backup specific folders to a flash
drive. I want to be able to change the physical drive using either an
entry calling the cmd file or imbed a command within the file to be able
to change the physical address of the drive. Sometimes I use F: G: or
H: and don't feel it is necessary to have separate cmd files for each
drive. I have been a programmer in another life, but am an old fart now
and can't get it done as I could in the past. I just need help. :).
If you call the .cmd file from a shortcut, or from a cmd.exe window,,
you can pass the F: G: or H: argument on the command line, then within
the .cmd file replace your hard coded drive letter with %1
 
B

Bob I

Use a generic target in the cmd file and then put the cmd file on the
flash drive and run it from there.
 
S

Stan Brown

I would appreciate some help with a problem I am having. I have some cmd
files that I have written to backup specific folders to a flash drive. I
want to be able to change the physical drive using either an entry calling
the cmd file or imbed a command within the file to be able to change the
physical address of the drive. Sometimes I use F: G: or H: and don't feel
it is necessary to have separate cmd files for each drive. I have been a
programmer in another life, but am an old fart now and can't get it done as
I could in the past. I just need help. :).
I am confident of the below, modulo any typos, but I haven't tested
it so you should. :)

1. In your existing cmd file, change all the f: or g: or h: to %1:
(that's percent, 1, colon).

2. Insert these lines at the beginning of the file:
@echo off
if "%1" == "f" goto 00_Start
if "%1" == "g" goto 00_Start
if "%1" == "h" goto 00_Start
echo Specify drive letter f, g, or h.
goto 99_Exit
:00_Start
echo on <-- add this line if you wish

3. Append this line at the end of the file:
:99_Exit

4. Call your file by "filename f", "filename g", or "filename h". The
lines in 2. above will catch a mistake.
 
D

Dond13

Bob I said:
Use a generic target in the cmd file and then put the cmd file on the
flash drive and run it from there.
That's what I had in mind, and I do call the .cmd file from a shortcut. I
have one icon for backup and another for restore. I don't know how to pass
the argument to the .cmd file. I must be missing something basic. I
understand that I would write the .cmd file with %1 signifying the drive
letter, and I can see how to do that using a cmd window but how do I pass
the argument to the file called by the shortcut?

I might not be making myself clear, but I have a file 'Backup.cmd' and have
created an icon that will execute the file when I click on that icon.
Sounds basic but I'm lost as to how to accomplish passing the parameter to
specify which drive I want to backup. Oh to be 60 again - or even 70.
 
S

Seth

Dond13 said:
That's what I had in mind, and I do call the .cmd file from a shortcut. I
have one icon for backup and another for restore. I don't know how to
pass
the argument to the .cmd file. I must be missing something basic. I
understand that I would write the .cmd file with %1 signifying the drive
letter, and I can see how to do that using a cmd window but how do I pass
the argument to the file called by the shortcut?

I might not be making myself clear, but I have a file 'Backup.cmd' and
have
created an icon that will execute the file when I click on that icon.
Sounds basic but I'm lost as to how to accomplish passing the parameter to
specify which drive I want to backup. Oh to be 60 again - or even 70.
2 ideas for you, both of which will work.

Put a specific "marker" file in the root of the USB drive (or drives) that
are appropriate for receiving backups. Now modify your backup script to go
through available/used drive letters looking for said file. backup to drive
that contains that file.

Or, put your USB drive in. Go into Disk management and manually change the
drive letter of the USB drive to something high up in the alphabet (when
Windows is auto-assigning drive letters it does it in order) so it will
always be the same letter (but not all the way to Z as when mapping network
drives Windows starts at Z and works it's way backwards). Now modify your
backup script to use that specific drive letter.
 
E

Ed Cryer

That's what I had in mind, and I do call the .cmd file from a shortcut. I
have one icon for backup and another for restore. I don't know how to pass
the argument to the .cmd file. I must be missing something basic. I
understand that I would write the .cmd file with %1 signifying the drive
letter, and I can see how to do that using a cmd window but how do I pass
the argument to the file called by the shortcut?

I might not be making myself clear, but I have a file 'Backup.cmd' and have
created an icon that will execute the file when I click on that icon.
Sounds basic but I'm lost as to how to accomplish passing the parameter to
specify which drive I want to backup. Oh to be 60 again - or even 70.
There's no way to do it with just the simple icon method you're using.

Best alternative is to have several commands in a folder;
BackuptoG.cmd
BackuptoH.cmd
etc.

If you want you can then create icons for each one; DoG, DoH etc.

Similarly for restores.

Ed
 
S

Seth

Ed Cryer said:
There's no way to do it with just the simple icon method you're using.
See the reply I sent the OP for 2 ways it can be done with a single icon/CMD
file.
 
D

Dond13

Ed Cryer said:
There's no way to do it with just the simple icon method you're using.

Best alternative is to have several commands in a folder;
BackuptoG.cmd
BackuptoH.cmd
etc.

If you want you can then create icons for each one; DoG, DoH etc.

Similarly for restores.

Ed
That is the way I am doing it now. I just thought that there was an easy
way to receive keyboard input within the cmd file. I thought that I
remembered from the distant past that you could do this but I have worked on
so many different systems and with so many different languages that I might
be thinking about my days with IBM mainframes, or some of the languages I
have used in the past. I would like to thank all who have made suggestions,
but I'll just continue with several .cmd files - one for each drive that I
use. I use several because I make backups to a CF, SD and to an external
hard drive. I don't think that I can have enough backups of the files I am
talking about. Thanks!!
 
D

Dond13

Seth said:
2 ideas for you, both of which will work.

Put a specific "marker" file in the root of the USB drive (or drives) that
are appropriate for receiving backups. Now modify your backup script to
go through available/used drive letters looking for said file. backup to
drive that contains that file.

Or, put your USB drive in. Go into Disk management and manually change the
drive letter of the USB drive to something high up in the alphabet (when
Windows is auto-assigning drive letters it does it in order) so it will
always be the same letter (but not all the way to Z as when mapping
network drives Windows starts at Z and works it's way backwards). Now
modify your backup script to use that specific drive letter.
Seth, I can see how your solution would work but I was looking for something
a little more direct. The steps required for this would be a little
cumbersome and would have some unintended consequences if I goofed for
instance and didn't reset Disk management after finishing. The multiple
icons for several .cmd files will continue to work and I just have to ensure
that each one is edited if I add or delete anything I want to backup.
Thanks!
 
S

Sunny Bard

Dond13 said:
I might not be making myself clear, but I have a file 'Backup.cmd' and have
created an icon that will execute the file when I click on that icon.
Sounds basic but I'm lost as to how to accomplish passing the parameter to
specify which drive I want to backup. Oh to be 60 again - or even 70.
Right click the shortcut, properties, change the "target" so that it
inncludes your F: G: etc

you might need to put double quotes round it, e.g

"C:\Whatever folder\backup.cmd " F:

sorry not in front of a windows PC right now, but you should get the idea.
 
S

Sunny Bard

Ed said:
There's no way to do it with just the simple icon method you're using.
Best alternative is to have several commands in a folder;
BackuptoG.cmd
BackuptoH.cmd
Rubbish!
 
E

Ed Cryer

That is the way I am doing it now. I just thought that there was an easy
way to receive keyboard input within the cmd file. I thought that I
remembered from the distant past that you could do this but I have
worked on so many different systems and with so many different languages
that I might be thinking about my days with IBM mainframes, or some of
the languages I have used in the past. I would like to thank all who
have made suggestions, but I'll just continue with several .cmd files -
one for each drive that I use. I use several because I make backups to a
CF, SD and to an external hard drive. I don't think that I can have
enough backups of the files I am talking about. Thanks!!
I presume that the "icon" is merely a Windows shortcut that you've
created for the .bat file and then changed the picture.

Seth's method of changing the target would work, but (and I guess both
you and I have experience of writing software for "other users") it
takes attention whenever used, is easy to get wrong, quite difficult to
teach to Joe Soap, is not user-friendly.
I worked with ICL mainframes; systems designer, analyst-programmer,
sometime system programmer; and I guess we think alike, having had to
earn our bread with it.

With the old ICL George and VM OSs the macros could all take string
parameters; things like %(driveletter). That was probably true of IBM as
well.

Ed
 
S

Seth

Dond13 said:
Seth, I can see how your solution would work but I was looking for
something a little more direct. The steps required for this would be a
little cumbersome and would have some unintended consequences if I goofed
for instance and didn't reset Disk management after finishing. The
multiple icons for several .cmd files will continue to work and I just
have to ensure that each one is edited if I add or delete anything I want
to backup.
You misunderstand... It's a 1 time process (insert USB stick, go into disk
management and change letter to S:, done). Now, as long as "S:" isn't
occupied (which is unlikely as it is far enough away from Z and C), that USB
stick will always come up as S: when inserted. What part is cumbersome and
where do you have to reset something in Disk management?

And also don't ignore the other method of making (1 time) a "marker" on the
USB drive and having your backup script find the marker.
 
Z

Zaphod Beeblebrox

Dond13 said:
That is the way I am doing it now. I just thought that there was an
easy way to receive keyboard input within the cmd file. I thought
that I remembered from the distant past that you could do this but I
have worked on so many different systems and with so many different
languages that I might be thinking about my days with IBM
mainframes, or some of the languages I have used in the past. I
would like to thank all who have made suggestions, but I'll just
continue with several .cmd files - one for each drive that I use. I
use several because I make backups to a CF, SD and to an external
hard drive. I don't think that I can have enough backups of the
files I am talking about. Thanks!!
Why not use the CHOICE command? It would look something like this:

CHOICE /C:FGHIN /N Backup to drive F:, G:, H:, I: (or N for None)?
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
IF ERRORLEVEL 5 SET DRIVE=
ECHO Backing up to %DRIVE%

When run, it will prompt "Backup to drive F:, G:, H:, I: (or N for
None)?" and will then wait for you to press a key, return an
errorlevel for the key based on the order of the /C: parameter, and
sets the variable DRIVE to the drive letter. You can then just use
%DRIVE% in your backup routine instead of a hard-coded F:, etc.
 
E

Ed Cryer

Why not use the CHOICE command? It would look something like this:

CHOICE /C:FGHIN /N Backup to drive F:, G:, H:, I: (or N for None)?
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
IF ERRORLEVEL 5 SET DRIVE=
ECHO Backing up to %DRIVE%

When run, it will prompt "Backup to drive F:, G:, H:, I: (or N for
None)?" and will then wait for you to press a key, return an
errorlevel for the key based on the order of the /C: parameter, and
sets the variable DRIVE to the drive letter. You can then just use
%DRIVE% in your backup routine instead of a hard-coded F:, etc.
Could this be run outside of DOS? I mean, could you set up a BAT file
with this CHOICE embedded, and then give the answer from inside Windows?

Ed
 
Z

Zaphod Beeblebrox

Gene E. Bloch said:
Yes.

But the command window that pops up will go away after you make a
choice...unless you run the backup program using "cmd /k" or "cmd
/c".

Note that the first line of the batch file above needs to be
changed:
CHOICE /C FGHIN /N /M "Backup to drive F:, G:, H:, I: (or N for
None)?"
The /M and the quotes are required; the colon after /C is ok,
though.

Here's a slightly prettier (more informative) version of the batch
file:

<Bat File>
@echo off

CHOICE /C FGHIN /N /m "Backup to drive F:, G:, H:, I: (or N for
None)?"
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
if errorlevel 5 (
echo No backup drive chosen
echo/
goto :EOF
)

ECHO Backing up to %DRIVE%
echo/
REM Put your code here, as in
cmd /k backup_code
</Bat File>

Weirdly enough, if you save this as a cmd file, the errorlevel tests
above have to be in the other order, i.e., test for 5 first, then
4,...

That struck me as so unlikely that I spent too much time setting up
a
file where I could control the test order from the command line, and
sure enough, the batch file has to test in one order and the command
file in the other.

BTW, everything in that file is case-insensitive, so most of my
changes
are in lower case, for visibility.
You shouldn't have to reverse the order, unless you change from SET
DRIVE=F: to something like GOTO DRIVE_F. The reason you would need to
for the GOTO version is that errorlevel tests are not really an equal
to, rather they are greater than or equal to, so a test for
ERRORLEVEL=0 is always true (unless there is some way to generate a
negative errorlevel, that is) and ERRORLEVEL=1 is true for any
errorlevel greater than 0, etc.

The above, of course, is my understanding of how it works and my
experience with batch files under DOS, XP and Vista, and is not based
on actual testing with a CMD file under Windows 7, so I'll be happy to
be proven wrong if in fact my understanding is inaccurate.

BTW, a great resource for CMD and batch scripting is
alt.msdos.batch.nt (.nt is taken to mean any Windows version from NT
and following, so would include Vista and Win7, btw). If the guys
posting in there can't do it, it can't be done!

--
Zaphod

Arthur Dent, speaking to Trillian about Zaphod:
"So, two heads is what does it for a girl?"
"...Anything else he's got two of?"
 
E

Ed Cryer

Yes.

But the command window that pops up will go away after you make a
choice...unless you run the backup program using "cmd /k" or "cmd /c".

Note that the first line of the batch file above needs to be changed:
CHOICE /C FGHIN /N /M "Backup to drive F:, G:, H:, I: (or N for None)?"
The /M and the quotes are required; the colon after /C is ok, though.

Here's a slightly prettier (more informative) version of the batch file:

<Bat File>
@echo off

CHOICE /C FGHIN /N /m "Backup to drive F:, G:, H:, I: (or N for None)?"
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
if errorlevel 5 (
echo No backup drive chosen
echo/
goto :EOF
)

ECHO Backing up to %DRIVE%
echo/
REM Put your code here, as in
cmd /k backup_code
</Bat File>

Weirdly enough, if you save this as a cmd file, the errorlevel tests
above have to be in the other order, i.e., test for 5 first, then 4,...

That struck me as so unlikely that I spent too much time setting up a
file where I could control the test order from the command line, and
sure enough, the batch file has to test in one order and the command
file in the other.

BTW, everything in that file is case-insensitive, so most of my changes
are in lower case, for visibility.
This worked here, saved as a BAT file and then clicked on.

Two points;
1. the REM has to be repeated for each line, otherwise, as here,
"backup_code" gets taken as a command.
2. It could do with putting control back to the user before going ahead
and doing its stuff.
Perhaps;
CHOICE /C YN /N /m "Proceed as displayed (Y or N)?"
IF ERRORLEVEL etc

Ed
 
E

Ed Cryer

This worked here, saved as a BAT file and then clicked on.

Two points;
1. the REM has to be repeated for each line, otherwise, as here,
"backup_code" gets taken as a command.
2. It could do with putting control back to the user before going ahead
and doing its stuff.
Perhaps;
CHOICE /C YN /N /m "Proceed as displayed (Y or N)?"
IF ERRORLEVEL etc

Ed
This is my user-friendly version.
I'd like your opinion of this, Dond13.

---------------------------------------

@echo off

CHOICE /C FGHIN /N /m "Backup to drive F:, G:, H:, I: (or N for None)?"
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
if errorlevel 5 (
echo No backup drive chosen
echo/
pause
goto :EOF
)

echo/
ECHO Backing up to %DRIVE%
echo/
CHOICE /C YN /N /m "OK to proceed? Y or N?"
IF ERRORLEVEL 2 (
pause
GOTO :EOF)

REM Place batch progs here
REM
CD C:\Program Files (x86)\RegCleaner
Regcleanr.exe
pause
 
C

Char Jackson

This is my user-friendly version.
I'd like your opinion of this, Dond13.

---------------------------------------

@echo off

CHOICE /C FGHIN /N /m "Backup to drive F:, G:, H:, I: (or N for None)?"
IF ERRORLEVEL 1 SET DRIVE=F:
IF ERRORLEVEL 2 SET DRIVE=G:
IF ERRORLEVEL 3 SET DRIVE=H:
IF ERRORLEVEL 4 SET DRIVE=I:
if errorlevel 5 (
echo No backup drive chosen
echo/
pause
goto :EOF
)

echo/
ECHO Backing up to %DRIVE%
echo/
CHOICE /C YN /N /m "OK to proceed? Y or N?"
IF ERRORLEVEL 2 (
pause
GOTO :EOF)

REM Place batch progs here
REM
CD C:\Program Files (x86)\RegCleaner
Regcleanr.exe
pause

--------------------------------
Just curious, why did you add the regcleanr.exe program to this batch
file? It doesn't seem to be a standard Windows file (it's not on my
Win7 system) and I'm not sure what it does nor how it's related to the
overall backup task. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top