Search code examples
batch-filesubstr

How to get path of file with batch


I have a batch file that is stored in the c:\data\profile\script.bat directory. My script.dat contains :

@echo off
echo %~dp0%
pause

If I run it, will display on the screen c:\data\profile\. but that's not what I want. I want to display on the screen c:\data\. for that, I have change my batch contains to :

@echo off
echo %~dp0:~0,8%
pause

then I run the file , but the screen only displays c:\data\profile\:~0,8. please let me know how to do it?


Solution

  • How about this:

    @ECHO OFF
    PUSHD
    CD ..
    ECHO %CD%
    POPD
    PAUSE
    

    Your code will work if you modify it like this:

    @echo off
    set tempPath=%~dp0
    echo %tempPath:~0,8%
    pause