Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Remove the last part of a string  (Read 5523 times)

0 Members and 1 Guest are viewing this topic.

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Remove the last part of a string
« 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).
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Salmon Trout

  • Guest
Re: Remove the last part of a string
« Reply #1 on: February 20, 2010, 05:36:13 PM »
Type FOR /? at the prompt and study the part about about variable modifiers. (Hint: %%~dpI)

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: Remove the last part of a string
« Reply #2 on: February 20, 2010, 05:41:46 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?
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Salmon Trout

  • Guest
Re: Remove the last part of a string
« Reply #3 on: February 21, 2010, 01:23:15 AM »
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.

« Last Edit: February 21, 2010, 01:42:19 AM by Salmon Trout »

ghostdog74



    Specialist

    Thanked: 27
    Re: Remove the last part of a string
    « Reply #4 on: February 21, 2010, 01:54:01 AM »
    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

    Salmon Trout

    • Guest
    Re: Remove the last part of a string
    « Reply #5 on: February 21, 2010, 05:19:03 AM »
    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%
    « Last Edit: February 21, 2010, 05:33:22 AM by Salmon Trout »