changing the path in the batch script dynamically on run time - batch-file

I am working on a batch script file to copy and trigger a .exe file installation
So i have coded it as follows:
set path = "c:\path_to_install_exe\"
set installationfilepath=%cd%
(this one gives, d:\installation_file_path\commands)
the installation file is present in the above path and i want to copy from my current working directory
so i gave the it as
echo xcopy "%installationfilepath%\..\install.exe" "%path_to_install_exe%"
But this does not do the necessary operation. i get d:\installation_file_path\commands..\install.exe as an output.
Can anyone please help me on this, as i am new to batch file scripting.

You could always use pushd and popd to change the directory the batch file assumes.
Like how
pushd C:\Users\Arescet\Desktop\
echo Hello world! > Hi.txt
popd
Will create "Hi.txt" with 'Hello world!' no matter where you launch the bat.
For shorter commands, you're better off setting a different fileName, in the list of default variables 'path' is already a system variable, and it is unwise to change it, as 'MC ND' stated.
For just one single file, you're better off with the copy command as it lets you indicate the path type.

Related

How to get the current path of my batch file? [duplicate]

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.

How to return to the directory from where I ran the batch script file?

My folder structure is like some thing below,
C:\WORK\Project\
Inside the above path there are multiple sub folders,
1. Src
2. bat
3. output
etc.
All my batch script file are present in C:\WORK\Project\bat\
So I will be going to the same directory and run my batch script file.
But as my source code and all present in side C:\WORK\Project\Src path so in my batch script file the path mentioned as C:\WORK\Project\
Problem :-
After finishing my batch command as the path mentioned C:\WORK\Project\ so it is going to the same folder in command prompt. But I want to be in C:\WORK\Project\bat from where I started my command.please let me know hoe to achieve this.
As there are many folders inside C:\WORK\Project\ which I need to take into consideration so I have to set my path in batch script file as C:\WORK\Project.
from the prompt, try
pushd /?
and its friend,
popd /?
Probably the easiest way is to include in the batch file, before any directory change
setlocal
This will make changes to the environment local to the current batch file. When the batch file finishes or when endlocal command is used, any change to the environment, including changes to the current active directory, will be discarded.

Batch file Start program

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.

Running batch file depending on other files from batch file

I'm writing a batch file which is supposed (among other things) to call another batch file.
The second batch file is depending on files located in the same directory, so, when I try running it from the first batch file, it fails.
Example:
Batch file #1: (Located on C:)
#echo OFF
call C:\Tests\Tests.bat
Output: "Could Not Find C:\Tests\Tests.txt
Reason: Both Tests.bat and Tests.txt are located under: C:\Tests while I'm calling it from C:.
Any ideas?
Thanks a lot,
Idan.
You can use %-dp0 within a batch file to get the path of the currently executing batch file. You can use that path every time you need to access a file from that directory. For example:
call "%~dp0\Tests.bat"
for /f "usebackq ..." ... in ("%~dp0\someFile") do ...
Another option is to temporarily set your current directory to the same path at the top of a batch script using PUSHD. From that point on you don't have to worry about providing the path to the other commands. Just be sure to use POPD before the script exits. That way the script will not impact any script that may have called it.
pushd "%~dp0"
rem Do you work
popd

iexpress extract files then run script referring extracted file

I was trying to deploy my project with IExpress. I have the following scenario. I want to extract some files to a location preferably c:\program files\. Then after it copies all the files i want it to run a .cmd file (which is a script). the script is also added in the project itself and it would refer to a file which is copied by IExpress. Now how can access the path on which the file was extracted. So that i can access it in my script.
If the script is in the project itself, thus being extracted in the same directory when you send your files to, it should start in the same directory.
Test it easy, make a cmd like this:
cmdsetup.cmd:
#echo Source path: %~dp0 >> %temp%\%~n0.log
Put this in your package and when it's done, go check the %temp% directory, locate the cmdsetup.log file and look in it. This should be the path where your files are.
If so, go from there. If I got this wrong, come back and comment, also amend your question to make it clearer.
I hope this helps.
P.s.: Voted for the question as I don't see why the negative vote was given.
The answer is use this format:
start /wait .\hello.cmd
I did this with the following two files. One key thing is that the file I was calling had to be in 8.3 format. In otherwords it failed to find hello.cmd the first time because i called it hello.world.cmd.
First file (start.cmd)
#echo off
cls
echo this is start.cmd
pause
dir
pause
echo going to hello world
start /wait .\hello.cmd
echo back in start.cmd
pause
Second file (hello.cmd)
#echo off
echo HELLO WORLD!
pause
exit
Directions
Use IEXPRESS to create a package that contains both files above. Have it launch START.CMD.

Resources