Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9301115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:56:28+00:00 2026-06-18T22:56:28+00:00

Within my batch file I have a variable that contains a file path: SET

  • 0

Within my batch file I have a variable that contains a file path:

SET VAR1=C:\Folder1\Folder2\File.txt

I would like to extract on the directory structure and retreive:

C:\Folder1\Folder2\

I have read threads like this where I need to use %~dp0 where 0 I believe is passed as a parameter. I have tried %~dpVAR1 but that doesn’t work. How can I get the output I’m looking for, but with a variable containing the file path?

Also, to make matters difficult, I have to perform all of this within an IF condition which means that once the variable is declared, I will need to refer to it with ! instead of % (I have declared setlocal enableextensions enabledelayedexpansion at the beginning of my script to allow for this).

Any help is much appreciated!

Thanks!

Andrew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:56:29+00:00Added an answer on June 18, 2026 at 10:56 pm

    You are attempting to use parameter expansion syntax on an environment variable – that cannot work. But it is relatively easy to do what you want.

    Using a CALL (relatively slow):

    (...
      call :getPath "!var!" var
      ...
    )
    exit /b
    
    :getPath
    set "%2=%~dp1"
    exit /b
    

    Using FOR, assuming the variable does not contain any wildcards (fast)

    (...
      for %%F in ("!var!") do set "var=%%~dpF"
     ...
    )
    

    Using FOR, if the variable may contain wildcards (also fast)

    (...
      for /f "delims=" %%F in ("!var!") do set "var=%%~dpF"
     ...
    )
    

    Note 1: If the variable does not contain the full path, then all the solutions will attempt to resolve the name into an absolute path and will return the full absolute path. For example, if var contains foobar\test.txt, then the solutions will include the full path to the current directory, even if the file is not found. Something like c:\pathToCurrentDirectory\foobar\.

    Note 2: All solutions above will remove all quotes from the path.

    Note 3: A path could include the ! character, which will cause problems when expanding %~dp1 or %%~dpF because you have delayed expansion enabled. The delayed expansion will corrupt both ^ and ! if the value contains !. There is a solution that involves protecting both ! and ^. Here is a demonstration applied to the last solution above. The protection requires normal expansion, and since you are within a code block, it requires at least one CALL. It could be done without a subroutine, but it is easier with a subroutine. The subroutine assumes the variable is named var.

    (...
      call :getPath
     ...
    )
    exit /b
    
    :getPath
    set "var=!var:"=!"
    set "var=!var:^=^^^^!"
    set "var=%var:!=^^^!%" !
    for /f "delims=" %%F in ("!var!") do set "var=%%~dpF" !
    exit /b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a batch file that I usually invoke like this: longjob.cmd >result.txt 2>&1
I have a batch file that I need to run within my NSIS installer.
I have a batch file that will check for updates within a directory then
I have a small Windows batch script that is executed from within a game
I have an issue with my batch file that incorporates a 10sec delay between
I have a batch file that runs an SSIS job. I have no knowledge
I have a simple FOR loop inside of a batch file I'm creating that
I have created a batch file that regularly gets triggered from an automated job.
I have a 3rd-party installer program that I would like for my users to
I have a requirement to, within a windows batch file, read the first available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.