Dos - How to check Variable Name exist based on user Input? - dos

I have below batch code and I want to check when user type e.g. SB1 as input, SB1 Varialbe Name exist. In my case it does exist. But if user type SX1 then it does not exit.
#ECHO OFF
set "SB1=hannlsb1.mydomain.com:30515 -i 05"
set "DB1=hannlsd2.mydomain.com:31315 -i 13"
set "QB1=hannlsqa1-1:30115 -i 01"
set "DB0=hannlsd1.mydomain.com -i 10"
set "QB0=hannlsps1-1.mydomain.com-i 03"
set "WB1=hannlsqa1-1:30315 -i 03"
set "VB1=hannlsvt-1.mydomain.com -i 01"
#ECHO OFF
SET /P sysid=Please enter SystemID:
IF "%sysid%"=="" GOTO Error
REM ECHO Hello %sysid%, Welcome to DOS inputs!
GOTO End
:Error
ECHO You did not enter System ID! Bye bye!!
:End

Here's one way using FOR /F to run an echo command which expands the sysid into its value in the middle of an expression that then expands the value again.
#echo off
set "SB1=hannlsb1.mydomain.com:30515 -i 05"
set "DB1=hannlsd2.mydomain.com:31315 -i 13"
set "QB1=hannlsqa1-1:30115 -i 01"
set "DB0=hannlsd1.mydomain.com -i 10"
set "QB0=hannlsps1-1.mydomain.com-i 03"
set "WB1=hannlsqa1-1:30315 -i 03"
set "VB1=hannlsvt-1.mydomain.com -i 01"
SETLOCAL ENABLEDELAYEDEXPANSION
SET /P sysid=Please enter SystemID:
FOR /F "usebackq delims==" %%i IN (`echo+!%sysid%!`) DO SET val=%%i
SETLOCAL DISABLEDELAYEDEXPANSION
IF NOT "%val%"=="" (
ECHO Hello %sysid%, Welcome to DOS inputs!
) ELSE (
ECHO You did not enter System ID! Bye bye!!
)
Note that the delayed expansion has an unfortunate side-effect that you cannot include exclamation marks in your echo commands while it is enabled.

Related

"ECHO is on" is setting in variable

I have a batch file which takes three parameters [log:path], [logok:path], [logerr:path]. The values of the respective parameter is log:c:\logs\install.log, logok:c:\logs\installok.log,
logerr:c:\logs\installerr.log.
I need to process these 3 parameters and set it on respective variable log=c:\logs\install.log, logok=c:\logs\installok.log, logerr=c:\logs\installerr.log so that I can use them in next step to create a file in those paths.
I have written below script but somehow each variable is printing "ECHO is on". It should actually print the location path. Any idea how to achieve this?
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> C:\LOGS\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ENDLOCAL
ECHO %LOG% >> C:\LOGS\TEST2.TXT
ECHO %LOGOK% >> C:\LOGS\TEST3.TXT
ECHO %LOGERR% >> C:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> %LOGOK%
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST %LOGOK% ECHO FAILED >> %LOGERR%
Output of TEST1.TXT:
log:c:\logs\install.log
logok:c:\logs\installok.log
logerr:c:\logs\installerr.log
Output of TEST1.TXT,TEST2.TXT,TEST3.TXT:
ECHO is on.
Try this:
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> c:\Logs\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ECHO !LOG! >> c:\LOGS\TEST2.TXT
ECHO !LOGOK! >> c:\LOGS\TEST3.TXT
ECHO !LOGERR! >> c:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> !LOGOK!
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST !LOGOK! ECHO FAILED >> !LOGERR!
ENDLOCAL
Basically this:
Extends the local scope so that the variables which were assigned inside the FOR loop will be available in the rest of the script
Moves variables to the !delayed! variable syntax in order to cause them to be evaluated as late as possible. See the documentation for "EnableDelayedExpansion" for more details
Note that "ECHO is on." is the output that you get when calling echo with no parameters:
C:\>type echo_test.bat
echo
echo %no_such_variable%
C:\>echo_test.bat
C:\>echo
ECHO is on.
C:\>echo
ECHO is on.

Create a numbered list based on a given list of strings

windows cmd batch
I have a text file that looks like this:
Dog
Cat
Frog
Bat
Bird
Mouse
I want to attribute a number to each string, each line.
So that it becomes
1 Dog
2 Cat
3 Frog
4 Bat
5 Bird
6 Mouse
Then I want to ask the user to input a number, and then have the corresponding string stored in the variable.
So if the user inputs 1, then the variable is set to the string Dog
So when the user inputs 1 the program stores/outputs Dog
set /p var1="number? " so var1 becomes the string, not the number.
This does the first part of the task kinda, but now I need the second part, storing the strings in avariable.
#echo off
set TEXT_T="list.txt"
set /a c=0
setlocal ENABLEDELAYEDEXPANSION
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
)
endlocal
pause
Here below is an updated answer, thanks to LotPings.
With a small tweak to ask for the folder by string
This provides an easier way to use Megatools from CMD
https://megatools.megous.com/man/megals.html
https://github.com/megous/megatools
#echo off
:start:
megals /Root/
set /p var1="dir? " & megals /Root/%%var1%%
for /f "tokens=1,* delims=:" %%A in ('megals -n /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)
for /f "tokens=1,* delims=:" %%A in ('megals -n -e /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
)
set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%
set "trimmedlink="
for %%h in (%Link%) do if not defined trimmedlink set "trimmedlink=%%h"
Megadl %trimmedlink% && goto :start:
pause
Edit: Had to trim %Link% to just the first word, i.e just the link
The output of Megals -e /Root/misc looks like this:
The asterisk are the unique link ids for the files
/Root/misc
https://mega.nz/#!********!********************* /Root/misc/File1
https://mega.nz/#!********!********************* /Root/misc/File2
https://mega.nz/#!********!********************* /Root/misc/File3
With the batch script above it looks like:
1 File1
2 File2
3 File3
Select #: <------input file number
Edit2 number listing is fixed
Edit3 Parentheses in filenames crash the program
i.e
1 File(1)
The chosen file number then gets passed to Magadl as the corresponding link
Megadl Link
The batch script allows you to download the link by just entering the corresponding file number so you don't have to type out the long link id in a standard cmd window.
To use megals output directly and avoid delayedexpansion (which removes the !)
Findstr /n will do the numbering.
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
set Item[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Echo Choice:%%Item[%choice%]%%
Using a (pseudo-)call with doubled percent signs is an old fashioned method of realizing delayed expansion without the problem with the !.
In programming/scripting you need to adapt techniques to fit your needs.
Without knowing the exact output of your megatools,
this could do the job :
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
set Folder[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Set Folder=%%Folder[%choice%]%%
for /f "tokens=1,* delims=:" %%A in ('megals -e %Folder% ^|findstr /n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%
megadl %Link%
As compo advised, please edit your question to contain all necessary information - don't force others to gather it from unnecessary answer and comments.
Just use an array:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set TEXT_T="list.txt"
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
echo !string[%number%]!
pause
For further details, see this answer.
What about making the output of a command into a list, instead of a text file into a list?
so the line with var1 here would be turned into a numbered list (the files listed in that directory), and Var2 is the number the users enters to create the returned string that gets passed to a command. i.e
https://megatools.megous.com/man/megals.html
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
(megals -e /Root/) <--------list all folders in root
(set /p var1="dir? " && megals -e /Root/!var1!) <-- select folder + list files
(set /p var2="Megalink? " && Megadl !var2!) <--- enter the file name for download
ENDLOCAL
pause
I want to be able to enter a number for the download, instead of the long file name. So this way the user can enter the number that corresponds to the link that they want to download. So if they enter 1 then the program does Megadl Dog
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
megals -e /Root/
set /p var1="dir? "
megals -e /Root/!var1! > rlist.txt
set TEXT_T="rlist.txt"
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
Megadl !string[%number%]!
Endlocal
pause
This kills the first part of the link because it removes everything in between and including exclamations. !dasasdasd!
all megalinks start with #!something!
This didn't work
I need output of Call Echo %%Item[%choice%]%% passed to Megadl
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/anime ^|findstr /n "." ') do (
set Item[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Echo %%Item[%choice%]%%
for /F "tokens=*" (`%%Item[%choice%]%%`) do (
set "var1=%%A"
Megadl !Var1!
)
pause

If statement in batch scripting

What I am trying to do is to check if the variable %%c contains a "More" if that is the case insted of a more there should be a "not granted"
#echo off
setlocal EnableDelayedExpansion
for /f "usebackq skip=10 tokens=1,5,9 delims= " %%a in ("log.txt") do (
if %%c == "more"(
set "status" ="Not granted"
)
set "date=%%a"
set "type=%%b"
set "status=%%c"
echo type: !type!
echo status: !status!
echo date: !date!
echo.
)>>Test.txt
pause
Yoru basic questin is: "Does a variable contain a certain string"
In your case (using a for variable), I would use:
echo %%c|find "more">nul && set "status=Not granted"
print the string, pipe it to find (redirecting the ouptut to NUL) and if find was successful, then do something

Why isn't my BAT file variable being overwritten?

I have this block of code:
#ECHO OFF
SET "SRCFOLDER=C:\Users\MyUserName\Desktop\PhotoTests"
SET "TEMPCODE=Hi"
ECHO %TEMPCODE%
ECHO.
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
)
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
PAUSE
I'm confused by the output as I noticed that the variable in the FOR loop is not overwritten by what I think should be the content of "test.txt", which will vary each time the FOR loop runs.
For the purpose of this example, the file tooltipInfo.bat will echo a text string like "1 Star" or "3 Stars" based on the rating recorded in the file's properties. The FIND statement should result in a "0" or "1" being saved into the test.txt file.
The output is:
Hi
Canon_Locked.JPG
Hi
Nikon_Locked.JPG
Hi
0
Press any key to continue . . .
May I know why the TEMPCODE variable isn't being overwritten in the loop and retains the original value of "Hi". However in the final block of code, it was able to read and echo out the actual content of the file.
This is a common mistake when using FOR and parentheses. The problem is that by default every variable between ( and ) is evaluated when the line is first read, and then re-used for each iteration of the FOR.
If the variable changes during the loop (via SET), then you will need to change % to ! for variables inside the parentheses, and also turn on delayed expansion using setlocal
SETLOCAL enabledelayedexpansion
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO !TEMPCODE!
ECHO.
)

Why is the variable disapearing?

I made a names.bat file
that will make a folder called profiles then add a batch in the profiles folder to view the profiles and I'm having a problem with the creation of program veiwer.bat
the names.bat contains this code:
#echo off
TITLE Profiles
SET /a YEAR=%DATE:~6,4%
if not exist Profiles mkdir Profiles
CD Profiles
ECHO #echo off>"Profile Viewer.bat"
ECHO :profile>>"Profile Viewer.bat"
ECHO title Profile Viewer>>"Profile Viewer.bat"
ECHO SET /P NAME=Search names:>>"Profile Viewer.bat"
ECHO call %NAME%.bat>>"Profile Viewer.bat"
ECHO goto profile>>"Profile Viewer.bat"
GOTO RES
:RES
SET /P NAME=State your name:
ECHO #echo off>%NAME%.txt
ECHO title %NAME%>>%NAME%.txt
ECHO echo Name:%NAME%>>%NAME%.txt
SET /P AGE=State your Age:
ECHO echo Age:%AGE%>>%NAME%.txt
SET /a YOB=%YEAR%-%AGE%
SET /P A=Where you born in %YOB%(Y/N)
IF %A%==Y goto YES
IF %A%==y goto YES
IF %A%==N goto NO
IF %A%==n goto NO
:NO
SET /P YOB=State your Year of birth:
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE
:YES
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE
:CONTINUE
ECHO pause>>%NAME%.txt
rename *.txt *.bat
start %NAME%.bat
%SystemRoot%\explorer.exe "C:\Users\%username%\Desktop\Profiles"
echo Here is your profile
pause
so I edit "Profile Viewer.bat" to see if it works it has this
#echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call .bat
goto profile
Where did the %NAME% variable go?
I want it to be in the profile viewer.bat
i know its an empty variable i want it to literally say in the code this;
#echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call %NAME%.bat
goto profile
I know I could make it myself but I want name.bat to do it for me
ECHO call %NAME%.bat>>"Profile Viewer.bat"
When this line is executed, the %NAME% variable is expanded. Since it hasn't been defined yet, it expands to nothing. Thus, we get the erroneous line in Profile Viewer.bat.
To prevent %NAME% from being expanded, we need to tell the command interpreter that we want an actual % instead of starting a variable. So, we need to escape the % like so:
ECHO call %%NAME%%.bat>>"Profile Viewer.bat"

Resources