Windows batch file starting directory when 'run as admin'
I have a batch file which is in a directory and must be run from there as well because it updates files within this directory. This works perfectly fine, except when the user runs the batch file as administrator (required on Vista). Then the starting directory is C:\Windows\System32. Is there any way to still be able to know from which directory the batch file was run? I dont want the user to enter the directory manually.
Try to access the batch files path like this:
For more information see the following quote from the command
for /? that describes how the above command works:
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 The modifiers can be combined to get compound results: %~dpI - expands %I to a drive letter and path only %~nxI - expands %I to a file name and extension only %~fsI - expands %I to a full path name with short names only %~dp$PATH:I - searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found. %~ftzaI - expands %I to a DIR like output line
batch current directory when starting as administrator, Okay, it is really just starting a batch script with administrator privileges. And after identifying the real problem, you can easily find an answer. Run a batch file at loading of Windows 8 and 10. Create a shortcut to the batch file. Once the shortcut is created, right-click the shortcut file and select Cut . Press Start , type Run , and press Enter. In the Run window, type shell:startup to open the Startup folder. Once the Startup folder is
call batch file and run using its own path/directory, You are a bit unclear where app1.exe is located. If it shares the folder with run1. bat change run1.bat. to either @Echo off Pushd "%~dp0"� On Windows 10, a batch file is a special kind of text file that typically has a .bat extension, which can include one or multiple commands that Command Prompt can understand and run in sequence to
This should solve your problem by setting the working directory for the batch file back to the current directory:
Include these two lines at the top of your .bat script:
@setlocal enableextensions @cd /d "%~dp0"
Batch file : How to get current directory, I need to get the initial starting directory and save it in a variable. My question is what's the simple way to get the the directory from batch script. ” Below is the� What is the current directory in a batch file? Using the variables mentioned here, you can update run1.bat to call app1.exe with the following line: %~dp0app1.exe. (The %~dp0 variable includes a trailing slash.) This will tell the batch file to run the executable from the current batch file's location.
at the beginning of the batch file to change directory to the directory where the batch file was started in.
[SOLVED] Create start in directory in a batch file?, I am using a batch file to call a program for citrix. Works fine but I need to change the start in directory. I assume this is different from working directory? Read full� Now, say for the second line, I would like the working directory to be D:\SomeDir, but in Prog1.exe and Prog3.exe I want the default working directory (normally, where my .bat file is). If I try this. Prog1.exe cd D:\SomeDir D:\SomeDir\Prog2.exe Prog3.exe Apparently Prog3 will be executed in SomeDir, which is not what I want.
You can CD directly from the file name by adding the parent (not tested in windows 8.x, but has worked "forever" as far as I can remember).
and CD will change drives as well using /D, which is shown above but not explicitly mentioned so might be missed. CD /D %FILENAME%\..
(FOR /? IF /? SET /? CALL /? GOTO /? all provide highly useful reading if you use cmd.exe, I reread them once in a while.)
Batch File Scripting �, bat file and windows knows to execute the commands in the file. When a procedure is to be run multiple times, there are clear advantages to scripting. However, a� I have given a sample code below to change the directory and run a command after that. cd C:\ #Will change the directory to C: ipconfig #Will return IP address details(any command can be used here) pause #Will prevent command prompt from closing and waits for a keypress Save this as a batch file filename.bat and you will get desired output. But
These two commands will copy the Batch file in start up folders (in XP). Start up folder path in Windows 7 is : C:\Users\sys\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. Change location to Files in Start up folder gets started automatically when windows starts . You should first two lines of code in every virus code so that it would copy itself in startup folder.
Updated: 04/26/2017 by Computer Hope To start an exe file from a batch file in Windows, you can use the start command. For example, the following command would start Notepad in most versions of Windows.
The Windows Startup Folder, once easily accessible via the Start Menu in older versions of Windows, is hidden in Windows 10 but still serves a useful purpose. Here's how to access the Startup Folder and configure your favorite apps to launch when you log in to your Windows 10 PC.
- Fantastic, I didn't know this one. Thanks a lot
- How obscure! But exactly what I needed. Thanks.
- @stucampbell try the command 'for /?' to get a detailed description of how this works (scroll down to page 4)
- Donny V try
- @Donny V it does work on Windows 8.1 - I tried it. Keep in mind that this will only work if you are running it within a batch file.
- Great solution for systems with multiple volumes, and works in Windows 7, 8, 8.1, and 10, +1
- This is perfect. Solved my "file not found" issue when running the bat file as administrator.
- That won't change drive letter.
- This is actually the best solution, just change it to
- Syed Abdul Haseeb, I doubt that this helps or even works at all. To convince me otherwise please add an explanation of how this works and why it helps. That would help fighting the misconeption that StackOverflow provides free programming serive.
- You can use
%~dp0directly. No need to invoke