Concatenate env variable value with string in command line - windows

This is Windows 7 command prompt question.
Suppose I have environment variable which were set in a next way:
set FILE_SRC="D:\Users\me\Documents and Settings"
I would like to call form command line utility which will get one of directory files as argument:
fooUtil.exe %FILE_SRC%\fileName.txt
In this case shell fails to construct correct path string. Instead of it utility get next argument:
"D:\Users\me\Documents and Settings"\fileName.txt
What is a correct way? Again, I talk about prompt command line and not a batch file.

I make it so ...
set "FILE_SRC=D:\Users\me\Documents and Settings"
fooUtil.exe "%FILE_SRC%\fileName.txt"
This works also with special characters.
set "line=lines & edges = figures"
#echo "%line%"

Just skip the quote marks when setting the variable. The variable will be set to the value terminated by a newline, not space.

Related

Invalid syntax with setx

I used the setx command to set OGRE_HOME:
setx OGRE_HOME D:\Program Files\OgreSDK
Now I need to change to value of OGRE_HOME.
How can I search all the values I have set?
If I run the command again, it shows that:
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Your path to the Ogre SDK has a space character in it, which is interpreted as a delimiter to another argument. Surround your path with " to keep it as one single argument to setx:
setx OGRE_HOME "D:\Program Files\OgreSDK"
To see the current value of the OGRE_HOME environment variable:
echo %OGRE_HOME%
You may have to open a new command prompt shell to see the value if you set it and are then trying to immediately see it's value.
To see all currently set environment variables, simply run:
set
To show only environment variables that have a certain prefix (so FOO would show FOOBAR and FOOBAZ), put that prefix after set:
set PREFIX
Alternatively, you can use the GUI to edit environment variables (assuming Windows 7 here).
Right-click Computer, choose Properties
Click Advanced system settings in the left pane
Make sure you're on the Advanced tab in the pop-up dialog
Click Environment Variables... at the bottom
A dialog will pop up with your user-specific environment variables as well as your system-wide environment variables. Select a value and use the New/Edit/Delete buttons to interact with them.
Command Prompt is giving you that error because you forgot the quotation marks. You should’ve typed:
setx OGRE_HOME "D:\Program Files\OgreSDK"
To see all the values you’ve already set, enter either:
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
OR
reg query HKEY_CURRENT_USER\Environment
setx and pretty much all windows command line commands are sensitive to certain special characters. Among them the space character but there's also the quote which is used to delimit an entry.
As #ajp15243 already said, you can deal with the space by locking off the path{s) between two quotations. But what if you have paths and those path already have quotations because they carry a space? Here's an example:
MY_PATHS="c:\Program Files\path1";"c:\Program Files(x86)\Path2"
In this case, you would have to put escape characters for those inner quotation marks when you use setx or it will get confused and give the error you listed. Eg:
setx -m MY_PATHS "\"c:\Program Files\path1\";\"c:\Program Files(x86)\Path2\""
As an addendum to #ajp15243's answer. If you are doing the same with PowerShell rather than the command prompt or batch file, you'll need to call SETX with a leading escaped double-quote character, as in:
$my_path = "%PROGRAMFILES%\MySQL\MySQL Server 5.7\bin\"
$hkcu_path = (Get-ItemProperty hkcu:\Environment).PATH + ";" + $my_path
SETX PATH "`"$hkcu_path" # note the leading escaped quote
However doing so, may result in adding a trailing double quote in the value of hkcu:\Environment\PATH, so you may need to do this too:
$dirty_path = (get-itemproperty hkcu:\Environment).PATH
$clean_path = $dirty_path -replace '"',''
SETX PATH $clean_path

How is it possible to view windows environment variable without executing arguments within it?

Basically I would like to export my exact PATH variable to a file automatically. It contains things like %ANT_HOME%/bin and I would like to keep it that way. From what I could find, using both set and echo will execute that argument and give me the absolute path. Is there something I'm missing?
To get a copy of your PATH without expansion of environment variables you could save the following as "rawPath.vbs"...
Option Explicit
Dim wsh
Set wsh = CreateObject("Wscript.Shell")
Wscript.Echo wsh.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
...and then issue the following command to pipe the output to a file
cscript -nologo rawPath.vbs > myPath.txt
Do you see %ANT_HOME% when you execute SET from the prompt?
If so,
>filename echo %path%
should export the path as desired.
If the PATH variable does not actually contain the "%" characters, then it's already been resolved. And remember, "%" is actually a legitimate (but annoying) filename character...
You CAN set a "%" character into an environment variable
set var=%%something%%
will set var to %something%
You need to escape the % good sir, example
>echo %path%
C:\windows\system32;C:\windows\system32\wbem
>echo ^%path^%
%path%

using the DOS start command when passed arguments have quotes

I have a question about the DOS start command.
I have already read this topic:
Using the DOS “start” command with parameters passed to the started program
Using the "start" command with parameters passed to the started program
but my question is a little different.
I have this problem: I need to pass paths that need to be quoted.
For example, if path have no quotes this works fine:
start "" app.exe -option c:\myapp\myfile.txt
but if path have double quotes it doesn't works.
I have this line in my BATCH file:
start "" myapp.exe -option %mypath%
and when %mypath% contains double quotes (paths that have spaces or other characters in names) the start command returns very strange results.
Thanks
Sandro
Normally it's not a problem to use parameters there with quotes, but you get problems if your app-path has also quotes.
Then you need to add an extra CALL statement.
start "" app.exe -option c:\myapp\myfile.txt - Works
start "" app.exe -option "c:\myapp\myfile.txt" - Works
start "" "app.exe" -option c:\myapp\myfile.txt - Works
start "" "app.exe" -option "c:\myapp\myfile.txt" - Don't works
start "" CALL "app.exe" -option "c:\myapp\myfile.txt" - Works
This might help, but it is a bit way round about method and slight modification may required to suit your need.
The idea is to:
Dump the environment variable which has quotes to a text file with a predefined name. Like:"set mypath2 > withQt.bat"
Use windows power shell or some third party tool to find and replace quotes in that file.
Create another text file (one time step only) containing string "Set "
Use copy command to append the file mentioned in step2 with the file created in step3 and create a batch file with a predefined name. Like: copy base.bat + withQt.bat withtqt.bat
Run the batch file, which creates another/replaces the environment variable with value without quotes.
Sorry, I couldn't get something more elegant at this time.

Manipulating variables in windows

I know there are a few things you can do directly with a variable, such as cut off ends of the variables via %var:~0,4%, or even do character replacement via %var:/=-%. What are these features called? And does anyone have a link to documentation for them?
It's the old dos string manipulation. See http://www.dostips.com/DtTipsStringManipulation.php
I would say "Environment variable substitution" as described in SET /? (although this omits to mention parameter substitution commands like %~nxN for extracting a file name from a path passed as the Nth command line argument).

Windows Command Line: Non Evaluation of Environment Variable

I would like to provide the raw text referring to an environment variable to a command instead of evaluating the environment variable.
I need this to configure BizTalk from the command line, for example:
BTSTask.exe AddResource -ApplicationName:App1
-Type:System.BizTalk:BizTalkAssembly -Overwrite
-Source:..\Schemas\bin\development\App1.Schemas.dll
-Destination:%BTAD_InstallDir%\App1.Schemas.dll
This command adds a resource to a BizTalk application. I want the destination to be %BTAD_InstallDir%\App1.Schemas.dll, however at present it is evaluating the environment variable (to nothing) and using \App1.Schemas.dll.
Is it possible to escape or disable the evaluation of this environment variable while parsing\executing this command?
I have tried escaping the first and both percentage characters with a carrot (^), however this did not stop the evaluation.
[EDIT] When I execute this at the command prompt it doesn't replace the environment variable, however it does when I run it as a script, any thoughts as to why this is different?
Try echo ^%path^% in a command prompt it prints...
path
instead of expanding the environment variable so I guess the following should work for you as suggested by Mikeage
BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:^%BTAD_InstallDir^%\App1.Schemas.dll
Did you try:
%%BTAD_InstallDir%%
in your script ?
That should prevent the script to interpret the variable, and it would pass %BTAD_InstallDir% to the program.
Try ^% instead of %.
Tried:
C:\PrgCmdLine\Unix\echo.exe "%"JAVA_HOME"%"
Got:
%JAVA_HOME%
[EDIT] Indeed, C:\PrgCmdLine\Unix\echo.exe ^%JAVA_HOME^% works too, and is simpler...
[EDIT 2] For the record: I used UnxUtils' echo to have the behavior of a plain program. Built in echo has a slightly different behavior, at least for quoted % signs.
Not sure if it's the same as my case, but i was troubling to use a batch file to create a script which has %temp% variable inside.
The workaround i found:
set test=%temp;
echo {command} %test%%>>path_to_my_batch_file;
Hope this helps someone:)

Resources