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.
Related
If I have call C:\Users\user\Desktop\Main\folder\edit.bat
What is the right way to set this up this:
I want to eliminate C:\Users\user\Desktop, and make my batch universal so that I can plug in to any computer, run it from a usb, or external or simply add my batch to the desktop of anyone's computer. And run it without having to edit source or target again.
If I want it to run my edit.bat from any computer do I set it up like this
call %SystemRoot%\Main\folder\edit.bat
or
call %UserProfile%\Desktop\Main\folder\edit.bat
or
call %UserProfile%\Main\folder\edit.bat
If I understand Mofi right I can change this
call "C:\Users\user\Desktop\Main\folder\edit.bat" Original
to this
call "C:\users\%username%\Desktop\Main\folder\edit.bat" Works
and to this
call "%UserProfile%\Desktop\Main\folder\edit.bat" Works
Can anyone tell me if this is the correct
call "%~dp0\edit.bat"
If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\ or %userprofile%\documents\.
#echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist checks for the directory. mkdir creates the destination directory. Pretty obvious but none-the-less.
%~dp0 defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile% environment variable is by default equal to the directory C:\users\%username%\, and %computername% expands to the computer's name.
xcopy takes the source directory and copies to our destination we created prior. /Y forces the copy and does not prompt to overwrite files.
* is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\* will not resolve. For something like that you would need to use for /D.
Lastly I always like to check for errors, and see if it was successful. If not we pause to make sure we see the error, or we put in a timeout /t seconds to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.
If you want to open a file in your portable drive you can do something like this
%~d0\folder\folder\file
I have a program with a separate setup for 32 and 64 bits. My goal is to create a single executable that can run the appropriate setup. So I created a folder and placed the two setups inside, then wrote the following script:
#echo off
if %PROCESSOR_ARCHITECTURE%==AMD64 goto :x64
if %PROCESSOR_ARCHITEW6432%==AMD64 goto :x64
:x86
"%cd%"\setup.exe
exit
:x64
"%cd%"\setup-x64.exe
exit
Afterwards, I created the SFX file with this folder in WinRAR, pointing to the BAT file. But when I run it, a command line window pops up and shuts down instantly and nothing happens. I go to the temporary folder and double click the BAT file and the setup starts. The same happens in the original folder. What is happening and how can I fix it? Thanks!
%cd% reffers to the directory of the call of the batch-file.
For example a batch-file is in %USERPROFILE%\Desktop\Folder\bat.bat:
echo %cd%
pause
and you start it for example from the command-line like this:
C:\>%USERPROFILE%\Desktop\Folder\bat.bat
it should echo C:\ as that is where you called it from.
Two ways from the comments to solve the problem:
Push the directory of the batch-file using pushd "%~dp0" -> will result in a change of the variable value of %cd%
or
do not use "%cd%" but "%~dp0"
Both ways use the fact that the zeroth argument of a batch-file is its path.
You can prevent the command-line window from closing if you are debugging the file from the command-prompt itself if possible. With that you should have seen an error that state something like ...\setup.exe not found. After that nothing had to be done from the batch-file so it closed.
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.
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
I am trying to make batch file for wkhtmltopdf.exe to convert html to pdf, and come up with this code.
#echo off &setlocal enabledelayedexpansion
for %%i in (*.html) do (
set "line="
for %%j in ("%%~ni.*") do set line=!line! "%%~j"
start "" wkhtmltopdf.exe !line!
)
However, it din't convert anything and gave 2 lines of result
wkhtmltopdf.exe awb112312.html awb112312.pdf
wkhtmltopdf.exe invoice.html invoice.pdf
I suppose, there is the error message
wkhtmltopdf.exe not recognized as an internal or external command, operable program or batch file.
output on running this batch file.
The reason is wkhtmltopdf.exe is used in batch file without complete path. Therefore Windows searches first in current working directory of the batch file and next in all directories included in environment variable PATH separated by semicolons for the file wkhtmltopdf.exe and does not find it anywhere as the application is most likely installed into a subdirectory of %ProgramFiles% or %ProgramFiles(x86)%.
wkhtmltopdf is a console application according to homepage of wkhtmltopdf and therefore the command START should not be really needed at all.
Also the inner FOR loop does not make much sense for me if simply each *.html file in current working directory should be converted to a PDF file. Why searching for other files with same name as already found *.html file with any file extension in current working directory and concatenate all found file names to one string?
I think, all really needed is:
#echo off
for %%i in (*.html) do "Complete path to wkhtmltopdf\wkhtmltopdf.exe" "%%i" "%%~ni.pdf"
This works just fine for me in Windows 10 1809 Just FYI took me forever to figure it out.
#echo off
"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" estimate.html estimate.pdf
echo Complete!
#pause
This happened to me on windows when trying to run it through system call in a PHP application. The quick and dirty solution is to Copy the contents of the bin directory to the directory of your php/web application.
I've tried setting the path, restarting the web server etc. It works correctly when invoked from the command line but not when run through a system call in a PHP app... even if you give it the full path.
Once you port your app to a correctly configured unix based web server it will work without having to mess about copying files.
Life is too short to figure out MSFT's quirks but if someone does figure it out I'd be happy to know!