Bash Script How do I Open an Executable and Open multiple files within the executable? - bash

My goal is to automatically open a few specific files from calling a bash script.
P:\ = personal directory
Approach 1:
Example Code Below (filename: test):
run "P:\Notepad++\notepad++.exe" "P:\test1.txt" "P:\test2.txt"
Question 1:
When I do "./test" in the bash shell, it opens up Notepad++.exe with test1.txt and says "P:\test2.txt" doesn't exist. Create it?"
I have both test1.txt and test2.txt in the P:\ drive so I'm not sure why "test2.txt" doesn't exist. Is it only allowed to take one parameter?
Approach 2:
I tried to use an array approach, but it's not working.
array = ("P:\test1.txt" "P:\test2.txt")
run "P:\Notepad++\notepad++.exe" $array[*]
Now it opens up Notepad++.exe with none of the files open and says "P:\test2.txt)" doesn't exist. Create it?"
echo ${ARRAY[0]} Prints the entire array as a string... not sure why
SOLVED See answer below.
Thanks all who helped. Any elegant approach/solution is appreciated too :)

Not sure about the parameters of the run command in bash, but it does seem like there is a need for a filler if you want to open multiple files within any executable.
Solution:
run "P:\Notepad++\notepad++.exe" "P:\test1.txt" "P:\test2.txt" ""
The "" is some kind of filler but ends up allowing you to open notepad++.exe and the two text files.
This is for automation purpose :) Hope it helps other users!

Related

How do I automate an input answer in a batch file

So I have been working on a simple batch file, that thanks to #Magoo, I was able to solve the first question I had about it.
Now my next question,
Is there a way to automate an answer input.
Since I'm running Windows 7 (this is the current course I am on, in my quest to become a knowledgeable IT guy), I cannot use PowerShell to extract an image, and to my best knowledge, PowerISO is the only program I have found where I can use command lines in a batch file to extract the .iso file that I want, and place it on the drive/directory that I want. However, I came across the need to input an answer 'NoAll' before I could finish the extraction. I'm wondering if there is a way to automate that answer, if needed. It will only be used once, as the NoAll implies.
All the code is correct and the batch file works properly barring this one little hiccup.
Here's my batch file.
echo
cd "C:\Program Files\Windows AIK\Tools\PETools"
call copype amd64 "C:\winpe-amd64"
copy "C:\winpe-amd64\winpe.wim" "C:\winpe-amd64\iso\sources\boot.wim"
copy "C:\Program Files\Windows AIK\Tools\amd64\imagex.exe" "C:\winpe-amd64\iso" & cd "C:\Program Files\Windows AIK\Tools\amd64"
oscdimg.exe -n -bC:\winpe-amd64\etfsboot.com c:\winpe-amd64\iso c:\winpe-amd64\winpe-amd64.iso
cd 'C:\program files\powerISO"
piso extract "C:\winpe-amd64\winpe-amd64.iso" / -od f:
as the extract begins to run, I get a prompt for input, and this is where I would like to be able to automate the 'NoAll' answer.
Thanks in advance!
Since all you need to do it input text, you can simply echo what you want to type and then pipe it to the other command.
echo NoAll|piso extract "C:\winpe-amd64\winpe-amd64.iso" / -od f:
You can use the input file and input redirector '<' to automate most of input. For example, create a text file 'NoAll.txt' with the content "NoAll" (without quotes, and add a newline at the end for 'Enter' key). On the command that needs NoAll, execute it as follow:
piso extract "C:\winpe-amd64\winpe-amd64.iso" / -od f: < NoAll.txt

Is there a way to run a command line "inside" a symlink?

I have a symlink named example.avi that points to a real example.avi video file. When a program tries opens the symlink, it really opens the video file. But now I would like execute a command line whenever a program tries to open the symlink file.
Is that possible?
ps: windows
No, there is no built-in way of creating a symlink or other file system object that causes a command to be executed when it is opened by an application.
It should in principle be possible to do this with a file system filter driver. But that's very complicated. I believe there are some third-party tools to simplify this sort of task, but I don't have any experience with them.
While I am clearly ignorant on the subject of symlinks in Windows (see my comments on your question). I just played with it and proved that you could basically do this by symlinking to a wrapper for your avi. I.e. symlink to an exe or a batch file, etc. which does what you want and then opens the avi. Here's a test I ran with txt files and notepad:
Create a file called test.txt with some text. Create a file next to it called test.bat. Here's the batch:
notepad test.txt
When you run the batch, it just opens the txt in notepad.
Then I added a symlink:
mklink test2.txt test.bat
Now, when I type test2.txt in the command prompt, or double click on it, it runs the batch and opens the test.txt file. Obviously, you can use the same basic logic. It doesn't, however, fire the batch off when I open the symlink in Notepad++. It just opens to batch for editing instead.
Of course, maybe you don't want a second file, in which case you need to literally embed your avi in some wrapper. I guess we ned to know more about what you want to do. It sounds like an attempt at malware hidden in a video to me...

How To Add A Script To Add Functionality To the Terminal?

I have a small perl script ( found here ) which adds command line functionality to an application I already have installed, Coda. Basically it will open a file with the application when I type:
coda filename.py
Where (on OSX) do I need to put this file to make it function? Do I need to do anything else to my environment to get this working?
Type echo $PATH at the terminal. You will get back a series of paths separated by colons. The file needs to be placed into one of those folders. The file also needs to have the execute flag set, which is done with the chmod tool.

starting a windows executable via batch script, exe not in Program Files

This is probably batch scripting 101, but I can't find any clear explanation/documentation on why this is happening or if my workaround is actually the solution. So basically any terminology or links to good sources is really appreciated.
So I have a program I want to execute via batch script (along with several other programs). It's the only one where the exe is not in a Program Files folder. I can get it to start like this:
C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe
But I get an error along the lines of:
Run-time Error '3024':
Could not find file
C:\Users\MyUserName\Desktop\ModuleSettings.mdb
So it seems that the program is looking for its settings files from the same location that the batch script starts up. Given that I finally got everything to work by doing the following:
cd C:\WeirdProgram\WeirdProgramModule\
weirdmodule.exe
That works fine, and it's not the end of the world to have to go this route (just one extra line), but I've convinced myself that I'm doing something wrong based on lack of basic understanding.
Anybody know or can point me to why it works this way?
Oh, and doing the following:
start "C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe"
doesn't do anything at all.
Thanks,
you are doing it perfectly :-)
the executable is probably looking for this file in the "current working directory", which is being set, when you "cd" to it before.
you can set your working directory manually by creating a shortcut to your batch file; right click; properties.
edit:
you can also set your current working directory using the start command:
start "Title" /D "C:\WeirdProgram\WeirdProgramModule\" "weirdmodule.exe"
edit:
If you like to pass params, just add them to the executable filename as you would in a regular shortcut:
start "Title" /D "C:\WeirdProgram\WeirdProgramModule\" "weirdmodule.exe" "param1 param2"
or
start "Title" /D "C:\WeirdProgram\WeirdProgramModule\" "weirdmodule.exe param1 param2"
For reference, the syntax is described here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true.
What's happening is that weirdmodule.exe is looking in the "current directory" for the .mdb file. You might be able to tell it where to find the .mdb file through a command line parameter or some other configuration method (registry or .ini file maybe). How you'd specify the location is entirely up to the weirdmodule.exe program, though.
Other than that, your current workaround is probably what you're stuck with.
As far as your problem with using start.exe... the start.exe program has the very, very odd behavior (bizarre behavior in my opinion) of treating the first parameter as the 'title' to put in the window if (and only if) the first parameter is in quotes. So you have a couple of options:
Don't use quotes to specify the program. This works for you because you don't need quotes (there aren't any spaces or other special characters in the path that would require quoting it):
start C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe
Give an empty (or some other string) title as the first parameter. This is something you'd have to do if your path required quotes:
start "" "C:\WeirdProgram\WeirdProgramModule\weirdmodule.exe"

How do I open a file using its default application from Perl on Windows?

I have a directory of files that I would like to scan on a regular basis and execute with the default application they are associated with. They are not executable so system("file.torrent"); does not work. How are you able to run files with there associated applications in Perl?
The standard windows way is with ShellExecute.
In perl you can do it with, well, ShellExecute. Its in the Win32::GUI package.
Have not tried it. But it looks simple enough.
start
You could manually parse the relevant part of the registry, find the associated application, and kick it off yourself: but the command prompt's built-in start command life easier.
So, for your example you would simply do a system("cmd /c start file.torrent")
Not Perl specific but you can always use the 'start' command. The first argument will be the title of the new command prompt opened and the second argument is the file to open.
system('start "dummy title" "some file.doc"'); # opens the document in word
Another option we use is
system("RunDLL32.exe SHELL32.DLL,ShellExec_RunDLL file.torrent")
Never use system() on windows !
Crappy and bad method (PAS)
Just to highlight the comment by BeowulfOF above
system( "test.log" )
will open test.log in it's associated application just the same as entering
test.log
on the command line

Resources