Run batch file in location A from anywhere


Marduk

New member
Local time
5:56 AM
Posts
3
OS
WIn 10
Hi,

I have a batch file that works fine but only when run from it's own location. How can I run from any other location?
 

My Computer

System One

  • OS
    WIn 10
    Computer type
    PC/Desktop
In a batch file, the shell variable%~dp0returns the script's folder location.
%cd%is the current folder your shell is running from.

If you are reading files from a folder (using a relative path, instead of an absolute path), you may need to use a different variable in the file's pathname so the script can find it no matter where you run the script from.
 

My Computer

System One

  • OS
    Windows 7
I don't understand how
Code:
%~dp0
can find the location of the script.

When a command prompt is opened it starts in C:\users\user (for me). Now the script is in A:\scripts\new tests.

How can I run that script from C:\users\user where it assumes it is actually in A:\scripts\new tests?
 

My Computer

System One

  • OS
    WIn 10
    Computer type
    PC/Desktop
I don't understand how
Code:
%~dp0
can find the location of the script.

When a command prompt is opened it starts in C:\users\user (for me). Now the script is in A:\scripts\new tests.

How can I run that script from C:\users\user where it assumes it is actually in A:\scripts\new tests?
either copy it to that location and run the copy or modify the content of the .bat. @garlin told you the correct information as to what is running it from the script dir.
 

My Computer

System One

  • OS
    Windows 11 Pro
If you still need help, please post your .bat file here so we can tell you specifically how.
 

My Computer

System One

  • OS
    Windows 11 Pro Insider 64 bit 25H2 26200.5742
    Computer type
    PC/Desktop
    Manufacturer/Model
    Gigabyte Z390 UD
    CPU
    Intel Core i7 9700K 3.60
    Motherboard
    Gigabyte Z390 UD
    Memory
    16 GB
    Graphics Card(s)
    nVidia GEForce RTX 2060 Super
    Sound Card
    onboard
    Monitor(s) Displays
    Two 27" Dell 4K monitors
    Screen Resolution
    3840 x 2160
    Hard Drives
    M.2 NVME SSD, 500 GB; Two 2TB Mechanical HDD's
    PSU
    850w PSU
    Case
    Cyberpower PC
    Cooling
    Water cooled
    Keyboard
    Backlit Cyberpower gaming keyboard
    Mouse
    Backlit Cyberpower gaming mouse
    Internet Speed
    1 GB mbps
    Browser
    Brave
    Antivirus
    Windows Security
Code:
@echo off

@echo Script path is %~dp0
@echo Current path is %cd%
@echo.

@echo DIR of Script path
dir %~dp0

@echo.

@echo DIR of Current path
dir %cd%

%~dp0%filename.doc
is a file in the script's own folder. You don't need a \ separator since ~dp0 already includes a trailing \.
 

My Computer

System One

  • OS
    Windows 7
Thanks guys, I finally figured it out. It was actually much easier then I assumed. I thought I had to somewhere path in the path of the script with the %~dp0 command. But now I understand that the command actually tells the script to be executed as if it was run from the path where the script is located.
 

My Computer

System One

  • OS
    WIn 10
    Computer type
    PC/Desktop
Yep. If you want to see more of those type of variables, look at the help for the "for" command, for /?. You'll see the variables near the end: ~d, ~p, ~x, etc.
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender

Latest Support Threads

Back
Top Bottom