0

I'm trying to find the simplest way to, in CMD, extract after either / or \, whichever comes last, if either are found, in a string.

For example:

Input

apiserver_sdk

Expected

apiserver_sdk

or

Input

D:/dev/some/folder/GO/projectName

Expected

projectName

I'm quite familiar with substr,substring from actual programming languages, but am unsure about it in shell languages like CMD.

2
  • Is the string with / or \ always a file path? Sep 25 '18 at 16:57
  • In my use cases, yes. Sep 25 '18 at 17:02
0

Here is a method that should work:

  echo %string% | findstr " / \ " >nul && for %%A in (%string%) do set string=%%~nA

before: apiserver_sdk  
after:  apiserver_sdk  

before: D:/dev/some/folder/GO/projectName
after:  projectName

before: c:\one\two three\four five
after:  five

before: "c:\one\two three\four five"
after:  four five

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.