Using a nested IF NOT within a batch file [closed] - if-statement

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
I am writing a batch script, and I need to use nested IF NOT in order to mimic 'AND AND' logic.
When trying to run it, I get an error:
"!ext!" was unexpected at this time.
I am not pasting the whole script since the issue is clearly in the nested IF NOT piece. (It works if I remove that). Here is the relevant part:
if not exist "%dst%!relPath!" (
echo file "%dst%!relPath!" does NOT EXIST and it will be created
rem Check if the file extension is png jpg or mp4
if /I "!ext!" == ".png" (
echo It worked!!
)
if /I "!ext!" == ".jpg" (
echo It worked2!!
)
if /I "!ext!" == ".mp4" (
echo It worked3!!
)
if not /I "!ext!" == ".png" if not /I "!ext!" == ".jpg" if not /I "!ext!" == ".mp4" (
echo It worked4!!
)
)
The first 3 ifs are okay, but the 4th if statement, the nested one, is giving me that error, and I can´t see where the issue could be, (unless IF NOT is not supported as nested.

Related

How to use if else comparing string in a batch file? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I would like to check the string is match or not. I tried this way, but it always return error, syntax error, I don't know which syntax that error.
Error message
The syntax of the command is incorrect.
if TXT EQU TXT(
SET Format=TXT
REM ECHO %Format%
if %Format% EQU TXT(
ECHO Format correct
GOTO END
)
ECHO Format not correct
This works for me:
#echo off
set format=TXT
if "%format%"=="TXT" (
#echo Format correct
goto :end
)
#echo Format not correct
:end

How to compare strings using =~? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I'm trying to create a string that will verify if input time is in the right format. I keep getting the 'else' portion to execute, but never the 'then' portion. I'm not sure where in the string there is a mistake. I execute the script in the shell using ./. I test it with 01:20. It will give me "Time entered is valid." when I input single digit int values. I want it to recognize the 00:00 format. Any suggestions?
echo "enter time" ; read time
if [[ '^(([01][0-3])|([2][0-9]))[:][0-5][0-9]$' =~ $time ]]
then
echo "Time entered is valid."
else
echo "Time entered is NOT correct."
fi
=~ is not commutative; the string you want to match must go on the left, the regular expression on the right.
if [[ $time =~ '^(([01][0-3])|([2][0-9]))[:][0-5][0-9]$' ]]

ERRORLEVEL in if statement not works correctly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
In this batch file ERRORLEVEL is displayed correctly (First option returns 1 and the second returns 2):
#echo off
choice /C YN /M "Yes or No"
echo The actual errorlevel is: %ERRORLEVEL%
pause
exit
But when i try with if statements something happens:
#echo off
choice /C YN /M "Yes or No"
if (%ERRORLEVEL% == 1) (echo You chose "Yes")
if (%ERRORLEVEL% == 2) (echo You chose "No")
pause
exit
Here no message is displayed... Any help? Am i doing something wrong?
Remove parenthesis:
if %ERRORLEVEL% == 1 echo You chose "Yes"
if %ERRORLEVEL% == 2 echo You chose "No"
Explanation
Unlike langauages like C, Javascript or Python, parenthesis in batch scripts denote blocks of code like { and }
So putting them around == checks is not needed and should not be used. If you want more complex expressions, you would instead probably have to split it into several if commands as outlined in this article about if
EDIT:
As noticed by Stephan :
In this case the mistake is caused by command processor
understanding ( and ) as a part of comparison strings,
rather than as special characters, and so interpreting
your statement as comparison between strings (%ERRORLEVEL% and 1)
so string "(1" is compared to "1)" - they do not match
so expression is false, but error is not generated since this is
technically valid syntax (even though it does not do what you wanted)
You could write "%ERRORLEVEL%" == "1" to disambiguate your intentions,
but really parenthesis should just not be used here
Although parenthesis around echo should work in this particular case on modern Windows...
they are not necessary when only one command is executed, and are usually excluded in such cases in batch scripts
I recommend to put them on their own lines (except that the opening parenthesis needs to be on the same line as if or for) to make sure they are not understood as a part of syntax of the command you are executing:
if %ERRORLEVEL% == 1 (
echo You chose "Yes"
)
Here is a more thorough explanation of parenthesis in batch scripts.
Additional note on exit:
You do not need exit at the end of the script unless you want the whole batch execution to be aborted.
This is relevant if one batch script is called from another, or in a subroutine.
If you want to end execution of your *.bat file or subroutine use goto :eof (note the colon) - this is similar to placing a label at the very end of the script and jumping to it with goto.
However, exit /b allows to exit a script and set ERRORLEVEL code to a specified value. This only returns from running subroutine or batch-script and not terminates cmd.exe as plain exit does.
More info on exit can be found here

Syntax error near unexpected token 'newline' while reading data from file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a problem with my programme. I try to read data from the file like this:
./programme <input.txt>
but there is an error:
bash: syntax error near unexpected token 'newline'
in my programme I use getline for reading data:
while(getline(cin,string))
{
...
}
It works properly when I input data from keybord.
This is what contains file input.txt:
0.2 0.1
1.2 0.2
1.2 1.1
1 2 0
0 1 2
You have to actually write to the standard input.
Try one of these:
./programme < input.txt
On Unix:
cat input.txt | ./programme
On Windows:
type input.txt | programme
I suppose that you actually literally typed:
./programme <input.txt>
That's a bash syntax error, because > must be followed by the name of a file to which output will be directed. But there is no file, so bash complains that it unexpectedly found the end of the line.
Similarly, < is followed by the name of a file from which input is taken.
So you presumably meant to write:
./programme < input.txt
Or you could have written
./programme < input.txt > output.txt
which would take input from input.txt and send output to output.txt.
(Whitespace before and after < and > is optional.)

cmd: replace inner part of a filename (using regular expression mask) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I run a small batch-file to rename some txt-file:
"C:\backup\t1-dd-dd t2-dd-dd.txt"
in the filename d - is a digit (from 0 to 9);
note: there is space in the filename between t1 and t2 marks (I need it for some reason).
now, I need to replace digits only in the 't1-dd-dd' part of the filename.
with powershell Rename-Item it can be done like this (example):
powershell -command "& { Get-ChildItem с:\backup -filter 't1-* t2-*.txt' | Rename-Item -NewName { $_.Name -replace 't1-\d\d-\d\d','t1-00-99' } }"
result would be like this:
C:\backup\t1-14-26 t2-56-48.txt (old filename)
C:\backup\t1-00-99 t2-56-48.txt (new filename)
is it possible to do same thing without powershell, just using cmd RENAME command?
You can't do it with regex, but you can do it with RENAME wildcards.
ren "c:\backup\t1-??-?? t2-??-??.txt" "???00-99*"
Within the target mask, each ? preserves one character, and the literals do a one for one character replacement, and the * preserves the remainder.
See How does the Windows RENAME command interpret wildcards? for more info.
If you are worried that the source mask is not specific enough, then you could use the following batch script to guarantee only properly named files are renamed.
pushd "c:\backup"
for /f "delims=" %%F in (
'dir /b /a-d "t1-??-?? t2-??-??.txt" ^| findstr /xirc:"t1-[0-9][0-9]-[0-9][0-9] t2-[0-9][0-9]-[0-9][0-9].txt"'
) do ren "%%F" "???00-99*"
popd
But your powershell script is probably easier :-)