View Single Post
  #4  
Old September 19th 17, 06:47 PM posted to alt.windows7.general
mike[_10_]
external usenet poster
 
Posts: 1,073
Default I need help writing a position independent CMD script

On 9/19/2017 12:43 AM, Paul wrote:
mike wrote:
I need help writing a position independent CMD script.

I'm helping a friend write a CMD script that is called by
another script in windows 7.

It does a lot of stuff, but the issue is encapsulated by the following...

Directory structure is
D:\
program root directory
dir1
somefiles
dir2
copyscript.bat

The copyscript.bat is one line

xcopy dir1\*.* dir2


The script works fine when executed from program root directory.
But it's called from a script on another partition.
Xcopy can't find dir1 or dir2. It's looking in system32.

The current workaround is to add the following

D:
cd pathto program root directory

That fixes the problem, but it's position dependent.

How do we modify the script so that it runs no matter
where program root directory is? How can the script
determine it's location?

In the current context, it's executed from task scheduler.
Task scheduler allows inputting the working directory.
That's not the question.
Ditto for calling the script thru a shortcut.

I want to learn the path syntax for writing a self-contained
position-independent
CMD script that uses relative addressing in the version of CMD
interpreter
that's in win7.
Explanations and links are fine, but the ideal answer would also edit
the above one-line script to make it position independent. The devil is
in the details.

I've already googled my ass off. I'm lost in escape sequences
and parentheses and pre-defined system variables and
the fact that CMD lines may parse differently if they're in a script.
Using a different scripting language won't help. I'd have to learn it
and teach it and become "customer support" forever. That's way
beyond what I'm willing to do.


You need the feature that determines where the caller came from.
This is supported on various platforms (the concept is used widely).
The question is, is it supported in *your* situation.

https://blogs.technet.microsoft.com/...pt-is-running/


So let's try it another way. "which" is used in Unix, to find the
first instance of executable in $PATH that matches.

https://stackoverflow.com/questions/...s-command-line


"anything after Windows XP 32 bit) provide the where.exe program"

*******

https://stackoverflow.com/questions/...file-directory


cd /d %~dp0


That was one of the things I tried. I was trying to use that
prepended to each of the command path arguments and couldn't get
the syntax right.
Your link is much clearer than the ones I found.
Using it as you suggest to CD seems to work.
Thanks.

Perhaps that line could be put inside "copyscript.bat" ?

And later...

https://technet.microsoft.com/en-us/.../bb491001.aspx

Use setlocal to change environment variables when you run a batch file.
Environment changes made after you run setlocal are local to the
batch file.

So if you wanted to cd before running a batch file, there's
presumably a way to pass the working directory. I would
have thought it would already be doing that.

Anyway, there are ingredients out there...

Paul


Ads