I'm trying to run a .bat file globally by adding the directory which contains it to PATH. This obviously works for exe files but is there a way to run .bat files this way?
As #SLaks said in his comment, this will work.
Based on the rest of your comments, you need to specify the full file name. If there's program.exe and program.bat, you need to enter program.bat and not just program at the command prompt.
When you enter program at a command prompt, the shell will first try to execute program.com, then program.exe, then program.bat. The exact order is saved in the PATHEXT environment variable:
C:\Windows>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Original MS-DOS didn't have this variable, as I recall. It searched .COM, .EXE, and then .BAT so the behavior has preserved since MS-DOS 3.3 (and I believe since IBM DOS 1.0), but I believe it was hard-coded. The PATHEXT variable was introduced with Windows NT, IIRC.
Edit to add:
Ah, alright. It sounds like your batch file also needs to change to it's own directory so that the current working directory is wherever it's at. The easy way to do that is at the beginning of the batch file (after your #echo off) put:
pushd %~dp0
This will change the current working directory to wherever the batch file is. And then at the very last line:
popd
That will change the current working directory to what ever the current working directory was before the last time that pushd was run.
The two commands, pushd and popd, are like advanced change directory commands. If you're at C:\ and you type pushd C:\Program Files\, you'll change to that directory. Then if you type popd, you'll go back where you just were at C:. You can do it multiple times, each time "pushing" another directory onto the "stack" of your directory history. popd then removes the top of the stack, and takes you back one. Think of it like the back button on your browser. You can even change the drive at the same time. pushd D:\ will change to the D: drive and set the directory to the root of D:.
Now, the %~dp0 is a bit weirder. It's a modified variable.
You might know that the arguments for a batch file get assigned to special variables. %1 is the first argument, then %2 is the second, %3 the third, and so forth up to %9. %0 is the zeroth argument. That's the name of the actual batch file itself. If we run program.bat from the C:\Folder\ directory, %0 is probably program.bat.
The tidle (~) removes double quotes around the argument. So %~0 is the file name of the batch file without any quotation marks, which can appear if you have spaces in your file or folder names.
The d means "drive letter only". So %~d0 would be C: (assuming we're on the C: drive).
The p means "path only". So %~p0 would be \Folder\.
We want both, so dp means "drive and path only". So %~dp0 expands to C:\Folder\.
So, the first line of the batch file is now:
pushd C:\Folder\
But it's dynamic! So if you move it to D:\AnotherFolder\, it will still work without needing to edit it. You can find a full list of the variable modifications that cmd.exe understands under the for command.
You can also add .bat files to PATH though the control panel:
Hit the windows key ⊞ Win, and start typing environment.
Click enter on the control panel option:
Click on environment variables:
Scroll down to Path, and click edit:
Then click new, and enter the folder of the batch file.
If your file is named abc.bat, and it is in your PATH, you can run it from CMD with:
abc
Related
I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"
I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.
So I am writing a little program in batch, and I am hoping it will be able to run it on other's computers. My cd is to C:\Users\%username%\Desktop\,\thing, but that is not where other people may store it. How can I locate where it is and do a cd to it? Thanks!
%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change e.g. by using the CD command)
%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.
#echo off
echo %appdata%
CD %appdata%
pause
echo %CD%
pause
Dir
pause
echo "%~dp0%"
pause
CD "%~dp0%"
Dir
cls
echo this is %%cd%% %cd%
echo this is %%~dp0 %~dp0
pause
Programs and documents can be added to the registry so typing their name without their path in the Start - Run dialog box or shortcut enables Windows to find them.
This is a generic reg file. Copy the lines below to a new Text Document and save it as anyname.reg. Edit it with your programs or documents.
In paths use \\ to seperate folder names in key paths as regedit uses a single \ to seperate it's key names. All reg files start with REGEDIT4. A semicolon turns a line into a comment. The # symbol means to assign the value to the key rather than a named value.
The file doesn't have to exist. This can be used to set Word.exe to open Winword.exe.
This sample add IE.Txt (from IE5) to the registry so typing IE.Txt will open it. I think the file is called IE4.txt in IE4.
REGEDIT4
;The bolded name below is the name of the document or program, <filename>.<file extension>
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\IE.txt]
;The # means the path to the file is assigned to the default value for the key.
;The whole path in enclosed in a quotation mark ".
#="\"C:\\Program Files\\Internet Explorer\\IE.txt\""
;Optional Parameters. The semicolon means don't process the line. Remove it if you want to put it in the registry
;Informs the shell that the program accepts URLs.
;"useURL"="1"
;Sets the path that a program will use as its' default directory. This is commented out.
;"Path"="C:\\Program Files\\Microsoft Office\\Office\\"
reg command can read or write to the registry in batch. Register it as if it's an exe file not a batch file.
M'kay, so I've written a few batch files before, so I'm not completely new to them, but this is stumping me. What I'm trying to do is run a .exe file from a batch file. Here's the Batch script:
#echo off
:start
setlocal EnableDelayedExpansion
cd "C:\Users\Zac\Dropbox\SoundCloud"
set n=0
for %%f in (*.html*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
move "!file[%rand%]!" C:\Users\Zac\Temp
start "~dp0Link_Open.exe"
echo %time%
timeout 70 > NUL
echo %time%
goto start
So from my understand, this moves a random .html file from one directory to another, this works, I've used it a lot, the only issue is the "Start" command, I don't use this very often. the "Link_Open.exe" is in the same folder as my .bat, but I've tried running it with the full directory written in, I've tried quotations, no quotations, brackets, no brackets, START, start, Start, Call, CALL, call, and none of them work, I'm always getting the same error "Link_Open.exe cannot be found, have you written it correctly"
The only reason I can possibly think of that would be why it wouldn't work, is that the .exe was written in AutoIT and then compiled...but that shouldn't effect it should it?
Running the batch file will result in a random file being moved, and then an error coming up, and then repeating.
What am I doing wrong?
Ps: Running the Link_Open.exe does what it's supposed to do, so there's no errors there, the only issue I'm having is opening it with .bat.
I'm still very new to Autoit, but if someone could show me a script to move a random .html file with Autoit, I could just combine the two scripts together couldn't I?
Provided that your bat file and exe file are in the same directory as you said, try changing:
START Link_Open.exe
to:
start "%~dp0Link_Open.exe"
%~dp0 expands to the current batch script's path
start equivalent to START, it doesn't matter
enclosed in quotes in case your path has spaces like C:\My Documents, thus keeps the whole path as one argument, instead of being misinterpreted as being multiple arguments separated by space
Error Explanation
Your error: Link_Open.exe cannot be found... is precisely that, batch can't find Link_Open.exe. Normally it would, because when you just give a program without full absolute path, it checks the current working directory. This defaults to where the batch script is. But notice you actually changed the current working directory with this line:
cd "C:\Users\Zac\Dropbox\SoundCloud"
Thus, unless you happen to have Link_Open.exe in that SoundCloud directory, or unless you had any code to cd back to the batch script's directory containing the exe, or unless you happen to have Link_Open.exe in %PATH%, then there is no reason batch would automatically know to check the original directory that the script was run from.
Thus we try to refer to Link_Open.exe in a way that batch will know how to find it. One way is if we did absolute path C:\path\to\Link_Open.exe however to keep things portable, I recommend %~dp0 which expands to whatever path of the current batch script, and thus batch is able to find Link_open.exe again.
You change directory to C:\Users\Zac\Dropbox\SoundCloud and then try to start Link_Open.exe which is left in script directory.
So next step you use ~dp0 variable to get folder from script executed, but there is missed leading %: you must write %~dp0
Even though you'll fix it, maybe Link_Open.exe also depend on current working directory? start allows to set up CWD: start "window header" /D "%~dp0" "%~dp0Link_Open.exe"
To debug: add echo before start and add pause next line. start command will be printed with all variables expanded. You'll see if the something is wrong - e. g. you may copy and test via Start -> Run.
Here's a simple example with two bat files, caller.bat and callee.bat in the same directory.
caller.bat
call callee.bat
pause
callee.bat
echo "All good"
When I run caller.bat by double clicking it in Explorer it works as expected but if use right-click "Run as administrator" I get
'callee.bat' is not recognized as an internal or external command...
The problem is that when run as administrator the current working directory is changed to C:\Windows\System32. My solution was to explicitly change the current working directory in caller.bat to be the same as the directory from where the file is run. This is done by extracting the drive and path from the %0 parameter as shown below:
cd /D %~dp0
call callee.bat
pause
The /D argument to cd causes the directory as well as the path to change and is needed to handle the case where the caller .bat file is not on the C: drive.
More info here: Hidden features of Windows batch files
Another solution is to add the directory where your scripts are stored to your path system environment variable. You could do this through the GUI that windows provides in the advanced system settings (type environment variable into the start menu and you should see the option), or you can run a cmd session as admin and enter:
setx path %path%;"your script dir" /M
The /M makes it system wide, not just your user (this is what requires the admin). The double quotes are only required if your path has any spaces in it. The path variable contains a list of paths delimited by semi-colons. The above just appends a new entry to the existing list. Adding an entry to the list allows you to execute programs from this directory without specifying the path.
Finally, if this does not work on it's own you may need to also add .cmd and/or .bat to your pathext variable:
setx pathext %pathext%;.cmd;.bat /M
To check the current values of the variables do set var_name i.e.:
set pathext