A Windows XP help forum. PCbanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » PCbanter forum » Microsoft Windows 7 » Windows 7 Forum
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

I need help writing a position independent CMD script



 
 
Thread Tools Rate Thread Display Modes
  #1  
Old September 19th 17, 07:44 AM posted to alt.windows7.general
mike[_10_]
external usenet poster
 
Posts: 1,073
Default I need help writing a position independent CMD script

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.

Ads
  #2  
Old September 19th 17, 08:43 AM posted to alt.windows7.general
Paul[_32_]
external usenet poster
 
Posts: 11,873
Default I need help writing a position independent CMD script

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

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

  #3  
Old September 19th 17, 04:48 PM posted to alt.windows7.general
JJ[_11_]
external usenet poster
 
Posts: 744
Default I need help writing a position independent CMD script

On Mon, 18 Sep 2017 23:44:58 -0700, 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 forgot that the directories that the script is working on, are
position-dependent. While the script can know where it was running, it
doesn't know where the two of those directories are. That's up for the
script to determine that, when only the name of directories is known. Also,
without knowing the full path (which is position-dependent), although the
directory may be found by the script, it may actually be a different
directory.

To find the drive letter, you can use the FOR command. Since both
directories are in the root directory, it'll give you a little more specific
path for them which a `\dir1` and `\dir2`. e.g. the copyscript.bat

@echo off
setlocal
set drive=
for %%A in (c d e f g h i j k l m n o p q r s t u v w x y z) (
if exist %%A:\dir1 if exist %%A:\dir2 set drive=%%A
)
cd /d %drive%:\
rem put your code here...

Note that with above method, if both `dir1` and `dir2` are found in the root
directory of drive C: and E:, the script will use E:. i.e. last match is
used. If you want to use the first match, use below code.

@echo off
setlocal enabledelayedexpansion
set drive=
for %%A in (c d e f g h i j k l m n o p q r s t u v w x y z) (
if exist %%A:\dir1 if exist %%A:\dir2 if "!drive!" == "" set drive=%%A
)
cd /d %drive%:\
rem put your code here...

But both of above methods may still lead to the wrong drive letter, when the
drive letter order has been changed. So you'll need another identification
to indicate which drive to use. One method is to use the volume serial
number. e.g.

@echo off
setlocal
set serial=2D5D-3D04
set drive=
for %%A in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=2,5" %%B in ('vol %%A: 2^nul') do (
echo %%B %%C ?? %serial%
if "%%B" == "Serial" if "%%C" == "%serial%" (
if exist %%A:\dir1 if exist %%A:\dir2 set drive=%%A
)
)
)
if "%drive%" == "" (
echo Required directories are not found in any drive.
goto :eof
)
cd /d %drive%:\
rem put your code here...
  #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


  #5  
Old September 20th 17, 02:13 AM posted to alt.windows7.general
mike[_2_]
external usenet poster
 
Posts: 50
Default I need help writing a position independent CMD script

On 9/19/2017 8:48 AM, JJ wrote:
On Mon, 18 Sep 2017 23:44:58 -0700, 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 forgot that the directories that the script is working on, are
position-dependent.


The directories are fixed relative to the script location
in the same directory as the script as described.
That part works just fine.

While the script can know where it was running, it
doesn't know where the two of those directories are. That's up for the
script to determine that, when only the name of directories is known. Also,
without knowing the full path (which is position-dependent), although the
directory may be found by the script, it may actually be a different
directory.

To find the drive letter, you can use the FOR command. Since both
directories are in the root directory, it'll give you a little more specific
path for them which a `\dir1` and `\dir2`. e.g. the copyscript.bat

@echo off
setlocal
set drive=
for %%A in (c d e f g h i j k l m n o p q r s t u v w x y z) (
if exist %%A:\dir1 if exist %%A:\dir2 set drive=%%A
)
cd /d %drive%:\
rem put your code here...

Note that with above method, if both `dir1` and `dir2` are found in the root
directory of drive C: and E:, the script will use E:. i.e. last match is
used. If you want to use the first match, use below code.

@echo off
setlocal enabledelayedexpansion
set drive=
for %%A in (c d e f g h i j k l m n o p q r s t u v w x y z) (
if exist %%A:\dir1 if exist %%A:\dir2 if "!drive!" == "" set drive=%%A
)
cd /d %drive%:\
rem put your code here...

But both of above methods may still lead to the wrong drive letter, when the
drive letter order has been changed. So you'll need another identification
to indicate which drive to use. One method is to use the volume serial
number. e.g.

@echo off
setlocal
set serial=2D5D-3D04
set drive=
for %%A in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=2,5" %%B in ('vol %%A: 2^nul') do (
echo %%B %%C ?? %serial%
if "%%B" == "Serial" if "%%C" == "%serial%" (
if exist %%A:\dir1 if exist %%A:\dir2 set drive=%%A
)
)
)
if "%drive%" == "" (
echo Required directories are not found in any drive.
goto :eof
)
cd /d %drive%:\
rem put your code here...


 




Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off






All times are GMT +1. The time now is 12:27 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 PCbanter.
The comments are property of their posters.