user3180704
user3180704

Reputation: 139

Trim the path upto a certain string using batch command

I have path in a variable from registry that looks like this :

SET dirpath="D:\Veritas\product\7.5.0.2\bin\DBBACKEX.EXE BACKUPDIR"

I need output only upto bin folder using windows batch command that should look this: D:\Veritas\product\7.5.0.2\bin

I used a simple VBScript within the batch command like in below code which did the job, but i need it using batch command only without the use of another file.

FOR /F "usebackq tokens=*" %%p in (`CSCRIPT //nologo regpath.vbs %dirPath%`) DO SET VPath=%%p

I don't see much string manipulation functions in batch commands, I want to remove the CSCRIPT line in above code and use only batch command. Can someone please help.

Upvotes: 0

Views: 1171

Answers (2)

Aacini
Aacini

Reputation: 67256

To "trim the path upto a certain string" using the string "bin" as you requested, you may use this simple method:

@echo off
setlocal

SET "dirpath=D:\Veritas\product\7.5.0.2\bin\DBBACKEX.EXE BACKUPDIR"

SET "VPath=%dirpath:bin=bin" & rem "%"

echo %VPath%

The trick consist in split the path at the desired string and eliminate the rest via a rem command; you may remove the @echo off line and execute the code in order to see what exactly is executed...

Upvotes: 2

Stephan
Stephan

Reputation: 56238

use for modifiers to get the drive/path:

for %%a in ("%dirpath%") do set dirpath=%%~dpa

See for /? for a description of the modifiers.

Upvotes: 1

Related Questions