View Single Post
  #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...


Ads