Windows shortcut or batch file to open command prompt in ruby directory with a pre populated string - ruby

I have just started using Compass within Ruby on Windows. In order to watch a directory for changes, I need to run cmd.exe in the Ruby directory and then use the command "compass watch C:\path\to\project".
I wish to make a shortcut that will open cmd.exe in D:\ruby\bin\ (Ruby directory) with a pre-filled string that I can simply complete and then press enter to execute. For example:
C:\ruby\bin>compass watch D:\path\to\_
...and then I simply type the project folder name and press enter.
I can create a shortcut that runs cmd.exe in C:\ruby\bin\ but I then have to type the rest.
I can create a batch file that runs cmd.exe in its default location and then changes directory to C:\ruby\bin, then executes compass watch C:\path\to\project
I can't figure out how to pre-fill the above command and have cmd.exe wait for me to input the relevant project directory before manually pressing enter to execute.

It's a pretty simple one liner...
Open Notepad or other text editor
Change directory (cd) to your Ruby directory
Execute 'compass watch'
cd C:\ruby\bin
compass watch
Go to Save As
Change 'Save as type' to 'All files'
Save as compass.bat
Run the file (double-click) to start polling for changes
Source http://omnitech-support.blogspot.co.uk/2013/04/how-to-create-batch-files-for-windows-8.html

Create a new shortcut wherever you want to be able to run it.
For the command line enter cmd /K compass watch.
Call it whatever you like (I suggest 'Compass Watch') and save it.
Right click on it and choose properties.
Change the "Start In" to the folder where your config.rb is.
You can also set the "Run" option to Minimized.
Save the changes to the shortcut.
Run the shortcut to set Compass watching for changes in the relevant files in the folders defined in your config.rb.

Related

Running a selfwritten ruby program outside of an IDE

I was wondering if it was possible to run a selfwritten ruby program just like any other program by double-clicking an icon of some sort.
And if it's possible, how do I do it?
I wrote a little program for a friend but I don't want him to have to use the command line to run it, because that's rather inconvenient (unless there is a way to just double-click and the command line opens the program itself..).
Thanks for your help!
The simple answer that should work for all versions of Windows is to just create a simple batch launcher.
Create a .bat file. I usually just create a new .txt file via "right click > new > text document". Then rename it, highlight everything, including the extension, and rename it to something like run.bat. The .bat part is important. Once you rename it, the icon should change to gears. If you can't overwrite the extension, or Windows is still treating it as a text document, you'll need to either manually save it as a bat, or disable "hide file extensions" in the explorer settings so the extension can be changed.
Edit the bat file, and put into it something like:
#echo off
YOUR RUN COMMAND HERE THAT YOU WOULD NORMALLY TYPE MANUALLY
pause
Paste the command that you would normally run manually where the capital text is. The first line is so it doesn't repeat the commands back, and the pause is so if an error happens, the command prompt doesn't immediately close. This gives you a chance to read the error.
Save it and close it. Now, if you double click on the bat file, your program should run.
Multiple ways
if it's for occasional use and for one script only I would pack it
to a Windows executable with Ocra, then you can double click
the .exe itself or a link to it
same as above but use jRuby and create a .jar file, not for beginners though
the easiest: if you configure Windows to start/run .rb files with your ruby.exe you can double click the .rb files itself and they
will execute, they will have the red Ruby stone icon
if you run a .reg file to enable drap and drop on .rb files you can combine the previous technique to drop files on the script and
they will be the parameters to the script, see my answer here for the reg file
my favorite: copy the .rb to your windows "C:\Users\your_user\AppData\Roaming\Microsoft\Windows\SendTo\"
folder, then you can right click file(s) or folder(s) and select
sendto and select your script, the files or folder will again be the
parameters for your script
you can create a .bat or .cmd file that starts with the path to your ruby.exe and the script as parameter, use rubyw.exe if you
don't want output

How can I open command prompt in the current directory as a shortcut

I'm just wondering how I can make a shortcut that will open command prompt in the folder it is currently in.
Why I need this.
I save my c program file in the directory Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
So I would have to type cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\ every time I want to open the command prompt in that directory.
Is there a shortcut I can make where I can move that shortcut around anywhere (don't need to change the shortcut) and it will open command prompt in the directory it is current in, so I don't need to keep typing cd .....
you can go to the directory and just type start . in the same directory
or you can make a batch file like test.bat and add below line in the same:
cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
Save it anywhere and whenever you will run that batchfile, it will automatically move PWD to the required directory
Right-click in the open space of your desktop and click New > Shortcut
For the location, type or copy and paste one of the following lines.
%windir%\system32\cmd.exe /k cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
As commented by aschipfl, you can already do it, (may be OS dependent)!
In Windows Explorer, or I suppose the desktop, just hold down the shift key, right click in a blank area of it and choose Open command window here, (or whatever your locale states).
I think in Windows 10 at least, the option is even available in the Explorer toolbar menu too.

How to make the cmd line executable

Here's the situation, every time I need to do the step below after I boot my computer:
Open the cmd(admin)
copy this cmd into cmd:
C:\Windows\system32\netsh int ipv4 set glob defaultcurhoplimit=65
enter
For now, I hope to double click a file that can directly execute the command above without the step above.
Any method?
You can save it to .bat or .cmd file and run it on double-click.
Create a file with any name and extension .bat I.g. setLimit.bat. I assume you know how to create files.
Open file from above step in file editor, i.g. notepad.exe. Copy and paste line C:\Windows\system32\netsh int ipv4 set glob defaultcurhoplimit=65 into the file and press Ctrl+S to safe it. I hope it makes sense otherwise ask.
Double click on the file to execute the command.
If you need to run it under elevated privilege (i.e. admin) right click on file and choose Run as administrator option in menu.
You need a .bat file to execute the command and a shortcut to have it executed as administrator.
Creating the .bat file is trivial: use you favorite text editor (or notepad), type the command, optionnaly add a line PAUSE if you want to see eventual messages and save it to disk with a .bat extension (say c:\...\sethoplimit.bat)
Creating the shortcut is not much harder: right click on desktop, Create new..., Shortcut, and just select the newly created file
The magic comes now: right click on the newly created shortcut and click Properties, in Shortcut tab, click the Advanced button and select Execute as admin.
That's all. Each time you double click on the shortcut, Windows will start a console with elevated priviledges, eventually open the User Account Control confirmation dialog, and execute the batch file as administrator.
Here I showed you how to create the shortcut directly on the Desktop, but the same process can be used for a shortcut anywhere on a disk folder.

Executing a command-line .exe file

I have a .exe file converted from a .jar.
It is a command based application, so I have to start it with a batch script. Here is the batch script:
#echo off
cd C:\desktop\plant-text-adventure-win
start planttextadventure
pause
When I double click on the batch script, this happens: Windows could not find 'planttextadventure'. Please confirm if you have input the correct name and retry.
I don't know what is happening, I have no idea about cmd as I use Mac, but I can confirm I have an executable called planttextadventure.exe in a folder called plant-text-adventure-win.
You should test your batch file by executing it within a shell.
Simply enter within the start menu the command cmd to open up a shell. Within this black box you could now simply enter the commands from your batch script and lookout for some error message.
If you look at your script I would guess that the cd command (to change the current directory) is not correct. Maybe you should replace it with
cd %USERPROFILE%\Desktop\plant-text-adventure-win
because the desktop folder is on a default installation not directly under the root drive but within the user profile available.
Another solution to get this thing to work, is by opening the windows explorer, going to the .exe file you wish to execute and drag & drop the .exe file with a right mouse click onto the desktop.
Then a context menu appears and you select the option Create shortcut here.

BAT file to open CMD in current directory

I have many scripts which I interact with from the command line. Everytime I need to use them, I have to open a command line window and copy+paste and CD to the path to the directory they are in. This is tedious (they are in a rather deep file system, so typing out the full path is a pain, copy+paste is better but not much). I tried to create a .BAT file that I could double-click on that would open a new command-line window in the folder the .bat file exists in but it does not work. It opens a new window, but the working directory is not the directory that .bat file is in. Here's what I've got after much googling (My cmd skills ain't so great):
cd %CD%
cmd.exe
I know from when I used Linux that Konqueror had a "Command-line window here" feature, and that's the effect I'm trying to get on Windows.
you probably want to do this:
cd /d %~dp0
cmd.exe
this will set your current directory to the directory you have the batch file in
Create a file named open_dos_here.cmd with the following lines:
%~d1
cd "%~p1"
call cmd
Put this file at any folder.
Then, go to your Send To folder (Win+E; Alt+D;shell:sendto;Enter).
Create a shortcut to point to this open_dos_here.cmd
Then, in any folder, select any file or sub-folder. Right-click and select "Send To" and then select open_dos_here.cmd to open the DOS in that folder.
You can just enter cmd into the address bar in Explorer and it starts up in that path. Likewise for PowerShell.
There's a simpler way -
start /d "folder path"
As a more general solution you might want to check out the Microsoft Power Toy for XP that adds the "Open Command Window Here" option when you right-click: http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx
In Vista and Windows 7, you'll get that option if you hold down shift and right-click (this is built in).
I'm thinking that if you are creating a batch script that relies on the Current Directory being set to the folder that contains the batch file, that you are setting yourself up for trouble when you try to execute the batch file using a fully qualified path as you would from a scheduler.
Better to add this line to your batch file too:
REM Change Current Directory to the location of this batch file
CD /D %~dp0
unless you are fully qualifying all of your paths.
Another solution is to use a shortcut file to cmd.exe instead of a batch file.
Edit the shortcut's start in property to %~dp0.
You achieve the same thing, except it has the Cmd icon (and you can change this).
Some people don't like clicking on batch files without knowing what's in them, and some corporate network drives have a ban on .bat files...
The simplest command to do this:
start
You can always run this in command line to open new command line window in the same location. Or you can place it in your .bat file.
Most simple way in explorer is to Shift + right mouse click on the folder or on an empty space in the folder and click on Open command prompt here.
CMD will then start in that folder
I must say, I'm not sure if it works for Windows Vista and below, but it surely works for Windows 7, 8, 8.1 and 10.
Referring to answer of #Chris,
We can also go to parent directory of batch file and run commands using following
cd /d %~dp0..
<OTHER_BATCH_COMMANDS>
cmd.exe
To understand working of command cd /d %~dp0.. please refer below link
What does it mean by command cd /d %~dp0 in Windows
You could add a context menu entry through the registry:
Navigate in your Registry to
HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell
and create a key called "Command Prompt" without the quotes.
Set the default string to whatever text you want to appear in the right-click menu.
Create a new key within your newly created command prompt named "command," and set the default string to
cmd.exe /k pushd %1
You may need to add %SystemRoot%\system32\ before the cmd.exe if the executable can't be found.
The changes should take place immediately. Right click a folder and your new menu item should appear.
Also see http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm
When you are in the desired folder , just type CMD in your address bar
A bit late to the game but if I'm understanding your needs correctly this will help people with the same issue.
Two solutions with the same first step:
First navigate to the location you keep your scripts in and copy the filepath to that directory.
First Solution:
Click "Start"
Right-click "Computer" (or "My Computer)
Click "Properties"
On the left, click "Advanced System Settings"
Click "Environment Variables"
In the "System Variables" Box, scroll down and select "PATH"
Click "Edit"
In the "Variable Value" field, scroll all the way to the right
If there isn't a semi-colon (;) there yet, add it.
Paste in the filepath you copied earlier.
End with a semi-colon.
Click "OK"
Click "OK" again
Click "OK" one last time
You can now use any of your scripts as if you were already that folder.
Second Solution: (can easily be paired with the first for extra usefulness)
On your desktop create a batch file with the following content.
#echo off
cmd /k cd "C:\your\file\path"
This will open a command window like what you tried to do.
For tons of info on windows commands check here: http://ss64.com/nt/
Create a new file startCmdLine.bat in your directory and put this line in it
call cmd
That is it. Now double click on the .bat file. It works for me.
You can replace call with start, it will also work.
this code works for me
name it cmd.bat
#echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop
you can try:
shift + right click
then, click on Open command prompt here
Inside given folder click on the top Adddress Bar and type cmd and click enter
It will open command prompt with current folder address.
You can simply create a bat file in any convenient place and drop any file from the desired directory onto it.
Haha. Code for this:
cmd

Resources