0

I am writing a batch file that stores some root path (without the file name) in one variable. For example:

set ROOT="C:\Program Files(x86)\Some Program\"

In another part of the batch file I am iterating through all the EXE files contained in the root path and all it's subdirectories:

pushd !ROOT!
for /R "." %%F in (*.exe) do (
...
)

So, for example, lets say the program directory structure contains the following:

C:\Program Files(x86)\Some Program\prog.exe
C:\Program Files(x86)\Some Program\bin\run.exe
C:\Program Files(x86)\Some Program\bin\help\tuturial.exe
C:\Program Files(x86)\Some Program\lib\xfg.exe

Then the values for %%F in the for loop will be these 4 fully qualified paths.

What I want to do is get the path, starting from the root, up to the last directory name in %%F. For example, if %%F is:

C:\Program Files(x86)\Some Program\bin\help\tuturial.exe

and ROOT is:

C:\Program Files(x86)\Some Program\

how do I subtract the two variables to come up with the result:

\bin\help\

?

In other words:

     %%F = C:\Program Files(x86)\Some Program\bin\help\tuturial.exe
- !ROOT! = C:\Program Files(x86)\Some Program\
= -----------------------------------------------------------------
                                             \bin\help\

Thank you,

Jan

P.S. Note that I am using delayed expansion, hence the ! instead of % for variable ROOT.

UPDATE

I am getting really close here. I am able to do the string operation but with one condition, I have to "hard code" the substring that I want to remove. If I try to put the substring in a variable it won't work. Here is my batch file:

set ROOT="C:\Program Files(x86)\Some Program\"
....
pushd !ROOT!
for /R "." %%F in (*.exe) DO (
    set CURR=%%~dpF

    set "X1=!CURR:C:\Program Files(x86)\Some Program\=!"       

    set TEMP="C:\Program Files(x86)\Some Program\"
    set "X2=!CURR:!TEMP!=!"

    ECHO X1=!X1!
    ECHO X2=!X2!
)

And for the test case where CURR=C:\Program Files(x86)\Some Program\bin\help\tutorial.exe here is the output:

X1=bin\help\
X2=TEMP

So as you can see using a variable to hold the substring doesn't seem to work (I am probably doing something blatantly wrong though.)

Anyway, I will keep trying. :)

1 Answers1

1

Use %string:SEARCH=REPLACE% format in batch file. More information at: http://scripts.dragon-it.co.uk/scripts.nsf/docs/batch-search-replace-substitute!OpenDocument&ExpandSection=3&BaseTarget=East&AutoFramed

Sanket Tarun Shah
  • 637
  • 10
  • 28