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!
Related
Made a simple batch script to create a .sfv for each file within the folder. I'm far from being a coder but I patched this together from various sites and made it work:
for %%f in (*) do (cfv -C -f"%%f".sfv -tsfv "%%f")
The issue is, if I run this batch file more than once, existing files get checked again and a separate .sfv file is created as a *.sfv.sfv
Just wondering, how can this be written to ignore checking a file/writing a new .sfv if a .sfv exists? Thank you!
Edit: I tried the following:
#echo off
#For /F "EOL=? Delims=" %%G In ('Dir /B /A:-D-S-L ^| "%__AppDir__%findstr.exe" /EILV ".sfv"') Do #If Not Exist "%%~nG.sfv" fsum -js "%%G" > "%%G.sfv"
It does an excellent job of not adding a .sfv.sfv file every time its ran, great! Issue now though is when the script is ran, it has to read each file regardless if there is a .sfv file beside it or not.
The idea here is to have the script see that there is a sfv file with the same filename as the original file, and if that .sfv file exists skip it entirely and move to the next file until there are no more files without a .sfv associated with it. I know it's not easy to put this into words so let me try and give an example.
/parent directory/
linuxiso1.iso
linuxiso2.iso
linuxiso3.iso
*Cujo67 runs Batch.bat script*
/parent directory/
linuxiso1.iso
linuxiso1.sfv
linuxiso2.iso
linuxiso2.sfv
linuxiso3.iso
linuxiso3.sfv
With the current code it has to crunch each .iso file and then sees that it's already got a .sfv in the directory, and skips it. Wondering how to have this script just avoid running the checksum command entirely if there is already as corresponding .sfv alongside the .iso file.
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.
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.
I am trying to overwrite two files with each other but it gives me a message that it cant find the files specified.
#echo off
set /p Drive=Enter local hard drive letter:
if "%Drive%"=="" goto :eof
xcopy Drive:\windows\system32\sethc.exe Drive:
xcopy Drive:\windows\system32\cmd.exe Drive:\windows\system32\sethc.exe
Make sure you files are in the same path as your batch file is, for example:
#echo off
start minecraft.exe
In this example, obviously it starts minecraft, but in the format it currently is, it would require minecraft to be located on your desktop if your batch file is on your desktop, if minecraft.exe is in a folder named "Folder1" then you would type:
#echo off
Start "C:\Users\<your name>\Desktop\Folder1\minecraft.exe"
In order for this to work, your batch file will also need to be located in Folder1 as well. Obviously, instead of minecraft.exe, you would use the files that you are using.
As long as your batch file is in the same path as your files then it should work. I hope the example I used helped you.
You are using %drive% where you should use drive
and using drive where you should use %drive%
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.