pwd alias on Windows cmd - windows

I want to create a fixed pwd alias like in this topic, but I want to keep all aliases in one file as in this answer by Argyll. Currently my file cmdAliases.cmd looks like this:
#echo off
doskey ls=dir
doskey pwd=echo ^%cd^%
Running pwd command now prints:
ECHO is on.
I believe the spaces in the command are the problem. Is there a way to fix it using only the cmdAliases.cmd file?

The links you provided show how to do it.
I have my doskey macros in a file aliases.txt in my folder %USERPROFILE%
11:24:16 C:\Users\LotPings________________________________________
> type Aliases.txt
~=CD /D "C:\Users\LotPings"
\=CD \
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL
And an autorun entry which loads this file
> reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v autorun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
autorun REG_SZ Doskey /MacroFile="C:\Users\LotPings\Aliases.txt"
To generate this Autorun automatically copy the following lines into cmd line or a batch file
Set "Key=HKEY_CURRENT_USER\Software\Microsoft\Command Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile=\"%USERPROFILE%\Aliases.txt\""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f

Related

Access Shell32.dll string table from a command line

I'm a making a simple Win32 batch to perform some tasks. One of them is to add "open prompt here" in every directory. Reading around I learned that you can use Shell32.dll string to table to use its string with regedit, for example this is my batch test.
echo Adding <need string here> to folders
REG ADD HKCR\Directory\Shell\Open.MSDOS /ve /f /d "#%SystemRoot%\system32\shell32.dll,-22022"
REG ADD HKCR\Directory\Shell\Open.MSDOS\command /ve /f /d "cmd.exe /K \"pushd \"%%1\"\""
my question is: How can I extract the string for shell32.dll so the user can see the output like this (Spanish):
Adding Símbolo del sistema to folders

REG add Doesn't Run

I've written a batch file that tests for a user's Microsoft Office version, copy an Excel Add-In to their device from a shared drive, and add a registry key to their device.
Each individual action in the batch works as designed. But when I combine them all together, the file doesn't like the REG add command and completely closes the command window (even if I put Pause after the REG add line).
To troubleshoot, I created a new batch file and added pieces of my code to it, one section at a time, and tested the file each time a new section was added. Every section ran fine until I got to this section:
CHDIR "C:\Windows\System32"
IF NOT %ALREADY_ENABLED%=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
)
To see if the problem was my IF statement, I commented out the REG add line and put ECHO Hello World inside the IF statement. The file ran just fine and gave the Hello World output as expected.
I know the REG add command works because I have a batch file that only includes that piece of code and it works just fine:
#Echo off
setlocal enableDelayedExpansion ENABLEEXTENSIONS
chdir "C:\Windows\System32"
SET PATH=\"C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam\"
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
I think the problem has something to do with the PATH variable, but I'm dumbfounded as to why this works in one file but not the other. Is it possible that the value of PATH is somehow changing during runtime after it's been set?
I'm not sure how to trap the error to even see what error is being thrown here. Everything I've tried to handle the error with doesn't work and the command window closes. Any ideas on what I'm doing wrong here?
Maybe this code snippet could help:
SET "myPATH=C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam"
IF NOT "%ALREADY_ENABLED%"=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%myPATH%" /f
)
Note:
do not change system environment variable PATH; use another variable name (myPATH);
quoting in set "variablename=variable value";
quoting in if statement;
to trap any error in a batch file, use echo ON while debugging.
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax

Get last directory with * in windows

cd $PACKAGING_EARS_PATH/target/*/*
but in windows cd target/*/* is not working.
I want to get last directory. Is it possible?
See if this floats your boat:
#echo off
for /d /r "%PACKAGING_EARS_PATH%/target" %%a in (*) do set "folder=%%a"
cd "%folder%"
It looks like you are using Unix syntax to expand an environment variable. Windows uses %var%.
The foxidrive solution may not give the correct answer if there are deeper subdirectories.
A proper solution requires two steps.
From the command line:
for /d %A in ("%PACKAGING_EARS_PATH%\target\*") do #for /d %B in ("%A\*") do #cd %B
Using batch:
#echo off
for /d %%A in ("%PACKAGING_EARS_PATH%\target\*") do for /d %%B in ("%%A\*") do cd %%B
Windows cmd has an interactive command line TAB folder (and file) name completion feature that might be useful to you. For example, type cd te and then press the <tab> key and it will automatically expand to the first folder that starts with te. Repeatedly press <tab> and it will cycle through all matching folder names. You can omit leading characters and it will simply cycle through all folder names.
The TAB completion feature may or may not be enabled by default. CMD /F:ON will explicitly launch cmd with name completion enabled.

How to use REG_EXPAND_SZ from the commandline?

I was reading the Windows Commandline Documentation (Win+F1) about the commands that modify the Windows registry, particularly the the "reg add" command.
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "%systemroot%\system32"
Now, I don't know how this was designed to work.
When I invoke the command above, the variable %systemroot% gets expanded to C:\Windows. I've tried the following not to make the variable to expand, but there is no way I could force it not to:
escaping the `%%`'s with an `%, ^, \` - doesn't work even if I use double quotes around
using the single quotes '' around the whole /d string
use `setlocal setdelayedexpansion`? sth like:
# (setlocal enabledelayedexpansion) && (reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "!systemroot!\system32") && (setlocal disabledelayedexpansion)
The variable 'data' (/d) field is either like ^%systemroot^% or like !systemroot! or just expands to C:\windows .
I could probably use the .reg file to accomplish my task, but I simply don't want to do it.
I thought that maybe there is something wrong with the program that I use to display the variable contents (regedit / regedt32 / reg query (commandline)), but after checking this is probably not the case.
Any ideas? I'm mainly interested how the variable value should look like in the regedit window, should it be like :"%systemroot%\system32" or "C:\windows\system32" to be properly expanded by other programs.
Regards.
From a command line, this worked for me:
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot^%\system32
The syntax suggested by aphoria is correct, without quotes as you discovered. But you run into trouble when you need spaces in the value.
However, it is possible to mix-in quotes in the value. It looks weird, but it works:
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d ^%SystemRoot^%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ
You can use quotes where needed, just not around your variables. And not right after a backslash.
Also, beware that the syntax for use in a batch file is not the same as on the command line. If you put that line into a batch file, you need to replace the "^" with another "%":
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d %%SystemRoot%%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ
(cmd.exe always reminds me of Henry Spencer's quote "Those who do not understand Unix are condemned to reinvent it, poorly")
I recognize that this is an old thread, but since there doesn't seem to be an answer and I too was driven crazy by this problem, I'll share what I've come up with. It's convoluted, it's ugly, and it looks like this:
SET VAR=% SET
SET VALUE=%VAR%SystemRoot%VAR%\system32...
REG ADD HKLM... /v Test /t
REG_EXPAND_SZ /d %VALUE%
I end up with Test containing the string %SystemRoot%\system32
Edit: Should have finished testing before posting. While this worked from the command line, it failed in my command scripts. What DID work was SET VALUE=%%SystemRoot%%\system32...
We have a use in an autounattend.xml. I was first following aphoria's answer, then I had to use mivk's enhanced answer. I had it working. Then I made a small change and it stopped working. I consider this insight worth reporting, to save others lengthy frustration.
Building on previous answers, here is how to make this work in autounattend.xml:
<CommandLine>cmd.exe /c reg.exe add HKLM\ourkey /v ourvalue /t REG_EXPAND_SZ /d "our data with spaces"^%USERNAME^%"and more spaces" /f</CommandLine>
What matters is the cmd.exe /c. If you don't use cmd.exe then reg.exe still runs, but it puts literally ^% into the registry, not the desired %.
Maybe the answer by ASTX813 would work, but this seems easier to read.
There's no magic here, you must escape every % in command-line with/become: %%.
In batch file, you again must escape them, become %%%%SystemRoot%%%%. Yes, four ampersands. It's ugly, but it's the way it is, and it's pretty consistent though.
note: Using ^ to escape it as literal character might help/cleanup in one step/degree only (as long as it's not interpreted). The advantage using ^ is that you can write in command-line or batch file with identical syntax: eg. "^%SytemRoot^%", since in both environments, % treated equally as literal.
The answer is very simple. Only quote the spaces and escape the %'s.
In command line, escape the %'s with the ^.
reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun /t REG_SZ /d title" "^%username^% /f
In batch file, escape the %'s with another %.
reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun /t REG_SZ /d title" "%%username%% /f
This took me a while to figure out. In my mind, I should quote the entire string, but could not get the variable to be passed without expanding it. I broke my way of thinking by quoting spaces instead.
Please try the following:
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot%^\system32
From a batch file you end up with problems with quotes, which you can then escape with a backslash, for example:
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%name%" /v "UninstallString" /t REG_EXPAND_SZ /d "\"%%comspec%%\" /d /c \"%dest%\uninstall.cmd\""
without quotes
reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "%%%^%systemroot%%%^%\system32"
with quotes
reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "\"^%%systemroot^%%\system32\""
I finally got sick of the banner/logo on CScript and decided to prefix VBS associations with //nologo in my 'new build' batch script. This required modifying a REG_EXPAND_SZ registry entry with an environment variable and this question and its various answers were the best inspiration I could find. Here's the final script.
SET "thecmd=\""%%SystemRoot%%\System32\CScript.exe\"" //nologo \""%%1\"" %%*"
ECHO.%thecmd%
REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell\Open2\Command" /ve /t REG_EXPAND_SZ /d "%thecmd%" /f
SET thecmd=
Storing the command in a variable and using \"" for " and %% for % are what allows it to work.
If you prefer a one-liner (again, from batch) you can use:
REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell\Open2\Command" /ve /t REG_EXPAND_SZ /d "\""%%SystemRoot%%\System32\CScript.exe\"" //nologo \""%%1\"" %%*" /f
If you want the same functionality, don't forget to set the verb for VBSFile to Open2 so you can just double-click VBS files to run them with CScript:
REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell" /ve /t REG_SZ /d "Open2" /f

How to open file with default application in cmd?

I'm trying to open a file in it's default editor after the user has created the file. So far my script is:
#echo off
#echo --- Create A New File ---
#echo -
#echo Where should we put the new file?
set /p fileLocation=# %UserProfile%\
#echo -
#echo What do you want to call your new file?
set /p fileName=#
#echo -
#echo Almost Done! What is the files extension?
set /p extension=# .
#echo -
copy NUL "%UserProfile%\%fileLocation%\%fileName%.%extension%"
(ignore the extra echos and '#' those are just for fun)
After I click the file, it does the command: Choose Location > Choose File Name > Choose File extension. I'm almost done on what I want but theres one last thing. How can I get the file name that I created and then open in its default text-editor?
You can use start to open the file with the associated application.
Resources :
Open a File in the Default Application using the Windows Command Line (without JDIC) (waybackmachine capture from Oct 30, 2010)
In windows you can use start (http://ss64.com/nt/start.html).
start "" "%UserProfile%\%fileLocation%\%fileName%.%extension%"
You can also use explorer.exe/explorer to open the file (e.g. explorer file.txt). This also works nicely if you use WSL, especially with an alias like alias open="explorer.exe" so you can just call it like, e.g., open file.txt.
I achieved the correct way of FILE ASSOCIATION using these cmd commands.
this is just an example:
REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v # /t REG_SZ /d "\"C:\\Program Files\\Noteepad++\\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f
assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"
(it's better to put them in .bat file)

Resources