Scripting in Windows Batch - windows

I have a pretty simple batch file that is basically checking to see if an enviornment variable is set, and then attempting to set it. Its not setting the variable correctly, and I am pretty new to batch files in general so I had some general questions about the script I am writing. Any insight would be appreciated. Here are the questions I have.
In line 15, what if I dont know the full name of the directory? i.e. what if I just knew 'whistle' was a part of it, and wanted to check if it was == to DIR?
In lines 16 and 19, I am trying to say if I am in 'whistle-1.0.0', I know that the BELFRAMEWORK_HOME variable should be set to 1 directory above it. I try to do this with '%DIR%..\' but this seems to have no effect. How can I set BELFRAMEWORK_HOME to 1 directory above 'whistle-1.0.0'?
In lines 17 and 20, the BELFRAMEWORK_HOME variable is not printed. I do not understand why, because it is of the same form as lines 10 and 12, which echo the variable as expected. Why doesnt BELFRAMEWORK_HOME echo?
Last thing, are there any good online resources for learning more about this type of scripting?
Thanks!
1#echo off
2
3if not defined JAVA_OPTS (
4 set JAVA_OPTS=-Xmx1024m -Dderby.stream.error.field=org.openbel.framework.common.enums.DatabaseType.NULL_OUTPUT_STREAM
5) else (
6 set JAVA_OPTS=%JAVA_OPTS% -Dderby.stream.error.field=org.openbel.framework.common.enums.DatabaseType.NULL_OUTPUT_STREAM
7)
8
9set DIR=%CD%
10echo "DIR/CD : %DIR%"
11set DIR=%~dp0
12echo "DIR/~dp0 : %DIR%"
13
14if not defined BELFRAMEWORK_HOME (
15 if "%DIR%"=="whistle-1.0.0" (
16 set BELFRAMEWORK_HOME=%DIR%\..\
17 echo "DIR == whistle, BELFRAMEWORK_HOME is now: %BELFRAMEWORK_HOME%"
18 ) else (
19 set BELFRAMEWORK_HOME=%DIR%\..\
20 echo "DIR != whistle, BELFRAMEWORK_HOME is now: %BELFRAMEWORK_HOME%"
21 )
22)
23
24set WHISTLE_HOME=%~dp0
25java %JAVA_OPTS% -jar %WHISTLE_HOME%\whistle-1.0.0.jar %*

Oooh, 4 questions for the price of one :-)
1) I believe you want something like C:\folder\xxxxWHISTLExxxx\ to match, but not something like C:\xxxxWHISTLExxxx\folder\. If that is the case then you will want to use FINDSTR with a regular expression to make sure the last directory is what matches WHISTLE. The search must be case insensitive.
You don't want the FINDSTR output. You just want to know if the string is found. It returns 1 if not found (error) and 0 if found (success).
echo %DIR% | findstr /irc:"whistle[^\\]*[\\]*$" >nul && (
rem Code for success (string found)
)
rem Code for failure (string not found)
)
2) If you want the trailing backslash, then you can use
for %%A in ("%DIR%\.") do set "BELFRAMEWORK_HOME=%%~dpA"
If you don't want the trailing backslash, then use
for %%A in ("%DIR%\..") do set "BELFRAMEWORK_HOME=%%~fA"
3) You are experiencing a classic stumbling block for new batch users. The %BELFRAMEWORK_HOME% is expanded when the line is parsed, and the entire IF statement, including the parenthesized code blocks, is parsed as one statement. So the expanded value is the value that existed before the IF statement is executed.
You can solve the problem by using delayed expansion.
#echo off
setlocal enableDelayedExpansion
set var=BEFORE
(
set var=AFTER
echo within block normal expansion = %var%
echo within block delayed expansion = !var!
)
echo after block normal expansion = %var%
The result of the above is
within block normal expansion = BEFORE
within block delayed expansion = AFTER
after block normal expansion = AFTER
4) Here are my favorite batch scripting web sites
http://dostips.com
http://judago.webs.com
http://robvanderwoude.com/batchfiles.php
http://ss64.com/nt/syntax.html or http://ss64.com/nt/

Related

Win10 Batch script: \Notepad++ was unexpected at this time

This batch file gives the error in the title:
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
echo FOOBAR
) else (
set HOME_EDIT=%SystemDrive%\Program Files (x86)\Notepad++
)
This is on a Win10 Pro x64 system (so the test is false).
Strangely the "set HOME_EDIT..." line, if executed by itself, does NOT generate the error. And this batch file seemed to work OK a few weeks ago (oldest complaint in the book, I know, but maybe the recent Win10 Creator's Update chagned something?).
Here's the whole output:
U:\Users\Dave\data\PC setup\2017-03 PC Setup for Win10>test.bat
\Notepad++ was unexpected at this time.
U:\Users\Dave\data\PC setup\2017-03 PC Setup for Win10> set HOME_EDIT=C:\Program Files (x86)\Notepad++
U:\Users\Dave\data\PC setup\2017-03 PC Setup for Win10>
Putting quotes around the assignment certainly solves the problem, but it has nothing to do with spaces. The problem is the ) in the path is closing the ELSE block prematurely unless the path is quoted (or escaped).
Without quotes, the ELSE block becomes
) else (
set HOME_EDIT=%SystemDrive%\Program Files (x86
)
And then the \Notepad++ is indeed unexpected, causing a syntax error.
I see 3 ways to eliminate the syntax error:
1) Eliminate the parentheses and put the SET command on the same line as ELSE
else set HOME_EDIT=%SystemDrive%\Program Files (x86)\Notepad++
2) Put quotes around the assignment
) else (
set "HOME_EDIT=%SystemDrive%\Program Files (x86)\Notepad++"
)
3) Escape the closing parenthesis
) else (
set HOME_EDIT=%SystemDrive%\Program Files (x86^)\Notepad++
)
If I were to do the assignment, I would use the predefined environment variable for the folder.
) else (
set "HOME_EDIT=%ProgramFiles(x86)%\Notepad++"
)
Answering my own question:
#drescherjm was correct (in the comments) that the immediate problem was lack of quote marks ("foo") around the argument to SET. Somehow this matters when the SET is within a IF statement, even tho it doesn't otherwise.
But adding quotes just broke something else later in my batch script:
set NEW_PATH=%HOME_WINDOWS%
set NEW_PATH=%NEW_PATH%;%BinPath%
set NEW_PATH=%NEW_PATH%;%BinPath%\ffmpeg\bin
set NEW_PATH=%NEW_PATH%;%BinPath%\mplayer
set NEW_PATH=%NEW_PATH%;%BinPath%\gui
set NEW_PATH=%NEW_PATH%;%BinPath%\dll
set NEW_PATH=%NEW_PATH%;%HOME_CYGWIN%\bin
set NEW_PATH=%NEW_PATH%;%HOME_CYGWIN%\sbin
set NEW_PATH=%NEW_PATH%;%HOME_CYGWIN%\usr\bin
set NEW_PATH=%NEW_PATH%;%HOME_CYGWIN%\usr\sbin
set NEW_PATH=%NEW_PATH%;%HOME_7ZIP%
set NEW_PATH=%NEW_PATH%;%HOME_EDIT%
set NEW_PATH=%NEW_PATH%;%HOME_DIFF%
set Path=%NEW_PATH%
setx Path "%NEW_PATH%"
If HOME_EDIT has quote marks in it, this causes SETX to fail.
Here's the fix I came up with (note lines marked with "TRICK1"):
REM The following is a trick to get around spaces in the path (TRICK1)
set HOME_EDIT=%SystemDrive%\%ProgramFiles(x86)%\Notepad++
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
set HOME_CYGWIN=%SystemDrive%\cygwin
set HOME_EDIT=%SystemDrive%\Program Files\Notepad++
) else (
set HOME_CYGWIN=%SystemDrive%\cygwin64
REM (TRICK1 rem this out) set HOME_EDIT=%SystemDrive%\%ProgramFiles(x86)%\Notepad++
)
The path with the spaces in it is SET as a default value prior to entering the IF statement. This way no quote marks are needed, and SETX doesn't break later.

Word Sorting in Batch

Right let me rewrite this try to make it more clear.
Picture added to make this even clearer:
I have two files
File 1, contains words.
file 2, contains commands.
I need to put words from FILE 1
into FILE 2
I cannot copy-paste them one by one, because there is a LOT of words in FILE 1
File 1 is listed in alphabetical order (by first letter)
File 2 the command does not change
The issue is getting words from file 1 into file 2
but they have to be moved into quotes " " in file 2
so a script that could for example..
Take apple from file 1 and move it between quotes admin.executemotecommand "apple"inside file 2 as it goes down the list keeping the words in order as they move them across.
This could perhaps be done the same way around in which, the script writes the command in front of the words in file 1 as it goes down file 1's list
Is this even possible? I've never seen this done anywhere else and completely clueless if batch is even the right language for it.
The question is a little confusing, but based on your responses in the comments my understanding is that you don't necessarily need the script to edit a preexisting file 2, because you're repeating the same command(s) for each word, so the script can just create a new file based on the words in file 1.
You can do it at the prompt like this:
FOR /F %a IN (words.txt) DO ECHO admin.executeremotecommand "%a" >> commands.txt
The original version of the question indicated that you want more than one command for each word. I take it you changed that in order to simplify that question, and figured you'd just run the script once for each command? However, it's quite simple to have it produce more than one command for each word:
FOR /F %a IN (words.txt) DO (ECHO first.command "%a" & ECHO second.command "%a") >> commands.txt
In a batch file, you'd do it this way:
#ECHO OFF
FOR /F %%a IN (words.txt) DO (
ECHO first.command "%%a"
ECHO second.command "%%a"
) >> commands.txt
BTW, in the code in some of your comments, you surrounded the variable with %'s (%A%). That's incorrect; it would evaluate to the value of %A followed by a literal %. Surrounding with %'s is used only for environment variables. (Note that the %'s around environment variables do not get doubled in a batch file. For example, to get the current date, use ECHO %date% both at the prompt and in a batch file.)

Which comment style should I use in batch files?

I've been writing some batch files, and I ran into this user guide, which has been quite informative. One thing it showed me was that lines can be commented not just with REM, but also with ::. It says:
Comments in batch code can be made by using a double-colon, this is better than using the REM command because labels are processed before redirection symbols. ::<remark> causes no problems but rem <remark> produces errors.
Why then, do most guides and examples I see use the REM command? Does :: work on all versions of Windows?
tl;dr: REM is the documented and supported way to embed comments in batch files.
:: is essentially a blank label that can never be jumped to, whereas REM is an actual command that just does nothing. In neither case (at least on Windows 7) does the presence of redirection operators cause a problem.
However, :: is known to misbehave in blocks under certain circumstances, being parsed not as a label but as some sort of drive letter. I'm a little fuzzy on where exactly but that alone is enough to make me use REM exclusively. It's the documented and supported way to embed comments in batch files whereas :: is merely an artifact of a particular implementation.
Here is an example where :: produces a problem in a FOR loop.
This example will not work in a file called test.bat on your desktop:
#echo off
for /F "delims=" %%A in ('type C:\Users\%username%\Desktop\test.bat') do (
::echo hello>C:\Users\%username%\Desktop\text.txt
)
pause
While this example will work as a comment correctly:
#echo off
for /F "delims=" %%A in ('type C:\Users\%username%\Desktop\test.bat') do (
REM echo hello>C:\Users\%username%\Desktop\text.txt
)
pause
The problem appears to be when trying to redirect output into a file. My best guess is that it is interpreting :: as an escaped label called :echo.
Comments with REM
A REM can remark a complete line, also a multiline caret at the line end, if it's not the end of the first token.
REM This is a comment, the caret is ignored^
echo This line is printed
REM This_is_a_comment_the_caret_appends_the_next_line^
echo This line is part of the remark
REM followed by some characters .:\/= works a bit different, it doesn't comment an ampersand, so you can use it as inline comment.
echo First & REM. This is a comment & echo second
But to avoid problems with existing files like REM, REM.bat or REM;.bat only a modified variant should be used.
REM^;<space>Comment
And for the character ; is also allowed one of ;,:\/=
REM is about 6 times slower than :: (tested on Win7SP1 with 100000 comment lines).
For a normal usage it's not important (58µs versus 360µs per comment line)
Comments with ::
A :: always executes a line end caret.
:: This is also a comment^
echo This line is also a comment
Labels and also the comment label :: have a special logic in parenthesis blocks.
They span always two lines SO: goto command not working.
So they are not recommended for parenthesis blocks, as they are often the cause for syntax errors.
With ECHO ON a REM line is shown, but not a line commented with ::
Both can't really comment out the rest of the line, so a simple %~ will cause a syntax error.
REM This comment will result in an error %~ ...
But REM is able to stop the batch parser at an early phase, even before the special character phase is done.
#echo ON
REM This caret ^ is visible
You can use &REM or &:: to add a comment to the end of command line.
This approach works because '&' introduces a new command on the same line.
Comments with percent signs %= comment =%
There exists a comment style with percent signs.
In reality these are variables but they are expanded to nothing.
But the advantage is that they can be placed in the same line, even without &.
The equal sign ensures, that such a variable can't exists.
echo Mytest
set "var=3" %= This is a comment in the same line=%
The percent style is recommended for batch macros, as it doesn't change the runtime behaviour, as the comment will be removed when the macro is defined.
set $test=(%\n%
%=Start of code=% ^
echo myMacro%\n%
)
Performance REM vs :: vs %= =%
In short:
:: and %= =% seems to have the same performance
REM takes ~ 50% more time than ::
In blocks, especially loops only REM consumes time, but :: is removed from the cached block when the block is parsed, therefore it consumes no time
For more info see SO: Question about Comments in Batch *.bat files and speed
This answer attempts a pragmatic summary of the many great answers on this page:
jeb's great answer deserves special mention, because it really goes in-depth and covers many edge cases.
Notably, he points out that a misconstructed variable/parameter reference such as %~ can break any of the solutions below - including REM lines.
Whole-line comments - the only directly supported style:
REM (or case variations thereof) is the only official comment construct, and is the safest choice - see Joey's helpful answer.
:: is a (widely used) hack, which has pros and cons:
Pros:
Visual distinctiveness and, possibly, ease of typing.
Speed, although that will probably rarely matter - see jeb's great answer and Rob van der Woude's excellent blog post.
Cons:
Inside (...) blocks, :: can break the command, and the rules for safe use are restrictive and not easy to remember - see below.
If you do want to use ::, you have these choices:
Either: To be safe, make an exception inside (...) blocks and use REM there, or do not place comments inside (...) altogether.
Or: Memorize the painfully restrictive rules for safe use of :: inside (...), which are summarized in the following snippet:
#echo off
for %%i in ("dummy loop") do (
:: This works: ONE comment line only, followed by a DIFFERENT, NONBLANK line.
date /t
REM If you followed a :: line directly with another one, the *2nd* one
REM would generate a spurious "The system cannot find the drive specified."
REM error message and potentially execute commands inside the comment.
REM In the following - commented-out - example, file "out.txt" would be
REM created (as an empty file), and the ECHO command would execute.
REM :: 1st line
REM :: 2nd line > out.txt & echo HERE
REM NOTE: If :: were used in the 2 cases explained below, the FOR statement
REM would *break altogether*, reporting:
REM 1st case: "The syntax of the command is incorrect."
REM 2nd case: ") was unexpected at this time."
REM Because the next line is *blank*, :: would NOT work here.
REM Because this is the *last line* in the block, :: would NOT work here.
)
Emulation of other comment styles - inline and multi-line:
Note that none of these styles are directly supported by the batch language, but can be emulated.
Inline comments:
* The code snippets below use ver as a stand-in for an arbitrary command, so as to facilitate experimentation.
* To make SET commands work correctly with inline comments, double-quote the name=value part; e.g., SET "foo=bar".[1]
In this context we can distinguish two subtypes:
EOL comments ([to-the-]end-of-line), which can be placed after a command, and invariably extend to the end of the line (again, courtesy of jeb's answer):
ver & REM <comment> takes advantage of the fact that REM is a valid command and & can be used to place an additional command after an existing one.
ver & :: <comment> works too, but is really only usable outside of (...) blocks, because its safe use there is even more limited than using :: standalone.
Intra-line comments, which be placed between multiple commands on a line or ideally even inside of a given command.
Intra-line comments are the most flexible (single-line) form and can by definition also be used as EOL comments.
ver & REM^. ^<comment^> & ver allows inserting a comment between commands (again, courtesy of jeb's answer), but note how < and > needed to be ^-escaped, because the following chars. cannot be used as-is: < > | (whereas unescaped & or && or || start the next command).
%= <comment> =%, as detailed in dbenham's great answer, is the most flexible form, because it can be placed inside a command (among the arguments).
It takes advantage of variable-expansion syntax in a way that ensures that the expression always expands to the empty string - as long as the comment text contains neither % nor :
Like REM, %= <comment> =% works well both outside and inside (...) blocks, but it is more visually distinctive; the only down-sides are that it is harder to type, easier to get wrong syntactically, and not widely known, which can hinder understanding of source code that uses the technique.
Multi-line (whole-line block) comments:
James K's answer shows how to use a goto statement and a label to delimit a multi-line comment of arbitrary length and content (which in his case he uses to store usage information).
Zee's answer shows how to use a "null label" to create a multi-line comment, although care must be taken to terminate all interior lines with ^.
Rob van der Woude's blog post mentions another somewhat obscure option that allows you to end a file with an arbitrary number of comment lines: An opening ( only causes everything that comes after to be ignored, as long as it doesn't contain a ( non-^-escaped) ), i.e., as long as the block is not closed.
[1] Using SET "foo=bar" to define variables - i.e., putting double quotes around the name and = and the value combined - is necessary in commands such as SET "foo=bar" & REM Set foo to bar., so as to ensure that what follows the intended variable value (up to the next command, in this case a single space) doesn't accidentally become part of it.
(As an aside: SET foo="bar" would not only not avoid the problem, it would make the double quotes part of the value).
Note that this problem is inherent to SET and even applies to accidental trailing whitespace following the value, so it is advisable to always use the SET "foo=bar" approach.
Another alternative is to express the comment as a variable expansion that always expands to nothing.
Variable names cannot contain =, except for undocumented dynamic variables like
%=ExitCode% and %=C:%. No variable name can ever contain an = after the 1st position. So I sometimes use the following to include comments within a parenthesized block:
::This comment hack is not always safe within parentheses.
(
%= This comment hack is always safe, even within parentheses =%
)
It is also a good method for incorporating in-line comments
dir junk >nul 2>&1 && %= If found =% echo found || %= else =% echo not found
The leading = is not necessary, but I like if for the symmetry.
There are two restrictions:
1) the comment cannot contain %
2) the comment cannot contain :
After I realized that I could use label :: to make comments and comment out code REM just looked plain ugly to me. As has been mentioned the double-colon can cause problems when used inside () blocked code, but I've discovered a work-around by alternating between the labels :: and :space
:: This, of course, does
:: not cause errors.
(
:: But
: neither
:: does
: this.
)
It's not ugly like REM, and actually adds a little style to your code.
So outside of code blocks I use :: and inside them I alternate between :: and :.
By the way, for large hunks of comments, like in the header of your batch file, you can avoid special commands and characters completely by simply gotoing over your comments. This let's you use any method or style of markup you want, despite that fact that if CMD ever actually tried to processes those lines it'd throw a hissy.
#echo off
goto :TopOfCode
=======================================================================
COOLCODE.BAT
Useage:
COOLCODE [/?] | [ [/a][/c:[##][a][b][c]] INPUTFILE OUTPUTFILE ]
Switches:
/? - This menu
/a - Some option
/c:## - Where ## is which line number to begin the processing at.
:a - Some optional method of processing
:b - A third option for processing
:c - A forth option
INPUTFILE - The file to process.
OUTPUTFILE - Store results here.
Notes:
Bla bla bla.
:TopOfCode
CODE
.
.
.
Use what ever notation you wish *'s, #'s etc.
This page tell that using "::" will be faster under certain constraints
Just a thing to consider when choosing
good question... I've been looking for this functionality for long too...
after several tests and tricks it seem the better solution is the more obvious one...
--> best way I found to do it, preventing parser integrity fail, is reusing REM:
echo this will show until the next REM &REM this will not show
you can also use multiline with the "NULL LABEL" trick...
(dont forget the ^ at the end of the line for continuity)
::(^
this is a multiline^
comment... inside a null label!^
dont forget the ^caret at the end-of-line^
to assure continuity of text^
)
James K, I'm sorry I was wrong in a fair portion of what I said. The test I did was the following:
#ECHO OFF
(
:: But
: neither
:: does
: this
:: also.
)
This meets your description of alternating but fails with a ") was unexpected at this time." error message.
I did some farther testing today and found that alternating isn't the key but it appears the key is having an even number of lines, not having any two lines in a row starting with double colons (::) and not ending in double colons. Consider the following:
#ECHO OFF
(
: But
: neither
: does
: this
: cause
: problems.
)
This works!
But also consider this:
#ECHO OFF
(
: Test1
: Test2
: Test3
: Test4
: Test5
ECHO.
)
The rule of having an even number of comments doesn't seems to apply when ending in a command.
Unfortunately this is just squirrelly enough that I'm not sure I want to use it.
Really, the best solution, and the safest that I can think of, is if a program like Notepad++ would read REM as double colons and then would write double colons back as REM statements when the file is saved. But I'm not aware of such a program and I'm not aware of any plugins for Notepad++ that does that either.
A very detailed and analytic discussion on the topic is available on THIS page
It has the example codes and the pros/cons of different options.
There are a number of ways to comment in a batch file
1)Using rem
This is the official way. It apparently takes longer to execute than ::, although it apparently stops parsing early, before the carets are processed. Percent expansion happens before rem and :: are identified, so incorrect percent usage i.e. %~ will cause errors if percents are present. Safe to use anywhere in code blocks.
2)Using labels :, :: or :; etc.
For :: comment, ': comment' is an invalid label name because it begins with an invalid character. It is okay to use a colon in the middle of a label though. If a space begins at the start of label, it is removed : label becomes :label. If a space or a colon appears in the middle of the label, the rest of the name is not interpreted meaning that if there are two labels :f:oo and :f rr, both will be interpreted as :f and only the later defined label in the file will be jumped to. The rest of the label is effectively a comment. There are multiple alternatives to ::, listed here. You can never goto or call a ::foo label. goto :foo and goto ::foo will not work.
They work fine outside of code blocks but after a label in a code block, invalid or not, there has to be a valid command line. :: comment is indeed another valid command. It interprets it as a command and not a label; the command has precedence. Which is the command to cd to the :: volume, which will work if you have executed subst :: C:\, otherwise you get a cannot find the volume error. That's why :; is arguably better because it cannot be interpreted in this way, and therefore is interpreted as a label instead, which serves as the valid command. This is not recursive, i.e, the next label does not need a command after it. That's why they come in twos.
You need to provide a valid command after the label e.g. echo something. A label in a code block has to come with at least one valid command, so the lines come in pairs of two. You will get an unexpected ) error if there is a space or a closing parenthesis on the next line. If there is a space between the two :: lines you will get an invalid syntax error.
You can also use the caret operator in the :: comment like so:
#echo off
echo hello
(
:;(^
this^
is^
a^
comment^
)
:;
)
:;^
this^
is^
a^
comment
:;
)
But you need the trailing :; for the reason stated above.
#echo off
(
echo hello
:;
:; comment
:; comment
:;
)
echo hello
It is fine as long as there is an even number. This is undoubtedly the best way to comment -- with 4 lines and :;. With :; you don't get any errors that need to be suppressed using 2> nul or subst :: C:\. You could use subst :: C:\ to make the volume not found error go away but it means you will have to also put C: in the code to prevent your working directory from becoming ::\.
To comment at the end of a line you can do
command &:: or command & rem comment, but there still has to be an even number, like so:
#echo off
(
echo hello & :;yes
echo hello & :;yes
:;
)
echo hello
The first echo hello & :;yes has a valid command on the next line but the second & :;yes does not, so it needs one i.e. the :;.
3)Using an invalid environment variable
%= comment =%. In a batch file, environment variables that are not defined are removed from the script. This makes it possible to use them at the end of a line without using &. It is custom to use an invalid environment variable i.e. one that contains an equals sign. The extra equals is not required but makes it look symmetrical. Also, variable names starting with "=" are reserved for undocumented dynamic variables. Those dynamic variables never end with "=", so by using an "=" at both the start and end of the comment, there is no possibility of a name clash. The comment cannot contain % or :.
#echo off
echo This is an example of an %= Inline Comment =% in the middle of a line.
4)As a command, redirecting stderr to nul
#echo off
(
echo hello
;this is a comment 2> nul
;this is another comment 2> nul
)
5)At the end of a file, everything after an unclosed parenthesis is a comment
#echo off
(
echo hello
)
(this is a comment
this is a comment
this is a comment

How to safely echo value of a variable inside if-clause skipping surrounding double quotes?

Running
#echo off
setlocal enabledelayedexpansion
set x=some value with unsafe (^&^<^>()") characters inside
if 1 == 1 (
echo "value of x (!x!) is interesting"
)
gives
"value of x (some value with unsafe (&<>()") characters inside) is interesting"
I had to put the value being echoed inside double quotes to avoid parsing error. I don't want these double quotes to be printed, however. Is there any way to temporarily (only to safely pass it to echo command) quote value being printed?
The answer is the same as in your question
How to escape variables with parentheses inside if-clause in a batch file?
#echo off
setlocal enabledelayedexpansion
set x=some value with unsafe (^&^<^>()") characters inside
if 1 == 1 (
set "output=value of x (!x!) is interesting"
echo !output!
)
you can add quotes to the variable and try a string substitution the moment you echo it.
with e.g.:
echo %x:"=%
but in your case i dont think that will work.
to make your example work just escape the parenthesis:
echo value of x ^(!x!^) is interesting
unfortunately there is no 100% answer for this.
let us know if it worked for you or you found a better solution
Excuse me, I think there is a little confusion here.
If you want to print any special character with an echo command just escape it. Period. Your problem have no relation with Delayed Expansion. For example, this code:
#echo off
if 1 == 1 (
echo value of x (anything) is interesting
)
mark an error because the parentheses in echo command interfere with the opened if. To solve this, just escape the parentheses as weberik said:
#echo off
if 1 == 1 (
echo value of x ^(anything^) is interesting
)
A Delayed variable Expansion never cause an error don't matter the value inside the variable.

Using an OR in an IF statement WinXP Batch Script [duplicate]

This question already has answers here:
IF... OR IF... in a windows batch file
(18 answers)
Closed 3 years ago.
Is there a way to pass an OR through an IF statement?
Such as:
SET var=two
IF "%var%"=="one" OR "two" OR "three" ECHO The number is between zero and four.
No.
if "%var%"=="one" goto foo
if "%var%"=="two" goto foo
if "%var%"=="three" goto foo
goto afterfoo
:foo
echo The number is between one and three (technically incorrect, since it includes the end points and thus is not between).
:afterfoo
If you need a more structured approach:
if "%var%"=="one" set OneToThree=1
if "%var%"=="two" set OneToThree=1
if "%var%"=="three" set OneToThree=1
if defined OneToThree (
echo Foo
) else (
rem something
)
See duplicate question IF... OR IF... in a windows batch file where following solution proposed by #Apostolos
FOR %%a in (item1 item2 ...) DO IF {condition_involving_%%a} {execute_command}
e.g.
FOR %%a in (1 2) DO IF %var%==%%a ECHO TRUE
I found to be the most straight forward and simple to use case in my batch scripts.
A bit late in the game, but nevertheless assuming if this might help anyone stumbling upon the question. The way I do this is using a combination of echo piped to findstr, this way:
(echo ":one: :two: :three:" | findstr /i ":%var%:" 1>nul 2>nul) && (
echo The number is between zero and four
)
Since findstr is an external command, I recommend not using this inside a loop which may go through 1000's of iterations. If that is not the case, this should solve what you are attempting to do instead of using multiple ifs. Also, there is nothing special in the choice of ":", just use a delimiter which is unlikely to be part of the value in the var variable.
Thanks to the rest of folks on pointing to another link which appears to have similar question, I will post this response there as well, just in case someone stumbles upon that question and doesn't quite reach here.

Resources