windows bat file using start cmd /k with doskey not work - windows

I tried to use start cmd with custom doskey file, but failed.
1.doskey
ls=dir /b $1
start "title" cmd.exe /k doskey /macrofile=1.doskey && ls .
Prompted
ls command is not recognized

Two issues:
Issue 1
The && will be interpreted by the shell you run this command from and not the shell in the window, so you are running doskey /macrofile=1.doskey in the new window but ls . in the old one.
The solution is to surround the whole command that should be passed to the new window with doublequotes:
start "title" cmd.exe /k "doskey /macrofile=1.doskey && ls ."
Alternatively you could escape both & characters with ^:
start "title" cmd.exe /k doskey /macrofile=1.doskey ^&^& ls .
...however, this will just uncover issue 2, so keep reading.
Issue 2
As you can deduce (sort of) from the DOSKEY docs, you cannot run a macro from a command that you didn't type in manually (after all, DOSKEY is a tool to process interactive input from a user):
To run a macro, type the macro name at the command prompt, starting at the first position. If the macro was defined with $* or any of the batch parameters $1 through $9, use a space to separate the parameters. You cannot run a doskey macro from a batch program.
(Emphasis mine.)
This talks only about batch files, but it applies to any non-interactive way of entering a command - and passing it to cmd /c or cmd /k is one of them (since you aren't typing the command into the new shell, it's run automatically on startup of the shell instead).
Based on how you described your intent ("use start with custom doskey file"), I assume you added the ls . only to test whether it works. In that case, just remove the whole && ls . part and try ls . manually in the new window, and you'll see that it does work!
In case that's not what you wanted, and you actually intended to run DOSKEY macros from that "cmd /k" command line or a batch file, you are out of luck - instead, you could call a batch file that contains functions and use that, or create custom batch files instead of macros.

Related

use root cmd window to execute commands in new cmd window

i'm trying to make a batch script for running my Java files. I've found out that there is no way to prevent auto-closure of a batch script(except using pause keyword, tho it just waits for key press). I've also discovered that starting a new window will cause only main windows to close, not the new one too so i want a way that the command SET /P file=Java file: is executed in the new window(I got the new window by using the start keyword. Is there any way to accomplish this without downloading other softwares? this is the code i came up with yet:
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
set /P file=Java file to execute:
java %file%^.jar
start
I guess you're looking for that :
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
start cmd /V:ON /K "#set /P "file=Java file to execute: " && java -jar !file!^.jar"
EDIT: Using expansion with /V and use of /K instead of /C to keep the cmd windows open.
Explanations : To launch a console process in another windows and keep it open after the end of this process console we launch another cmd process with the start command. We use /V:ON to use delayed expansion by default, meaning modified variables (like the one we prompt, %file%) will be expanded on the fly with ! (so !file! instead of %file%). We use /K to tell to this cmd process to not close when provided commands end. To this cmd process, we provide the following commands :
#set /P "file=Java file to execute: "
This one will ask for the jar filename (without extension) to launch.
The # means "do not echo the command itself on the console stdout" for a nice display.
java -jar %file%^.jar
This one launch the java interpreter (JVM) with the filename of a jar file to execute through the -jar parameter, filename build from the previous prompt and the .jar extension. The ^ here escapes the ., it seems not useful here but maybe your script/env requires it.
We link the both commands with && which means : _if left command from && is successful (it exits with ERRORLEVEL 0) then execute the right command from &&.

Making a batch file for multiple CMDs

Lets say I have 2 services I want to start and a property file.
-svc1.cmd
-svc2.bat
-svc2prop.properties
Lets have directories as C:\program1\bin\ and C:\program2\bin\ + C:\program2\config\
then I tried to do it like this:
start cmd /k call C:\program1\bin\svc1.cmd
start cmd /k cd C:\program2\ .\bin\svc2.bat .\config\svc2prop.properties
I can start them both separately by opening a local CMD. The thing is, if I am in the directory C:\program2\ I can open up a local CMD and run this statement without problems " .\bin\svc2.bat .\config\svc2prop.properties"
But I want to create a batch file that: first, opens a new cmd and starts the svc1.cmd, then opens another cmd in which it goes to C:\program2\ and runs the "" .\bin\svc2.bat .\config\svc2prop.properties" " statement ... but for some reason it doesnt work.
Any possible solution ?
the second line should use /D option to start the process in the required directory:
start /D C:\program2 cmd /k .\bin\svc2.bat .\config\svc2prop.properties
(in your example, you were just passing a lot of arguments to an useless cd command)
If you have some current directory problems with the first line, just do the same:
start /D C:\program1\bin cmd /k call svc1.cmd

Running File from Notepad Plus Plus and Current Directory

There are a number of examples on the web of how to run a file from the Notepad Plus Plus (NPP). But they all fail to account for the fact that the current working directory is the location of the NPP's executable, and not the location of the file.
Usually they go something like this:
cmd /K "$(FULL_CURRENT_PATH)"
Consider the following Python script:
with open('somefile.txt', 'a') as file:
file.write('Hello there.\n')
This file will be created in the NPP folder, which is not at all what most people would expect. Most people would want it in the same location as the Python file.
You could also do something like this, and it works as expected, but this limits you to Python files only:
<Command name="Run This Python File" Ctrl="no" Alt="no" Shift="yes" Key="116">cmd /K python "$(FULL_CURRENT_PATH)"</Command>
I would not want to add extra code to the Python script to change the current working directory, as normally this would not be needed.
I have been trying to solve this and came up with the following. This line goes in "shortcuts.xml" in the NPP folder.
<Command name="Run This File" Ctrl="yes" Alt="no" Shift="no" Key="116">cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""</Command>
So you shut down the NPP, edit the "shortcuts.xml" by adding this line, using another editor, then launch the NPP.
To run the file, use Ctrl+F5 key combination.
This works in Windows 10, but fails in Windows XP.
How can I tweak it to work in Windows XP?
Try this:
cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
My guess would be that the problem is the improperly nested quotes in the command; I'm not sure exactly why it would work on later Windows' while failing on XP.
The command
cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""
represents
cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""
Even from the syntax highlighting you can see that the quotes are not quoting what you expect.
To get the desired effect, you can use this command:
cmd /K cd "$(CURRENT_DIRECTORY)" ^&^& "$(FULL_CURRENT_PATH)"
:: XML-ified:
cmd /K cd "$(CURRENT_DIRECTORY)" ^&^& "$(FULL_CURRENT_PATH)"
I run several Windows .bat files from Notepad++. To achieve this one of the entries in the <UserDefinedCommands> section of the file C:\Users\AdrianHHH\AppData\Roaming\Notepad++\shortcuts.xml is:
<Command name="CD and run file" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /C "cd /d $(CURRENT_DIRECTORY) && $(FULL_CURRENT_PATH)"</Command>
With the .bat as the current file I then use menu => Run => CD and run file.
The command line shown in the question appears to have too many " symbols. The current directory includes the drive specifier and so the CD needs the \D option.
The command I use starts cmd \C ... (rather than the \K in the question) so the command window closes automatically. My .bat files normally finish with choice /t 60 /C Y /d Y /n so I can see the command's output.
Notepad++ > F5 (Run) > then type following command
cmd /K cd "$(CURRENT_DIRECTORY)" && python "$(FULL_CURRENT_PATH)"
assuming you have setup the path, or you may use C:\Python27\python.exe or the path of your python binary, and you will run the python at the folder where the python resides in. You can also save this command to shortcut by clicking button Save....
Afterwards, you also can modify the command in toolbar > Run > Modify shortcut/delete command.

Run alias command from command prompt in new command prompt

So I've set up a file with some aliases for commands that I commonly use. I added it to the registry like in this answer.
I want to use this alias like so:
>cmd /k newalias
'newalias' is not recognized as an internal or external command,
operable program or batch file.
So this alias cannot be used.
If I type >cmd /k newalias again, now it works, so the problem seems to be that the command is being run before the doskey commands in the alias file are executed.
Is there any way to wait until after these aliases are created before running the command?
strange behaviour, but if you use doskey after you import your macro that is working :
cmd /K "doskey /macrofile=c:\temp\macros.txt & doskey /macros >null & newalias"
edit the above commant doesnt work, newalias has to be written manually in the console.

Activating a Python virtual environment & changing directory in one shortcut (with cmd)

I'm trying to do the following:
Open a CMD prompt
Activate a virtual environment
Change the current directory to my project folder
In essence, I need to execute the following commands sequentially:
C:\Envs\djangorocks\Scripts\activate
cd "D:\GitHub\steelrumors"
I've found this link, but creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
After quite a while of searching I'm still doing it manually, any help is appreciated.
"creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):"
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
Observations:
cmd \k should be cmd /k.
& should be && when using a shortcut.
You dont need all the " characters.
Try the following as the shortcut target:
cmd /k C:\Envs\djangorocks\Scripts\activate && cd D:\GitHub\steelrumors
Consider creating a batch file (e.g. c:\scripts\launchEnv.cmd) that does something like the following:
#echo off
C:\Envs\djangorocks\Scripts\activate
cd /d "D:\GitHub\steelrumors"
Then create a shortcut that invokes cmd /k c:\scripts\launchEnv.cmd .
Some notes:
the #echo off will prevent the commands from showing up in the cmd windows. If you do want to see the commands, then omit that line from your batch file
you'll need the /d param when changing directories to make sure you actually change and navigate there, independent of where the script is currently executing from.
As an extension to the great answer from #DavidPostill I've added an additional step to run a command from the newly created python env.
In my example below, I'm launching a new instance of the awesome data mining program, orange, from an anaconda env called orange. I've also cd'ed to the directory containing my orange data files. Note that I had to use the quotation marks "" to make it work.
C:\Windows\System32\cmd.exe /k "F: && cd \Dropbox\IT\Python\Orange && C:\Users\dreme\Anaconda3\Scripts\activate.bat orange && python -m Orange.canvas"

Resources