How to check if an "if" statement contains something [duplicate] - batch-file

This question already has answers here:
Batch file: Find if substring is in string (not in a file)
(13 answers)
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
(1 answer)
Closed 1 year ago.
For example:
if "%input%" contains "Hello"
I've also tried:
if "%input%" neq "Hello" (
echo example
)
But whenever I press ENTER, or type anything, I didn't set here, it just returns example
Any way to fix this?

Related

Batch File How to echo Series Variable's Value in a loop [duplicate]

This question already has answers here:
Arrays, linked lists and other data structures in cmd.exe (batch) script
(11 answers)
Batch Script - How to get variable value using a different variable that has the variable name stored
(1 answer)
using a DelayedExpansion index variable for an array within an IF statement fails
(2 answers)
Closed 3 years ago.
I have a series variable named Installer1 to Installer18
I am trying to install all installer 18 using a loop, so far I have this code to check if I am getting the value but the real value of variable is not reflecting, I believe there is missing on my code but I have tried my best but it is not enough. Thank you in advance
Here is what I have so far
I've got this result, I want to echo the value of the variable i declared on top of my loop but I got the variable name instead of value. I am not sure how to concatenate string and script will look it as a variable name.
I tried below code:
And I got this result:
Use array , Please try this.
#echo off
setlocal enabledelayedexpansion
SET var[0]=A
SET var[1]=foo bar
SET var[2]=123
for %%a in (0,1,2) do (
echo !var[%%a]!
)
echo Get one of the variables directly: %var[1]%

String compare with batch file [duplicate]

This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 3 years ago.
A very simple batch file. I'm trying to search for file extensions that are not .txt. There will be one .txt, but the rest will be like .txt_20190607.
for %%I in (\\01mtsdv130\Myapp\Log\*.*) do (
set var1=%%~xI
echo %var1
if %var1%==".txt" (
echo Matches
) else (
echo does not match
)
)
I have files in that folder both .txt and those with the extra date info in the extension. What do I have wrong?
There are two problems in the code.
The first one is that %-based expansion of normal variables is rather "static", in that it happens the first time code is parsed/executed and is fixed since then. That means that in iterations of the loop after the first, the result of %var1% will not change. You'd have to use !var1! (along with setting EnableDelayedExpansion) to get the behaviour you want.
An easier alternative is to get rid of var1 altogether and just use %%~xI.
The other problem is that CMD treats quotes (almost) as any other character. Most notably, the strings a and "a" are not considered equal. Therefore, the if should look like this:
if "%%~xI"==".txt" (

How to echo something to a .txt file even if it's executable as a program BATCH [duplicate]

This question already has answers here:
using batch echo with special characters
(8 answers)
Closed 4 years ago.
I have a BATCH script that will create a text file that will later be executed by another program based on user input. I want to be able to echo Label>start to a text file. Unfortunately, CMD reads it as a command (because of the > character) and does not echo properly. If I use quotation marks they echo to the .txt file
"Level>start"
and so it cannot be executed. I really need some help with this.
echo Level^>start
seems to work.

Create cmd command with parameters in batch [duplicate]

This question already has answers here:
How can I pass arguments to a batch file?
(20 answers)
Closed 6 years ago.
I want to create a cmd command by writing a batchfile and put it into "system32" to call it in the cmd console by its name. Is there a way to expect parameters in the batch file:
Write in cmd:
fake-command Test
And then work with the string "Test" in the batch file?
Use %1 to access the first argument, %2 for the second, etc.

Copy from variable to variable [duplicate]

This question already has answers here:
Assign command output to variable in batch file [duplicate]
(5 answers)
Closed 6 years ago.
For my batch file I need to do a few different things...I have completed steps 1-3
1) Perform a dir search and save the result of that search in a variable
2) Set the destination path as a variable
3) Copy the source file to the destination path
My code so far :
#echo off
cls
cd /d D:\Downloads\Videos
set "flshvid=&dir *flash*.mkv /s /b"
set "flshdir=O:\Aayush\Videos\TV Shows\The Flash\Season 3"
xcopy %flshvid% %flshdir%
Why doesn't this code work?
Any help is appreciated. Thanks in advance!
Environment variables may not start with a numeric because %n where n is 0..9 refers to the serial parameter number provided to the routine.
replace 1 and 2 with variable names that do not start with a numeric

Resources