How to simulate opening of .BAT file without one? - windows

I want to launch several Perl scripts one after another (although, technically in parallel here) automatically.
It is easy to make a routine where the script makes a trivial .BAT file consisting only with the next Perl script, for example:
write_file("Z.bat", "$myfile_nextnumber.pl\n");
system "start Z.bat";
and you get nice output from each script in a separate window.
But how to achieve this without creating any file?
The closest what I got:
system "start cmd /k $myfile_nextnumber.pl";
but in this case, it does not show the path and file that was executed.

Why not just put a print statement before each system call?
foreach my $file (#filelist) {
print "Calling $file\n";
system "start cmd /k $file";
}

Related

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

cmd.exe doesn't terminate when using a .bat file

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
If start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat

help with windows batch scripting basics - execution and calling a separate executable within the script

Newbie to windows scripting. I need help running the .bat file on the command line so I can test it.
I used Text Document as my editor to create the file (opens up also as Notepad).
I performed file "save as" (ALL FILES). If I open up cmd, I can see the file has a .txt extension (myfile.bat.txt). So if I just type in cmd myfile.bat.txt the editor opens. I am not sure how to execute this correctly.
As for the logic in my batch script, I am basically logging into a remote directory (already created the net mount) and now I want to:
run an executeable file
rename some files.
With some research, I written this so far. I have saved it as a .bat file
# echo off
echo This is a batch file to run an executable and rename some files
pause
--run executable file here, just don't know how to do it
x:
cd x:
rename fileA fileB
Any help, good tips/practice would be great. Thanks.
Type in this command in cmd window:
rename myfile.bat.txt myfile.bat
Now you can run the script by simply invoking:
myfile.bat
or
myfile
(provided there's no myfile.exe or myfile.com in the same directory).
If you need to edit the script further, you can either right click it in Explorer and choose Edit or call the editor from the command window:
notepad myfile.bat
To call a program from the script, simply add its name, if it's in the current directory:
someprogram.exe
or the name with the path, if it's somewhere else:
directory\program.exe
or
d:\directory\program.exe
If the name or the path contain spaces, be sure to enclose the entire name & path string in double quotes:
"d:\directory\program name.exe"
you can just type the full name of the program
eg
"c:\program dir\program.exe"
or you can add the program directory to your path environment variable
set PATH=%PATH%;"c:\program dir"
and just type the program name
program
you can also edit your PATH variable in windows http://support.microsoft.com/kb/310519
NOTE: When you save the file in notepad, you want to save it as filename.BAT and select All Files from the second dropdown. If you don't it still gets saved as a .TXT.
A couple of command to consider:
CSCRIPT cscript /? in CMD
START http://ss64.com/nt/start.html
If you're doing say a VBSCRIPT use CSCRIPT to start it. If you're trying to execute another BATCH script or an EXE, use START

How do I make a shortcut for a Perl program under Windows using a batch file?

I'm trying to "hide" some of my Perl program from the end user to make things easier on them. I'm doing what I can to keep them out of the command prompt. The program itself has a GUI designed in Perl/Tk, so they don't have to worry about the command prompt.
Could I write out a quick batch file that goes along the lines of:
START perl 'C:\[some path here]\myscript.pl'
with START to start a program, the Perl interpretor as my program, and the path/name of my Perl script as the parameter?
Would I have to specify where to find perl or would Windows just know because perl is in the computer's PATH variable?
I have a totally evil VBS script and batch file to deal with this kind of thing with Strawberry Perl Portable. The VBS script runs the batch file without spawning a cmd window. The batch file works out where it is, where perl.exe is relative to it, and runs the perl script. In this case it's to run a Catalyst server, but can be used for anything else too.
Here's the vbs (shudder):
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & fso.GetParentFolderName(wscript.ScriptFullName) & "\perlshell.bat"& Chr(34), 0
Set WshShell = Nothing
And here's the batch script (only slightly less shudder):
echo off
set bindir=%~dp0
set perlpath=%bindir%perl\bin
set buildpath=%bindir%\bin
set PATH=%PATH%;%perlpath%;%buildpath%
"%perlpath%\wperl.exe" "%bindir%MyPhp\script\myphp_server.pl" -p 35900
you don't need "start" for this. either add perl.exe from your path or specify the full path to the perl interpreter.
so ... put this in your batch file:
c:\program files\perl.exe "c:\whatever\myscript.perl"
-don
If the idea is to keep them away from the command line, why use a batch file at all? Why not just make a shortcut? (You could use Win32::Shortcut if you don't want to make one by hand.)
There's an easier way. For windows:
Go to the desktop -> right click -> create a shortcut
make your target look like (i.e. the path to the perl.exe file) something like the following - I'll show you what mine looks like
C:\Strawberry\perl\bin\perl.exe
In my case I have Strawberry Perl installed
right click desktop shortcut -> click properties
In the target input box, alter it to look something like mine:
C:\Strawberry\perl\bin\perl.exe D:\Developer\CustomTools\login.pl
where login.pl is the absolute path to my perl script
from there you should have a shortcut created to allow execution of a perl script.
In case you might be interested:
in the properties popup you can also assign this invocation of the perl script to a keyboard button press and assign it a shortcut key to launch anytime ^_^
For me, I chose to do:
ctrl+alt+i
to stand for login - to invoke the login.pl script I wrote. be aware of default system keyboard combinations
If you have any questions, let me know!
Thanks for you time!

How do I create drag-and-drop Strawberry Perl programs?

I've got a Strawberry Perl program that accepts a single-file as a command-line argument. How can I set things up such that I can drag and drop the desired file onto the Strawberry Perl program (or a wrapper around it) and the program runs with that file's name as an argument?
Under Windows (tested with XP), you can create a .cmd file and simply have it run the Perl program with the argument of %1 to pass the filename over, as if executed by commandline.
perl c:\test.pl %1
Then you can simply drag and drop a file onto the .cmd file to execute.
Eeek! Please don't create a wrapper script/cmd when you don't need to.
Go into your Registry or your File Type dialog box in Windows, and redefine the Perl default action to say:
"C:\path-to-perl-folders\perl.exe" "%1" %*
This will cause double-clicking the .PL to launch perl.exe with the name of the double-clicked file (%1). The %* stuff (passing any filename arguments to the Perl script) is trickier.
Go into the Registry again (really, it's not as scary as people think) and find/create a "shellex" key under the Perl class, and then create a sub-key called "DropHandler" with a default string value of "{86C86720-42A0-1069-A2E8-08002B30309D}" (at least, that's my DropHandler in the US version of Windows XP).
This allows .pl files (actually, anything associated with the Perl class) to have a drop handler that tells Explorer what to do when you drop file(s) on the .pl script. In this case, it just means "run the Perl script with the dropped file(s) as arguments".
Hmmm, I don't think I explained that very well, but that's how I've set up Perl (running off a network drive) for a large engineering organization. Google for Perl and DropHandler, and you should be able to get the .reg Registry script to do this for you.
Here's another alternative to a "wrapper", but it requires a slight modification to the perl script:
Rename your script.pl script to script.cmd.
Add the following to the top of the file:
#SETLOCAL ENABLEEXTENSIONS
#c:\path\to\perl.exe -x "%~f0" %*
#exit /b %ERRORLEVEL%
#!perl
#line 6
# ...perl script continues here...
The script is run like any other batch file. The first three lines basically invokes Perl on the CMD file itself (%~f0, which only works if CMD extensions are turned on). The -x paremeter to perl.exe tells Perl to skip everything until the #!perl line. "#line 6" just aids in debugging.
This is my preferred solution when I don't know much about the target system (and may not be able to edit the registry).

Resources