How to assign chdir to a variable in .bat? - bash

I'm trying to translate a bash script into a .bat script. The specific line I'm tripping over is this:
X=`pwd`
What is the .bat equivalent?
I need to take the directory that the script is currently running in as a variable so I can use a generic relative path to find files in the directory. I'm running on Windows-XP in the command prompt.

The current directory is available in the pseudo-variable %cd%. So:
set X=%cd%
stores it in a variable named X.

Related

How to make a batch script to execute from anywhere?

I would like to run my batch script from anywhere, for example:
C:\>test123
And that command line executes my batch script C:\Documents\test123.bat.
I tried to do it with environment variable path, but it doesn't seem to work.
Add your bat to PATH and make sure PATHEXT contains .BAT
How to add your program to the PATH env variable
Note, that the current working directory will be whereever you called it from.
If you want relative paths to where the bat is stored, use %~dp0
Please post the output of echo %PATHEXT% %PATH% and tell us the exact location of your bat file and more error info why it failed in your case?
Adding to C:\Windows\System32
Worked for Me.
System info:
Windows 10 with Admin access.

prevent Windows .bat file from quiting

I have a very simple Windows .BAT file:
set PATH=c:\xxx;%PATH%
call foo.pl
set VAR=true
I thought "call" will start a new batch process, without affecting the current one. However, the batch file exited immediately after the foo.pl finished executing. The set VAR=true has never been called.
Is there a way to fix it?
foo.pl is not a batch file, it is a Perl script.
So you need to use
path c:\xxx;%PATH%
"Path to\Folder With\perl.exe" "foo.pl"
rem Additional batch code executed after Perl script execution finished.
In other words you have to run the console application perl.exe best with full path, or with just perl.exe if program files folder of Perl is not the same on all computers on which this batch file is used and hopefully PATH contains also the directory containing perl.exe.
If you specify on a command line or in a batch file just foo.pl, Windows looks in Windows registry which application is associated with .pl for action Open. If there is such a file association, Windows runs this application in a separate process like when using command start.
So using call foo.pl is like using start foo.pl.
PATH is not only an environment variable, but also an interal command written for changing the value of environment variable PATH at any time within a batch file. This is the reason why I removed set from first line. It is better to use internal command path for modifying environment variable PATH.

Execute Batch Script From Environment Variable

I've set an system environment variable called find which points to a batch script. I did this so that in Win command prompt i could type %find% and it would execute my script. It works the only problem is it only works once, my script takes a parameter or requires user input (have tried both), and then it is as if the %find% is temporarily overwritten, and the %find% of course no longer works, until i reopen the command window. Basically it works once and that's it!
How can i make it work every time? i want to execute my script using the environment variable over and over again at will without reloading the command window.
Thanks.
I created a batch script with the following code:
#ECHO off
echo hello
and added a environmental variable called TEST that points to the script. I have no problem executing the script using the environmental variable multiple times.
Can you please provide some information or code of what your script does?
Remember that find is a MS-supplied utility.
Try using a different name. And show us your batch - even possibly describe what happens when it "no longer works." Games of 20-questions are tedious.
The problem is that the Batch script uses a variable with the same name, so after it run for the first time the variable value is overwritten and no longer works. To prevent this to happen, insert a setlocal command at beginning of the Batch file; this way, when the script ends all variables are reset to the values they had before the script run. This method also delete all new variables defined in the Batch script, so it keep the environment clean.
If your intention is to override the behavior of the existing find.exe utility, you could add the location of the script to the global path variable before your System32 folder (where find.exe is located). For example, let's say your script is C:\Scripts\find.bat. If your path variable is currently set to this:
%SystemRoot%\system32;%SystemRoot%
...then you would change it to this:
C:\Scripts;%SystemRoot%\system32;%SystemRoot%
Beware though... doing this could break other scripts that use the find command (if they don't use the absolute path to find.exe).
If you are just wanting an easy way to run your alternate find command, you could just give it a different name as the others have suggested, then add it to the end of the path or place it in the System32 folder. That would save you from having to type the percent signs at least.

How to execute a .bat file first instead of an file .exe when both have the same path/name?

My emacs.bat rest in the same bin directory as emacs.exe. This directory is included in the PATH variable (Windows).
Content of emacs.bat:
#echo off "%~dp0emacsclientw.exe" -na "%~dp0runemacs.exe" "%1"
However, whenever I use this command, emacs.exe gets executed instead of the .bat file. How would I go about fixing this?
It seems you could put the emacs.bat in another directory and place that directory earlier in the PATH. Note: if you were to run "emacs" while in the directory containing emacs.exe then the .exe would take precedence.
Edit: Alternatively, you could just type emacs.bat instead of emacs at the command prompt.
Edit 2: As seen in this Answer, you could set the PATHEXT environment variable. Something along the lines of:
PATHEXT=.BAT;.COM;.EXE;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.RB;.RBW

How to call a .bat file from any location in CMD on Windows

I have a Batch file which I want to execute in CMD from any directory. Something like this:
File name: MyBatch
Path: C:\MyBatch.bat
Open CMD:
c:\Program Files> MyBatch
How can I accomplish this?
Set that location in your PATH environmental variable.
I wouldn't put it the root or the system directory.
I keep a directory with all my scripts in C:\DRR\CMD
and either set it in the MyComputer GUI or run at the command script:
set PATH=%PATH%;C:\DRR\CMD
You could just put it in your c:\windows\system32 directory, as its always in the system path.
How about...
"%MyBatch%"? (the double qoutes are intended) That should work!
to change your Variable, use set MyBatch="Path\Whatever.bat"
and to ask the user for a String, use set /p MyBatch="Question? "
-- or, you can use a BAT-to-EXE converter to run the batch in a Executable.
You would need to set the PATH environment variable to include the path to your batch file
If you are talking Windows, then the PATH Environment variable is what you need to set.
The path where your bat file is placed should be appended to the PATH variable.
In your example append "C:\;" in the value for Path environment variable.
Then you can execute MyBatch.bat from anywhere on the command line.
Create a folder called Batches (lets say in your C drive).
Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.

Resources