Run batch file in location A from anywhere


Marduk

New member
Local time
9:45 PM
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 My Computer

At a glance

WIn 10
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 My Computer

At a glance

Windows 7
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 My Computer

At a glance

WIn 10
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 My Computer

At a glance

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

My Computer My Computer

At a glance

Windows 11 Pro Insider 64 bit 25H2 26200.5742Intel Core i7 9700K 3.6016 GBnVidia GEForce RTX 2060 Super
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 My Computer

At a glance

Windows 7
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 My Computer

At a glance

WIn 10
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 My Computer

At a glance

Windows 11 Enterprise 25H2 [rev. 8893]
OS
Windows 11 Enterprise 25H2 [rev. 8893]

Latest Support Threads

Back
Top Bottom