0

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.

4
  • So, which do you want to remove? "C:\Program Files (x86)\NVIDIA" or the first directory named in the path? Commented Feb 8, 2018 at 5:10
  • 1
    for /F "tokens=1* delims=;" %%a in ("%path%") do echo %%b Commented Feb 8, 2018 at 5:28
  • @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. Commented Feb 8, 2018 at 12:24
  • The for loop will launch another instance of cmd.exe in the background and isn't necessary to solve this problem. Commented Feb 8, 2018 at 23:35

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.