You are here

.BAT File vs Changing drive letters

26 posts / 0 new
Last post
IvanEBC
Offline
Last seen: 13 years 10 months ago
Joined: 2008-08-04 23:07
.BAT File vs Changing drive letters

This was origianlly posted elsewhere but after someone pointed me here .... i decided to try here .. and oh boy .. what a great app this is and i'm going to use it to make my little program run in a "cooler" way hehehe
I don't need my USB stick for firefox and all the other great options although i will investigate that further ... instead .. i want to run a small .bat script that does a few automated tasks for me ... cleans a folder on the USB stick, then copies files to the stick automatically.
I actually have the script up and running and so far it works ... then i hit a hitch .... moving the stick to another of my computers and the drive letter changes.... you can proabbly already guess the issue ... but here is what i orginaly write.

Hi Guys and Gals

Little puzzle for you.

Simply this .. making a tiny DOS script that does a CD command to a USB stick and delete some files from it and copy new files to it .... so it kinda looks like this

echo cleaning your stick
del h:\backup\*.*
copy c:\pictures\*.* h:\backup\*.*

Now .. this actually works .....

BUT ... if i put this script (running from the autobat on the USB stick on another computer... the drive letter changes and could cause a massive amount of harm .. hehehe you can see why. I thought my solution was simple...

rename the USB stick so it is called USB
In my computer, the USB stick shows up as USB: (H:)

I adjust my .bat to say

echo cleaning your stick
del USB:\backup\*.*
copy c:\pictures\*.* h:backup\*.*

But it can't find USB:

What other solution is there? Why can't i see USB: i thought this was a standard windows command. I'm using Vista command prompt and hope this will work on any XP or vista machine as i have several machines here and want to make simple and easy backups.

And i'll iuse portable app to late turn the bats into .exe and click on the backup i want to make Smile

Any advice on how to fix the .bat to follow the drive letters around?

Thanks for any help

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
There are 2 things you can

There are 2 things you can do:
The first is using %cd% environmental variable - it contains a path to your current directory, which is usually your script's directory (*). Because it's not always - I don't recommend it.
The second thing requires at least Windows NT4, but is correct.
%~d0 contains your drive letter with a colon (i.e. "h:")
%~dp0 contains full path of your script's directory (i.e. "h:\scripts\").

Ad.(*): When you double click on a .bat, you have no problems. But .i.e when you call a batch from another one, which is located somewhere else or - obviously - when your batch (or batch that called yours) executed cd / chdir command, current directory changes...

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

Bahamut
Bahamut's picture
Offline
Last seen: 12 years 5 months ago
Joined: 2006-04-07 08:44
AFAIK, that only works in a

AFAIK, %~dx and friends only work in a for loop.

Vintage!

richo
richo's picture
Offline
Last seen: 6 days 18 hours ago
Joined: 2007-01-31 22:03
Re: %~ Modifiers

Bahamut, %~dx and friends work in for loops, if statements and batch parameters.
Not sure about variables set using the SET command.

Bahamut
Bahamut's picture
Offline
Last seen: 12 years 5 months ago
Joined: 2006-04-07 08:44
What exactly are batch

What exactly are batch parameters?
And how would it work in an if statement?

Vintage!

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
http://www.robvanderwoude.com

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

Bahamut
Bahamut's picture
Offline
Last seen: 12 years 5 months ago
Joined: 2006-04-07 08:44
Arguments. To-may-to,

Arguments.
To-may-to, to-mah-to.

Would you post an example for %~dx in an if statement? I absolutely cannot get it to work outside of a for loop. The variable is simply undefined.

Vintage!

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
@echo off if not

@echo off
if not "%~nx0"=="a.bat" echo You renamed my batch! :((

If you want exactly drive letter, then:
@echo off
if not "%~d0"=="C:" echo I'm not on C

I'm not sure how about casing, on my PC it's capital letter but is it always like this??

Edit: More funny version of the first batch (untested):
@echo off
if not "%~nx0"=="a.bat" (
copy "%~dpnx0" "%~dp0a.bat"
call "%~dp0a.bat" %*
del /y "%~dpnx0"
)

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

Bahamut
Bahamut's picture
Offline
Last seen: 12 years 5 months ago
Joined: 2006-04-07 08:44
It works now. Too bad I have

It works now. Too bad I have no idea why it wasn't working before.

Vintage!

richo
richo's picture
Offline
Last seen: 6 days 18 hours ago
Joined: 2007-01-31 22:03
Casing

to make the if statement ignore casing, just add the /I parameter.
Eg: if /I not "%~d0"=="C:" echo I'm not on C

Ed_P
Offline
Last seen: 5 years 6 months ago
Joined: 2007-02-19 09:09
If you're running the .bat

If you're running the .bat from the USB stick you don't need to specify the stick's drive letter.

echo cleaning your stick
del \backup\*.*
copy c:\pictures\*.* \backup\

will work just find regardless of where you plug it in.

hth

Ed

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
That's exactly the same thing

That's exactly the same thing as %cd%\backup\*.*
And has the same drawbacks.

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

Ed_P
Offline
Last seen: 5 years 6 months ago
Joined: 2007-02-19 09:09
Not really.

No system variable, therefore nothing to ever be changed. The bat will always execute from the location it is in and if it's on the USB drive it will always execute from there regardless of drive letter.

Try it, you'll see.

Ed

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
There's nothing about USB

There's nothing about USB here, it works exactly the same everywhere.
%cd% is not a system, but command processor variable.
And I'll say again - if you start a batch directly by double clicking, there's no issue, but if you want to call it from another one or from a program or from a command prompt, it can be different.
Try:
a.bat:
@echo off
md tst
cd tst
call %~dp0b.bat
cd..
rd tst

b.bat:
if exist b.bat (echo I'm in my own directory.) else (echo Where am I?)
pause>nul

Place them in the same directory and execute a.bat.
Note: They need at least NT4.

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

Ed_P
Offline
Last seen: 5 years 6 months ago
Joined: 2007-02-19 09:09
gad

%cd% is not a system, but command processor variable.

Whatever. Picky, picky, picky. Smile

start a batch directly by double clicking, there's no issue

Exactly. And isn't that what the OP is talking about?

but if you want to call it from another one or from a program or from a command prompt, it can be different.

Agreed but I don't think that is the issue here. However if the OP is talking about a .bat file being executed from the PAM then some minor tweaking of the .bat code might be required. Add a cd \ or cd ..\ etc depending on the folder location of the .bat. The drive letter would still not be an issue.

Ed

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
%cd% is not a system, but

%cd% is not a system, but command processor variable.

Whatever. Picky, picky, picky. Smile
Actually here it's quite important as system variables don't change on their own and this one does, even during runtime.
Agreed but I don't think that is the issue here. However if the OP is talking about a .bat file being executed from the PAM then some minor tweaking of the .bat code might be required. Add a cd \ or cd ..\ etc depending on the folder location of the .bat. The drive letter would still not be an issue.

..And a month later decide to tweak your directory structure and rewrite 20 batches that you created since then.
And add a note at the start of each .bat
:: This script assumes to be run from own directory, use with care.
because you can easily forget about it ( ok, maybe not in 1 month Wink )
This can be done to work easily, but why not use a better solution? If OP deals with Win 9x then OK, there's probably no better way. But otherwise i.e. starting a batch with cd %~dp0 improves manageability and in case of using several batches dealing with each other - bug proofness.

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

Unemployed Stor...
Offline
Last seen: 11 years 8 months ago
Joined: 2007-12-18 22:35
.

If you are running the batch file of your USB Drive then replacing the USB sticks drive letter with the %HOSTDRIVE% variable in the batch file should work.

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
?? I never heard of

??
I've never heard of it...neither google did. Also, on my system such variable does not exist.
Do you run batches via customized command processor?

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

Unemployed Stor...
Offline
Last seen: 11 years 8 months ago
Joined: 2007-12-18 22:35
?

Could you be a little more specific ? Did it give you an error message such as directory not found ? What was your exact usage of the variable ? And know I dont use any special command interpretor just the stranded windows command prompt.

IvanEBC
Offline
Last seen: 13 years 10 months ago
Joined: 2008-08-04 23:07
Hey Guys - thanks for your

Hey Guys - thanks for your replies ...

amazing .. the first reply was all i needed... the script is running from the USB stick

So firstly .. thankyou .. the script is behaving .. and as for the other replies .. heheh well i can't answer that.

I still have certain issues. I would love to run the .BAT from the portableapps program .. and yeah i saw the .BAT to .EXE creator ... but when i create an .EXE ... the script no longer works.... obviously there isn't much to do here to get it running, the .exe creator either works or doesn't.

I'll ask more stuff later, but thanks for the replies .. appreciated.

Ivan

muldrec
Offline
Last seen: 13 years 10 months ago
Joined: 2008-06-20 12:02
I have the same problem ....

The script does not work once the batch file is wrapped in an executable (.EXE).

Any thoughts on this problem is appreciated.

m2
Offline
Last seen: 13 years 3 months ago
Joined: 2006-12-12 12:00
I simply executed set

I simply executed
set HOST
And it told me there's no such variable(which means there's no env. var. with name starting with host).
What OS do you use? Mine is XP x64.
Can you find any reference that tells what it is?
Who told you about it?

"Those people who think they know everything are a great annoyance to those of us who do." Asimov

abushcrafter
abushcrafter's picture
Offline
Last seen: 13 years 8 months ago
Joined: 2008-05-12 13:39
Even better (I think) is.....

Even better(I think) is:
(example is for a portable shortcut)

cd / rem top of current drive bat is on.
start FULL\path\to\file.file
rem "start" means launcher file ruffly.

dark_yux
Offline
Last seen: 10 years 3 months ago
Joined: 2007-10-23 11:23
I would use this.
echo
del %drive%\backup\*.*
copy c:\pictures\*.* %drive%\backup\*.*

or if you want to get fancy Smile

echo
del ..\backup\*.*
copy c:\pictures\*.* ..\backup\*.*
rem And the dots will work!

self.path = path if self.path == None else self.path

Ed_P
Offline
Last seen: 5 years 6 months ago
Joined: 2007-02-19 09:09
fancy? maybe

..\ only works depending on where the bat is located. I have some files on my sticks with ..\..\..\..\Documents\Switch.cmd in them.

So as long as you know where the bat is relative to the root ..\s work very effectively.

Ed

EspaÑaks (not verified)
I use a batch file (well I use

I use a batch file (well I use seven) for launching different groups of programs. I solved that thing of the drive letter by NOT putting anything in the drives letter. for example, instead of

start z:\startportableapps.exe
stert z:\portableapps\keepassportable\keepass.bat

I'll use

start \startportableapps.exe
stert \portableapps\keepassportable\keepass.bat

I think the difference is appreciable. I think something similar could be applyed to the commands u want so it'll be:

echo cleaning your stick
del \backup\*.*
copy c:\pictures\*.* h:\backup\*.*

THIS ONLY WORKS WHEN THE BATCH IS IN THE DRIVE WITHOUT LETTER

Log in or register to post comments