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 6598201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:18:37+00:00 2026-05-25T18:18:37+00:00

I have a BAT file in a directory D:\dir1\dir2\getpath.bat when i run the bat

  • 0

I have a BAT file in a directory

D:\dir1\dir2\getpath.bat

when i run the bat with the below code it prints

D:\dir1\dir2\

i want only the path D:\dir1\

The directory structure is not fixed , need the complete directory path other than the current directory in which BAT file resides.

@echo off
SET SUBDIR=%~dp0
ECHO %SUBDIR% 

tried using delims in a for loop but it didnt help.

  • 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-05-25T18:18:37+00:00Added an answer on May 25, 2026 at 6:18 pm

    If it’s the parent directory of the directory your script resides in you want, then try this:

    @echo off
    SET batchdir=%~dp0
    cd /D "%batchdir%.."
    echo %CD%
    cd "%batchdir%"
    

    (untested, please comment if there are problems)

    Note that this will, of course, change nothing if your batch resides in your drive root (as in F:\) 😉 If you’d like a special output if that’s the case, you should test %CD% against %batchdir% before the echo.

    EDIT: Applied patch, see comment by @RichardA

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have bat-file, that make some operations. How to run this file from Delphi
I have bat file like below with name myBat.bat 1) @echo off set CLASSPATH=%CLASSPATH%;C:\Documents
I have the following directory structure: C:\UserX\A\Bat C:\UserX\B\Bat C:\UserX\C\Bat and in each Bat directory
I need to create a .bat file to run java program. I have a
I have a bat file that I should use to delete a part of
I have a PHP script that executes a .bat file using system(cmd /c C:\dir\file.bat);
I have a simple bat script that copies files from an known directory to
I would like to have a BAT file open a sql server script. Currently
I have the following line in a BAT file, it does not work, how
I have a batch file and I need to invoke it like this mybatch.bat

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.