.bat file auto path updater

  • I am trying to run a .bat file, it functions correctly when its on a specific path but when i move it, it wont work so i came to ask if there is any code that updates the path.

    This is the script.
    set file=C:\Users\NAME\Desktop\Folder\OUTPUT.txt

    I want it so it updates the path, so no matter where i put it, every time i run the .bat this path will update but i need a automatic path updater script to work with the script above if thats possible so that path will update and the script will perform as usual. Just need the path to automatically update when i relocate it, also when i put it into different pc’s.

    (Windows 10 if that helps)

    Would like a script, Thanks.

  • @Alexander-Brkljac

    This is a Notepad++ forum, not a batch coding forum. Lucky for you I’m quite skilled in the ways of Windows batch. Your question is not clear - auto-update to what directory - the current one the batch script is running from?

    Open cmd.exe, type for /? - all things will become clear.

    Especially the following:

    In addition, substitution of FOR variable references has been enhanced.
    You can now use the following optional syntax:
        %~I         - expands %I removing any surrounding quotes (")
        %~fI        - expands %I to a fully qualified path name
        %~dI        - expands %I to a drive letter only
        %~pI        - expands %I to a path only
        %~nI        - expands %I to a file name only
        %~xI        - expands %I to a file extension only
        %~sI        - expanded path contains short names only
        %~aI        - expands %I to file attributes of file
        %~tI        - expands %I to date/time of file
        %~zI        - expands %I to size of file
        %~$PATH:I   - searches the directories listed in the PATH
                       environment variable and expands %I to the
                       fully qualified name of the first one found.
                       If the environment variable name is not
                       defined or the file is not found by the
                       search, then this modifier expands to the
                       empty string

    So use:


    In the Windows batch file whenever you want the full path / directory to where the batch file is running from.


  • @Michael-Vincent said in .bat file auto path updater:


    Hi Michael

    I am having abit of trouble with finding a place to put %~dp0
    is it ok if you could make an example of where to put it here is the whole .bat

    @echo off



    color 02


    goto :start


    echo --------------COMMAND FULFILLED-------------

    set file=C:\Users\NAME\Desktop\FOLDER\OUTPUT.txt <------- path i want to auto change when i move the output.txt

    ipconfig /all>> %file%


    goto :A


    echo --------------COMMAND PENDING----------------

    set file=C:\Users\NAME\Desktop\FOLDER\OUTPUT.txt

    systeminfo>> %file%

    goto :B


    echo ---------------COMMAND COMPLETE----------------

    set file=C:\Users\NAME\Desktop\FOLDER\OUTPUT.txt

    time /t>> %file%


    timeout 1 >nul


    I am quite new to .bat and im really trying to get this to work it would be extremly helpful if i could have an example.
    i want the path ‘file=C:\Users\NAME\Desktop\FOLDER\OUTPUT.txt’ to change every time i move the document.

    Hope this makes sense its the final stage of my program.

    Thanks Alex,

  • @Alexander-Brkljac

    set file=%~dp0\OUTPUT.txt


Log in to reply