Windows: script/program for USB key that opens a console, and sets the path and the working directory
The goal behind this question is to create a portable Windows script/program that would help users run开发者_Go百科 (console) programs directly from a USB key (not necessarily through autorun, though). The script/program would thus do the following when double clicked on:
- Launch a console (
cmd
would do) and give a DOS command prompt, - Have the current directory set to a specific directory
WorkingDir
on the USB key (which contains various [Python] programs), - Set the path so that the user can run a command (python.exe) found in another USB key directory (so that the user can launch various Python programs found in
WorkingDir
).
The whole thing is based on Portable Python, which is on the USB key. I would also like to be able to simply put the contents of the key on a hard drive and use it from there.
I tried to write a file that contains commands like:
PATH=..\"Portable Python 2.7.2.1\App":%PATH%
cd WorkingDir
cmd
but I'm not sure how to call it so that Windows runs it (and I was therefore not able to see whether these command would work).
I don't know much about DOS and Windows, so any help would be much appreciated!
Actually, in windows you use ; for separating paths :) And you shouldn't use .. like that. You can use %CD% to get current directory and then navigate from it. And don't use quotes. Also, you could put @ before any command that you don't wish to be echoed to the console.
You can put this into run.bat (this should work :P):
@PATH=%PATH%;%CD%\..\Portable Python 2.7.2.1\App
@cd WorkingDir
@cmd
And then just double click it and it will open commmand prompt just as you want it. Or maybe you can add autorun.inf file to open it automatically.
You need to create two files:
autorun.inf
[autorun]
open=cmd.exe "Python Console" /k autorun.cmd
action=Open Python Console...
autorun.cmd
@Echo Off
CD %~d0\WorkingDir
Path %Path%;%~d0\Portable Python 2.7.2.1\App
The phrase %~d0
represents the drive the command file resides, namely the flash drive's letter (E:).
Now, I encountered two slight hiccups. My USB drive had a hidden, system, readonly autorun.inf file on it already. I had to unprotect it with the following command before I could edit it.
Attrib autorun.inf -r -s -h
My second hiccup, is that Windows 7 won't autorun from a USB drive. You have to right-click the drive in Explorer and select "Run Python Console..."
精彩评论