0
votes

If I have my path set as follows:

C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\

I need it to be set as follows:

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\

I am using the following code to fetch the first parameter:

@echo off
setlocal EnableDelayedExpansion
set c=1
for %%A in ("%path:;=";"%") do ( 
set b[!c!]=%%~A
set /a c=c+1
)

echo !%b[1]%!
pause

But I cannot seem to find a way to cut the output string from the PATH variable.

2
So, which do you want to remove? "C:\Program Files (x86)\NVIDIA" or the first directory named in the path?jwdonahue
for /F "tokens=1* delims=;" %%a in ("%path%") do echo %%bAacini
@Aacini please send your comment as an answer so that I can close the ticket by marking it as an answer. Its exactly what I wanted.Tom Hagen
The for loop will launch another instance of cmd.exe in the background and isn't necessary to solve this problem.jwdonahue

2 Answers

1
votes

FOR /F command is your friend. tokens option lets you to take a certain part of a string and optionally to take the rest of the string, that is what you want:

for /F "tokens=1* delims=;" %%a in ("%path%") do echo %%b

Further details at any for /f description site, like this one.

1
votes

Please let me know if this is not sufficiently instructive:

@setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@set prompt=$G

@set "_literal=test (blah);"
@set "_tmpPath=%_literal%%path%"
@set _tmpPath
set "_tmpPath=!_tmpPath:%_literal%=!"
set _

Result:

> test
_tmpPath=test (blah);C:\Program Files...SNIPPED...

>set "_tmpPath=!_tmpPath:test (blah);=!"

>set _
_literal=test (blah);
_tmpPath=C:\Program Files...SNIPPED...

Cool thing is that if there were more than one "test (blah);" in the path, it would be removed, unless of course it was the last entry and the path didn't have a terminal semicolon. In which case you first strip the longer version, then the shorter version just to make sure.

Good enough?