windows 7, cannot invoke exe from bat file - windows

In windows 7 a bat file was made:
rem set UD_LOG_FILE_PATH=%temp%\defrag_native.log
C:
cd \Windows\System32
udefrag.exe --optimize-mft C:
udefrag.exe -o C:
pause
I can double click the bat file and it runs OK. However, the bat file was made to be invoked from a running program. When the program shells out to the bat file the following error is produced:
'C:\Windows\System32\udefrag.exe' is not recognized as an internal or external command,
operable program or batch file.
Invocation is done using Shell in VB6. Different things were tried. First the bat file was called directly, then with C:\Windows\System32\cmd.exe /c, then with C:\Windows\SysWOW64\cmd.exe /c. All produce the same result. The bat file runs but will not run the exe file within the bat file. But the bat file works OK if run directly. Please help. Thanks

Google says that udefrag.exe is a third party program. If it is in c:\windows\system32 then it is on the path and you only need the last 3 lines in your batch file. Try this first and see if it needs elevated permissions. If you have UAC turned off then turn it back on for the test. Your VB program may not have the right permissions.
#echo off
udefrag.exe --optimize-mft C:
udefrag.exe -o C:
pause

I used task scheduler to resolve this. A task was created with no trigger named "defragment". The task invokes the bat file. Instead of invoking the bat file from the running program the following command is given:
schtasks /Run /TN defragment
This runs the bat file through the task scheduler. I don't know why I have to do it that way, but it works good.

Related

running batch files inside another batch file

I have made a batch script to run some .exe files and .bat files. The format of the script is as follows:
a.exe
b.exe
c.bat
d.bat
e.bat
f.exe
g.exe
but the problem is when I run the script it is only executed till c.bat. d.bat is not executing. Is there any problem in writing the batch script for executing other batch files?
just in case you missed the full application of call
a.exe
b.exe
call c.bat
call d.bat
call e.bat
f.exe
g.exe
is how your script should now look.
if you use the 'call' command, the batch files next batch/exe file wouldn't execute unless the batch file stops. Instead, use the 'start' command. that way, it'll execute the batch in a new window

start an exe with bat and it's own location

Okay this is my ToolChain setup:
I got a bat script that is calling a further bat script
The second bat script is calling an .exe which is performing autoit's on a software like starting, saving and closing.
In other words: bat > bat > exe (AutoIt) > exe (a Parser)
Okay here is the issue, the software (Parser) that will be started at least is some kind of weird, it won't find important files, if it's not being started from the same directory.
So as example if i put my autoit .exe or my .bat file into the directory of the software and start it - all is fine. But if i move my exe or my bat scripts outside of the directory and call it, it won't find files to start working.
For exactly this reason i made a second bat file and tried to set the path (set PATH=%PATH%;D:/filetype/dbcparser/) but that didn't have any effect on my case; if i call every bat or exe file not from the directory, it won't work correctly.
I hope someone found something like this before and could give me a hint for a workaround.
I was in the same situation, but using cd before start worked for me(whole file):
#echo off
cd "<start here>"
start "" call "<start here>your.bat"
also if you have PATH problems, try even this:
#echo off
cd "<start here>"
set PATH=<something>;%PATH%
start "" call "<start here>your.bat"

.BAT file not running in task scheduler

I have a simple batch file set up on a 64-bit Windows Server 2008 R2. I'm trying to set it up to run under the Windows task scheduler, specifying the admin account as the user. I have no problem running the batch file manually, when logged on to the server with the same admin account. When in the Task Scheduler though, the task just Runs and hangs.
Here's the code from the batch file, moving files from a mapped drive to which the admin account has access:
DEL "O:DEL "O:\Finance file\AP\Check Run Merge\*.*" /Q
copy "O:\Finance file\AP\Invoices\ADT SECURITY_549527511_2015-05-12_655.32.pdf" "O:\Finance file\AP\Check Run Merge\142000.pdf"
copy "O:\Finance file\AP\Invoices\CENTRAL DRUG SYSTEM_244658_2015-05-08_3.50.pdf" "O:\Finance file\AP\Check Run Merge\142001.pdf"
copy "O:\Finance file\AP\Invoices\DATAFLOW BUS.SYSTEMS_156290_2015-05-20_7.50.pdf" "O:\Finance file\AP\Check Run Merge\142003.pdf"
I have just run a batch file from task scheduler using the following settings for the Action on Windows 7. The main part I got wrong the first time was not running it using cmd.exe. Also adding the /c so that the cmd closes after the batch file has run (that is probably why it takes forever).
I made my batch file write to a log file as follows so I could see that it was running:
time /t >> C:\temp\mytestlog.txt
just run it as a cmd.exe with the bat file as an arg. you could also short cut it and run that

Run an input file using an exe file with cmd

I am using Windows 7
How can i run an input file (text file of commands) in an exe progam in CMD please.
Using other questions on the site, i have tried:
CMD /c ""C:/Program Files/Mplus/Mpluswin.exe" "C:/Users/jj/Desktop/mplus/test_mplus.inp""
which opens the input file in the program but does not run it
and this, which opens the program, but not the script
CMD /c "C:/Program Files/Mplus/Mpluswin.exe" < "C:/Users/jj/Desktop/mplus/test_mplus.inp"
Does this depend on the exe program?
Edit:
At present, the first command above launches the exe program and opens the text file within it (this is a file of program specific commands that will read in data, run calculations and output automatically). I can then run the commands in the exe program that has been opened (by selecting run in a menu) . But, I would like to pass the file to the exe program and it to be run automatically, ideally in the background. I am not sure of the correct terminology to use, so sorry if my description is unclear.
I've just noticed that you enclosed the entire term in an extra set of double quotes, and used linux forward slashes - try this batch file and also see if there is any error message on the console.
#echo off
cd /d "%userprofile%\Desktop\mplus"
"C:\Program Files\Mplus\Mpluswin.exe" "test_mplus.inp"
echo mplus was launched
pause

Run a batch file from Task Scheduler is not working with a java command

Run a batch file from Task Scheduler is not working with a java command inside the .bat file. If I run the .bat file manually its working good.
Here is the simple .bat file I'm trying to schedule
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_24;
set CMD= "%JAVA_HOME%\bin\java" -version
echo %CMD%
%CMD%
When you type batchfile.bat on the command line, you are telling cmd.exe to read the file and execute each line it finds in it. When you double-click on your batch file in explorer, it calls cmd.exe for you, after reading the file associations in the registry.
Task Manager is not so kind.
So for your task to work, schedule it like this (from memory, not on a Windows box right now) :
cmd /c "c:\full\path\to\your\batchfile.bat"
For extra robustness, you could make sure you batch file run from a known directory, like the one that it reside in, by adding this at the top:
pushd %~dp0
REM .... The original batch file goes here ....
popd
And finally you could disable CMD autorun entry by adding /d right after cmd like this:
cmd /d /c "c:\full\path\to\your\batchfile.bat"
If ixe013's suggestion doesnt work go to
'Actions'
'Edit' the task
'Start in (optional):' Put the path to the directory where the script is
So for the last one if you have 'C:\Users\Desktop\script.py' just put in 'C:\Users\Desktop\' in the 'Start in (optional):' field
What worked for me was running the task as "Users" ( computername\Users ). Once I did that, and "run with highest privileges" checked, it ran without a hitch.
Giving the full path of java.exe in the batch file fixed it for me. In a notepad, I typed the following line:
"C:\Program Files\Java\jdk1.8.0_40\bin\java.exe" -jar "C:\Users\usernameXXXX\Documents\NetBeansProjects\JavaApplication5\dist\JavaApplication5.jar"
Save this as a app1.bat file (C:\temp\app1.bat)
In the Actions tab of the task scheduler, give the path to the batch file, i.e, C:\temp\app1.bat
Also, be careful in the Conditions tab of task scheduler- make sure you uncheck "Start the task only if the computer is on AC power"
All other ways did not work for me, I followed this guide:
http://richardstk.com/2012/06/15/scheduled-task-to-run-a-batch-file/#comment-6873
In order to get the batch file to run, I had to set the "Program\script" box to contain just the name of the script (ie. script.bat) and set the the folder path of the script in the "Start in (optional)" box
I gave full permission to user Everyone from security tab from Properties of the folder in which batch file is. and it started working.
What a coworker discovered on something he had that wasn't working, and I have verified on the system I had that wasn't working is the following:
When the whole task is initially setup, you HAVE TO initially use the radio button "Run only when user is logged on". It will ask for your password for the change.
Now run the task.
Verify that whatever the batch was supposed to do, did happen.
And THEN change to the radio button BACK TO 'Run whether user is logged on or not."
This solved a problem for both of us that we had individually been working on for hours.
Side notes: both issues were also trying to elicit a 3rd party FTP app (WinSCP and WinFTP respectively) in each of our cases. Regular "inhouse" batch/tasks were having no issues.
I had the same problem, and to solve it, I put the next command line into the batch file:
cd "CURRENT_DIRECTORY"
where CURRENT_DIRECTORY is the directory where the batch file is located.
Example:
Suppose i have my batch file named test.bat located into c:\windows\system32\mytest
in my test.bat file, i introduce the next command line:
cd c:\windows\system32\mytest

Resources