1.

Solve : Remove the last part of a string?

Answer»

Is there a way to remove the last section of a STRING, divided by \? For example:

C:\Documents and Settings\User\My Documents\folder_a\b.txt
would become
C:\Documents and Settings\User\My Documents\folder_a\
But the problem is, I don't know how long the wanted part is, or the unwanted (the file name).Type FOR /? at the prompt and study the part about about variable modifiers. (Hint: %%~dpI)
Quote from: Salmon Trout on February 20, 2010, 05:36:13 PM

Type FOR /? at the prompt and study the part about about variable modifiers. (Hint: %%~dpI)

Ahh...thank you. So how would I use that with %1? %~dp1?

Pre-post edit: I was right. Thanks salmon, I wasn't quite sure how to implement those. But how would I do that with REGULAR variables? Quote from: Helpmeh on February 20, 2010, 05:41:46 PM
how would I do that with regular variables?

Essentially you are splitting a string into two parts: (1) From the start to the final token delimiter, (2) the rest. Since DEALING with drives, paths and filenames is a pretty common task, cmd.exe has this built in.

One way is to get the ordinary variable into a FOR metavariable...

Code: [Select]echo off
set var=C:\Documents and Settings\User\My Documents\folder_a\b.txt
for /f "tokens=*" %%A in ("%var%") do set DriveAndPath=%%~dpA
echo %DriveAndPath%

...or pass it to a called label "subroutine"

Code: [Select]echo off
goto start

:getpath
set DriveAndPath=%~dp1
goto :eof

:start
set var=C:\Documents and Settings\User\My Documents\folder_a\b.txt
call :getpath "%var%"
echo %DriveAndPath%
You can have the "subroutine" anywhere in the script e.g. at the end but you need to be alert to ensure, with gotos or some other method, that you don't arrive at it by mistake.

The %~N and %~x modifiers work on URLs as well. (The others do not give very useful values)

Note that the string can be a fictitious one. Neither the file "C:\Documents and Settings\User\My Documents\folder_a\b.txt" nor its drive, path, folder, name, extension etc, have to actually exist. Using the date and size modifiers on a nonexistent file will return null (blank) results.

Quote from: Helpmeh on February 20, 2010, 05:16:46 PM
Is there a way to remove the last section of a string, divided by \? For example:

C:\Documents and Settings\User\My Documents\folder_a\b.txt
would become
C:\Documents and Settings\User\My Documents\folder_a\
But the problem is, I don't know how long the wanted part is, or the unwanted (the file name).

vbscript,
Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strPath = objArgs(0)
WScript.Echo objFS.GetParentFolderName(strPath)

on command line, pass your path as arguments to the script
Code: [Select]C:\test>cscript //nologo getpath.vbs "c:\Documents and Settings\Administrator\Desktop\shortcutlnk"
c:\Documents and Settings\Administrator\Desktop
More general method which can be used for strings which are not seen by cmd.exe as representing a path. Illustrates some things you can do with tokens, and you will see clues on how to get at various things, like the first, last, nth, penultimate tokens, count the tokens & so on...

Code: [Select]echo off
REM get all but last token
set string=C:\Documents and Settings\User\My Documents\folder_a\b.txt
REM MAY be useful
set tokencount=1
REM this format can handle white space delimiter
set "delimiter=\"
REM If delim is a space do this
REM set "delimiter= "
REM Create temp string for result
set ResultString=
:startloop
        REM get the first token in %%A and the remainder in %%B
        for /f "tokens=1* delims=%delimiter%" %%A in ("%string%") do (
                REM no trailing spaces!
                set "firsttoken=%%A"
                REM if remainder is blank then
                REM we have got to the end
                REM so exit
                if "%%B"=="" goto exitloop
                REM otherwise collect the remainder
                set restofstring=%%B
                )
        REM add first token and delimiter to result string
        set ResultString=%ResultString%%firsttoken%%delimiter%
        REM Chop off first token
        set string=%restofstring%
        REM may be useful
        set /a tokencount+=1
        REM go round again
        goto startloop
:exitloop
REM this is the last (unwanted) token
set lastttoken=%restofstring%
echo Result=%ResultString%


Discussion

No Comment Found