Running cmd scripts from a ruby file? - ruby

Is there anyway to write commands to the command prompt in windows and execute directly from a ruby program?
I would use this as a one click installer for all the gems I wanted to install on the computer after installing ruby. I hope that it would save time when transferring my ruby files to a new computer. Or would be an easier way to get a non-ruby person set up very quickly with all the gems I thought them might need.
I am imagining something like Watir but for the Cmd rather than a browser.
EDIT
Thanks to
How can I then close the cmd window without closing the program for instance:
'notepad'
starts a cmd window and it also starts notepad but the cmd windows stays until the notepad is closed.

Ruby will execute anything you put in backticks ` in your associated shell.
so if you type
test = `ipconfig`
puts test
test should now have stored in it the data from the cmd call ipconfig
EDIT
You can also use the System(..) call in Ruby to execute commands

Related

Running keystrokes from program

I'm writing a program to automate an e-mail process and would like to know if there's a way to run keystrokes from within the program?
For example say I have this string:
str = "test"
And it gets copied to a file:
File.open('str.txt', 'w') { |s| s.puts(str }
And after that I want to use CNTRL-A; CNTRL-C on the file and copy the information, is this possible in a Ruby program, without the use of external gems?
Operating system: Windows 7
If sending arbitrary keystrokes to other applications is what you're after you can use the gem https://github.com/erinata/auto_click for it. However, if you can't use gems, what you can do instead is run NirCmd (or one of its alternatives) with the appropriate command line arguments to achieve the same result.
For example:
# Tell the OS to bring up the Notepad window and give it the time
# to do so.
`nircmd win activate ititle notepad`
sleep 0.5
# Select all text in the Notepad window and copy it to the
# clipboard.
`nircmd sendkeypress ctrl+a`
`nircmd sendkeypress ctrl+c`
If you can't install gems but can copy ClipText.exe to your current directory, do so then run this code in Ruby:
File.open('str.txt', 'w') { |s| s.puts(str }
`cliptext.exe from str.txt`
For a more rigorous way of executing commands on Windows see "How to execute Windows CLI commands in Ruby?".

How to get files and directories set up for Ruby?

I am doing prep work for App Academy but I am having a hard time setting up files/directories to be able to run everything correctly. I have a couple questions and haven't been able to find concrete answers:
How do you make a directory in the windows terminal?
How do you run files previously written in Notepad in IRB? I wrote some simple scripts in Notepad because Sublime was causing me severe migraines. Or how do you change Notepad files to Ruby files?
How do you create new files in IRB like test_code.rb?
1) When at the Windows command prompt and having navigated to your working directory, type md directory_name
2) Unless you have specifically told notepad otherwise, notepad will have saved your file as a .txt file. You will simply have to rename the file extension from *.txt to *.rb.
3) When you installed Ruby on your Windows machine, the Ruby interpreter would have undoubtably been added to your path, so you should just be able to run your *.rb file direct from the windows cmd prompt and it will execute.
To load it while in IRB: Make sure you run IRB from the same folder as your *.rb file is in. Once you have cranked up an IRB session, type load 'my_file.rb'.
IRB is a great environment for testing code, but not for writing full scripts. Use Notepad or Notepad++ or Vim for Windows or your editor of choice, as long as it's capable of generating a text (non word processing document).
You can make a directory in the terminal or in the Explorer, it doesn't matter. Just note where you created it so you don't lose it.
If you want to run a script in Ruby, simply type ruby /path/to/the/file/script_to_run.rb and the Ruby interpreter should load and run the file.
You can load a script into IRB and watch it run, but that's rarely something we need to do. More often you'll want to run scripts using Ruby, and try things in IRB, since it's like a scratchpad.

How to launch a program from perl?

How do i launch firefox from perl? i just need to launch the browser so WWW::Mechanize::Firefox can manipulate it. Searching around stackoverflow ive seen a few solutionsl like system('start cmd.exe /k $cmd) where $cmd is arguments to throw as input once cmd is started.
However, these have not helped me to solve my problem at all.
solutions ive tried
system("start cmd.exe /k start firefox");
system("firefox");
system("cmd","start","firefox");
system("cmd start firefox");
Basically a lot of the alternatives ive found, but i could not launch Firefox browser at all.
You're on the right track. Your second line is almost correct. If firefox is not in your PATH environment variable, you need to supply the complete path.
Click on the Firefox icon on your desktop, open the properties and check where the firefox executable is located. Then use that with your system call.
For me, it looks like this (the ' are for Perl's string, the " are for the Windows shell, because the path has spaces in it):
system('"C:\Programme\Mozilla Firefox\firefox.exe"');
You can test it by opening a new command line (win + r, cmd), cding to the directory where your Perl program is run from, and just entering the command:
C:\Dokumente und Einstellungen\simbabque>"C:\Programme\Mozilla Firefox\firefox.exe"
It will not print anything, but just open a new Firefox window after a couple of seconds. So you'd probably need to hold your program execution in Perl while the browser is starting up.
WWW::Mechanize::Firefox will launch firefox for you but you can use
system 1, qq{$ENV{PROGRAMFILES}\\Mozilla Firefox\\firefox.exe}

how to make my console in python not to close?

I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.
I don't know why this is happening. I need your help.
Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...
raw_input()
... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.
Run your program from a Windows command prompt. That will not automatically close when the program finishes.
If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).
Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:
python main.exe
pause
Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.

Batch Scripts - Need to specify to use an ANSI console and execute a command

This is a noob question at its best but Google isn't finding what I need.
I have a ruby script that I need to fire off via task scheduler. I thought I did it right by using the following:
cmd /k ruby test.rb
This works when starting the .bat file from the folder. However, if it runs from taskeng.exe it fails because its looking in my system32 folder. I don't want it to run from system32 so how do i format this to run from say, c:/dev/
Again, sorry for the extremely noob question.
You can leave out cmd of that and just use
ruby test.rb
or rather (in your case):
ruby C:\Users\Foo\test.rb
or something like that. Giving the complete path to the script usually helps in finding it ;-)
In any case, if you need the working directory you can set it in the scheduled task itself. Something akin to the following:
       
Likewise, if you actually need cmd in there. Just fill out the Start in field and you have a working directory.

Resources