batch file - use of variable - windows

#echo off
set filename =
cd GWConfig_TDS-mtpe3003
set filename = VCU17_CCU6\applications\VCU17APP
GOTO CHECKFILE
:CHECKFILE
echo reached
IF EXIST %filename% ( echo exists
) ELSE ( echo Doesnot exist )
/////////////////////////////////////////////////
Here output shows :
reached
Doesnot echo "exists" or "Doesnot exist"
Is there anything wrong with the use of variable "filename".
Also,
#echo off
set filename =
cd GWConfig_TDS-mtpe3003
set filename = VCU17_CCU6\applications\VCU17APP
GOTO CHECKFILE
:CHECKFILE
echo reached
IF EXIST VCU17_CCU6\applications\VCU17APP ( echo exists
) ELSE ( echo Doesnot exist )
gives output:
reached
exists.

There are two problems here. One is the space after the variable name:
SET filename = whatever
should be
SET filename=whatever
(Or you could use %filename % later, but that's just horrible :)
The second problem is that without any quotes, your "IF" test won't work properly if %filename% is empty. Quote it instead:
IF EXIST "%filename%" ( echo exists
) ELSE ( echo Doesnot exist )

I don't have the time to properly re-create this now, but I can spot a few potential problems:
Have you tried removing the spaces around the = in set:
set filename=VCU17_CCU6\applications\VCU17APP
otherwise you might have a single space leading your file name
You might want to try to quote the use of %filename%:
IF EXIST "%filename%" ( ...

Related

In windows batch file, re-set variable inside if-else block doesn't work

I've got the code below, try to re-set variable "name" inside an if-else block:
#echo off
set name=kk
echo %name%
if "%name%"=="jj" (
echo case1
) else (
echo case2
set name=ll
echo name=%name%
)
Under cmd of win10, it outputs:
aa
kk
case2
name=kk
This is weird, I wish that my last echo should print:
name=ll
Seems the "set name=ll" didn't work. So would you help to explain why it didn't work as I expected, and how to fix it?
You need a delayed expansion
#echo off
setlocal enableDelayedExpansion
set name=kk
echo %name%
if "%name%"=="jj" (
echo case1
) else (
echo case2
set name=ll
echo name=!name!
)

Setting environment variable using batch file

I want to set PATH for a tool I wrote using a batch file. But I only want to modify it if the path I want to add isn't already contained in that String. I think I get the syntax wrong because I can't get it to work.
#setlocal enableextensions enabledelayedexpansion
#echo off
set MinGWmsys="%CD%\tools\MinGW\msys\1.0\bin;"
set MinGWbin="%CD%\tools\MinGW\bin;"
set SDCCbin="%CD%"\tools\SDCC\bin;"
set lpath="%PATH%"
if not x%lpath:%MinGWmsys%=% == x%lpath% (
echo PATH already contained %MinGWmsys%
) else (
echo Adding %MinGWmsys% to PATH
setx PATH "%MinGWmsys%;%PATH%"
)
if not x%lpath:%MinGWbin%=% == x%lpath% (
echo PATH already contained %MinGWbin%
) else (
echo Adding %MinGWbin% to PATH
setx PATH "%MinGWbin%;%PATH%"
)
if not x%lpath:%SDCCbin%=% == x%lpath% (
echo PATH already contained %SDCCbin%
) else (
echo Adding %SDCCbin% to PATH
setx PATH "%SDCCbin%;%PATH%"
)
endlocal
Can somebody help me here please?
x%lpath:%MinGWmsys%=%
is parsed as two variables: %lpath:% and %=%, leaving the string inGWmsys as is. Try:
echo x%lpath:%MinGWmsys%=%
and you will see it.
Instead, you should use
if not "!lpath:%MinGWmsys%=!" == "%lpath%" (
so that %variable% is interpolated before !another:value=! (adapted from this post). I used quotation marks instead of x because if seems to (mis)interpret =! even before the variables are interpolated.
A second problem is the quotation marks:
set MinGWmsys="%CD%\tools\MinGW\msys\1.0\bin;"
should be
set MinGWmsys=%CD%\tools\MinGW\msys\1.0\bin;
because obviously your path does not contain a string "%CD%\tools\MinGW\msys\1.0\bin;" with quotation marks.
This works:
#setlocal enableextensions enabledelayedexpansion
#echo off
set MinGWmsys=%CD%\tools\MinGW\msys\1.0\bin;
set lpath=%PATH%
if not "!lpath:%MinGWmsys%=!" == "%lpath%" (
echo PATH already contained %MinGWmsys%
) else (
echo Adding %MinGWmsys% to PATH
setx PATH "%MinGWmsys%;%PATH%"
)
endlocal

How can I check if a variable contains another variable within a windows batch file?

Assuming the following batch file
set variable1=this is variable1
set variable2=is
set variable3=test
if variable1 contains variable2 (
echo YES
) else (
echo NO
)
if variable1 contains variable3 (
echo YES
) else (
echo NO
)
I want the output to be a YES followed by a NO
I've resolved this with the following
setLocal EnableDelayedExpansion
set variable1=this is variable1
set variable2=is
set variable3=test
if not "x!variable1:%variable2%=!"=="x%variable1%" (
echo YES
) else (
echo NO
)
if not "x!variable1:%variable3%=!"=="x%variable1%" (
echo YES
) else (
echo NO
)
endlocal
I got the basic idea from the following answer but it wasn't searching by a variable so it wasn't completely what I was looking for.
Batch file: Find if substring is in string (not in a file)
another way:
echo/%variable1%|find "%variable2%" >nul
if %errorlevel% == 0 (echo yes) else (echo no)
the / prevents output of Echo is ON or Echo is OFF in case %variable1% is empty.
Gary Brunton's answer did not work for me.
If you try with set variable1="C:\Users\My Name\", you will end up with an error :
'Name\""' is not recognized as an internal or external command
Adapting this answer Find out whether an environment variable contains a substring, I ended up with :
echo.%variable1%|findstr /C:"%variable2%" >nul 2>&1
if not errorlevel 1 (
echo Found
) else (
echo Not found
)
The following is based on JBEs answer in this thread. It distinguishes empty/undefined variables.
if not defined VARIABLE1 (echo VARIABLE1 undefined) & goto proceed
if not defined VARIABLE2 (echo VARIABLE2 undefined) & goto proceed
echo %VARIABLE1% | find "%VARIABLE2%" > nul
if ERRORLEVEL 1 (echo Not found) & goto proceed
echo Found
:proceed
If the value of VARIABLE1 contains parentheses, e.g. the value C:\Program Files (x86), then the parentheses may be interpreted as a distinct command rather than echoed, causing an error. Substrings like (x86) can be escaped with carets for purposes of echoing properly, e.g.:
SET V1_DISPLAY=%VARIABLE1%
if /i "%V1_DISPLAY:(x86)=%" NEQ "%V1_DISPLAY%" set V1_DISPLAY=%V1_DISPLAY:(x86)=^^(x86^^)%
echo %V1_DISPLAY% | find "%VARIABLE2%" > nul
The second statement, above, can be read as follows:
If replacing substring (x86) with nothing makes a difference, then (x86) is present, so replace any occurrence of (x86) with ^^(x86^^), where each pair of carets represent an escaped single caret.

Batch To get Count Of 0 Byte Files

I'm trying to run a batch to get the number of 0 byte files in a directory. If the count is one or more then I want to delete the files otherwise quit. Here is what I have so far.
#echo off
if "%~z1" == "" (
echo File does not exist.
) else if "%~z1" == "0" (
echo File is empty.
) else (
echo File is non-empty.
)
(Edited, see comments. Original answer below.)
The following script counts empty files in a directory, then, if the count turns out greater than 0, deletes the empty files. The directory is specified as a parameter to the batch script. For example, if you need to process the directory of C:\Users\DS\Downloads, call the script like this (assuming script.bat is the script file's name):
script.bat C:\Users\DS\Downloads
This is the script:
#ECHO OFF
IF "%~1" == "" (ECHO Usage: %~nx0 path\to\files& GOTO :EOF)
SET "workdir=%~1"
SET count=0
SET "command=SET /A count+=1"
CALL :processempty
ECHO Number of empty files: %count%
IF %count% GTR 0 (
ECHO Deleting files...
SET "command=DEL ^"%%~F^""
CALL :processempty
)
GOTO :EOF
:processempty
FOR %%F IN ("%workdir%\*") DO (
IF "%%~zF" == "0" %command%
)
Original answer follows:
#ECHO OFF
SETLOCAL
SET firstfile=
SET delfirstfile=
FOR %%F IN (*) DO (
IF "%%~zF" == "0" (
IF DEFINED firstfile (
ECHO DEL "%%F"
SET delfirstfile=1
) ELSE (
SET "firstfile=%%F"
)
)
)
IF DEFINED delfirstfile ECHO DEL "%firstfile%"
ENDLOCAL
The above script works like this:
It iterates through all the files in the current directory and checks their sizes.
When the first empty file is found, its name is stored into a variable.
Every subsequent empty file is deleted and another variable is set to indicate that the first empty file should be deleted too.
After the loop the indicator variable is checked. If it is set, the first empty file is deleted.
PS. I would suggest you first to run this script as is to make sure it works correctly. Afterwards you'll need to remove ECHO in front of each of the two DEL commands to let the script actually remove files.

Set variable in "if" block

The following program always echoes "machine-abc" in the end:
#echo Off
set dropLoc=machine-abc
IF %computername% == "xyz" (
%dropLoc% = machine-xyz
)
echo %dropLoc%
Is this a scope issue? Does the dropLoc variable in the if statement have a different scope? I have tried the following to address the issue:
#echo Off
set dropLoc=machine-abc
IF %computername% == "xyz" (
!dropLoc! = machine-xyz
)
echo %dropLoc%
and
#echo Off
set dropLoc=machine-abc
IF %computername% == "xyz" (
set dropLoc = machine-xyz
)
echo %dropLoc%
How do I make this work?
You got the SET syntax right the first time, how come you decided to write something else the second time round? Also, you have to add the quotation marks on both sides of the comparison. Unlike in other script interpreters, quotation marks aren't special for the batch interpreter.
#echo off
rem Change this for testing, remove for production
set computername=xyz
set dropLoc=machine-abc
if "%computername%" == "xyz" (
set dropLoc=machine-xyz
)
echo %dropLoc%

Resources