delete the value of variabel in batch after code is done - batch-file

I wanna make an if/else statement where if my variabel is empty it has to echo an text but it is never empty because it keeps giving me the previous value I wrote in cmd. How can I possibly solve this, I tried using startlocal and endlocal but I couldn't get it to work this is my code:
#echo off
set /p "Input= text:"
FOR %%a IN (%Input%) DO (
IF "%%a"=="" (echo Write atleast 1 word) else (
ECHO %%a
)
)
In this picture u can see that if I write something first and click enter it puts everything underneath each other but the second time I wrote nothing and got the same values back
Cmd picture

Remove whatever is in the input variable at the beginning.
#echo off
SET "Input="
set /p "Input= text:"

Related

Echo not working properly inside IF ELSE structure

I have had plenty of problems when moving lines inside IF/ELSE structure. Code below does not print
echo %VALUE1%;%VALUE2:~0,2%;%VALUE3%;%VALUE4%;%VALUE5%
to file. Instead of that values are printed to console and last character is dropped. What is the problem?
#echo off
setlocal enabledelayedexpansion
SET ENABLED_X=1
SET FILE=test.txt
SET VALUE1=23,5
SET VALUE2=34,1
SET VALUE3=0,45
SET VALUE4=3,33
SET VALUE5=3,5
IF /I %ENABLED_X%==0 (
echo %VALUE1%;%VALUE2%;%VALUE3%;%VALUE4%>>%FILE%
echo NOT ENABLED
) ELSE (
echo %VALUE1%;%VALUE2:~0,2%;%VALUE3%;%VALUE4%;%VALUE5%>>%FILE%
echo %VALUE1%;%VALUE2:~0,2%;%VALUE3%;%VALUE4%;%VALUE5%
echo ENABLED
)
The part 5>>test.txt will be interpreted as redirection. Normally it's used as 1>>file or 2>>file to redirect standard output and error output, respectively.
Use
>>%FILE% echo %VALUE1%;%VALUE2:~0,2%;%VALUE3%;%VALUE4%;%VALUE5%
instead. Doing the redirection at the beginning of the line will never let it interfere with something else.
Note: without quotes, %FILE% is ok. You might run into trouble when using spaces in the file name.
Just place a single space before both of your >>
or
(echo %VALUE…)>>%FILE%

How to change the delimiter of for /f function loop in a bat file and how to not output the result of the program in a new trailing line

This is my first stop so please point out any errors I have made into my post or way of resolving this problem! So, I have a small project with my friends in which we have to make a batch script in which it has to access a text file with a few words on the first lines. Selecting one word at the time, we have to access a site and find it a synonym and paste the result. Here is what I have done so far:
#echo off
title Prg1
set COS=
:b
for /f "Delims=%COS%" %%a in (word.txt) do (
set SIN=%%a
set COS=%SIN%
)
cd C:\
cd Folder
* finds synonim
:Ask
echo Change the word " %SIN% " with the above word? (Y/N)
set INPUT=
set /P INPUT=Type input: %=%
If /I "%INPUT%"=="y" goto yes
If /I "%INPUT%"=="n" goto no
:yes
*send synonym* >> sinonim.txt
echo The transfer was succesful!
pause
goto :b
OK so I am sorry that I wasn't able to put the code in a box just like the other posts, I wasn't able to figure it out. Alright then, I have 2 errors:
1) At the beginning of the program I gave COS the value of space so when it would enter the for loop the delimiter would be the space too. I then asigned SIN the value of the first word in the text file. Right after that I gave COS the same value as SIN so after all the other commands would be executed, goto :b would return me to the for loop and I would have a new delimiter: the first word in the text thus SIN will then be set as the second word and the loop begins again. My problem is that even though COS got the SIN value, when it accesses the loop it one again has the space value so the script has no choice but do the same thing over and over again. What should I do?
2) When the command send synonym >> sinonim.txt executes it places the new word on a new line. Now, I have researched this for quite a while today but I couldn't solve it since the code is something like this : (grep "blabla" blah.html) >> sinonim.txt. I tried to write it as (echo|set /p=(grep "blabla" blah.html)) >> sinonim.txt but it didn't work. Again, what should I do?
I would really appreciate if you could answer both of my question StackOverflow Community! Have a great day!
Your logic is bad. You can't process a file word by word. Instead you have to process it line by line and with another for process each line word by word.
By the way: your for loop ends before anything gets done, resulting in %SIN% being the last line of your file only.
Pseudo-Code:
for every line in word.txt do
for every word in that line do
(get the synonyme)
write the synonyme (without linefeed)
next word
write a linefeed
next line
Finally redirect the whole output to the new file.
Your code should look something like:
#echo off
setlocal enabledelayedexpansion
(
for /f "delims=" %%a in (word.txt) do (
for %%b in (%%a) do (
rem replace next line with some code to get the synonyme of %%b
set "syn=[%%b]"
<nul set /p "line=!syn! "
)
echo/
)
)>out.txt
Note: this is just a "how-to" - it ignores empty lines and some punctuation (because they are treated as standard delimiters)

Conditional IF function for Batch File

Firstly this is a work around to another issue, but to not complicate this question I'll not go into what the original approach was.
Now I am trying to use a batch file to:
Read a text log file,
loop through to the penultimate line of the file
then do a conditional check to see if the last entry was a confirmation that a process had completed successfully or another message which would indicate it did not.
Based on the result of step 3 above, return the value of 0 for pass and 1 for fail so that SSIS can interpret the result.
The name of the text file is fed into the batch file as a parameter.
Everything seems to work except for when I try to include an IF statement at which point it just doesn't return any result.
Can anybody advise what I'm doing wrong? - have tried to follow the guidance here: http://ss64.com/nt/if.html but clearly without success!
#ECHO off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (%1) do (
set "previous=!last!"
set "last=%%x"
)
IF !last! == "EXPORT completed successfully" ECHO 1 ELSE ECHO 0
If the last line is changed to say ECHO !last! it correctly returns the string EXPORT completed successfully. I have tried the above code with & without quotes but to no success.
Any help would be appreciated as I've never had to use batch before.
Thanks
You were very close! Note that I've added brackets around your IF and ELSE clauses. I've also added quotes around your last.
Finally, bear in mind that the whitespace around an IF...ELSE in batch is actually important - see this answer for details.
#ECHO off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (%1) do (
set "previous=!last!"
set "last=%%x"
)
IF "!last!" == "EXPORT completed successfully" (
ECHO 1
) ELSE (
ECHO 0
)
ECHO !last!
I think that when you set a value in a command line using the set command, you should refer to this variable using %. For example:
#echo off
cls
:loop
rem DO SOMETHING
set /p userinp=Repeat (y,n)?
if "%userinp%" == "y" (goto loop)
pause
This batch does something and then asks a user if the procedure needs to be repeated using a userinp variable. Hope this helps.

trying to make a score system

well I'm trying as title say> to make a score system. ive been googling. and tried different things but I simply cant get it to work
i want it to work like this: you type in your name. then it checks if the dll file exist in the players folder. if it doesn't, it will make it. and if it does. it will just overwrite.
if it does exist however. it will first retrieve the existing score value from the dll file.
then I want it to just add in 1 point by every time
you come to the :addscore section.
and each time it passes :addscore. this new value will then be saved to the file (replaced) and then this continues :P
heres the code I got so far: and as u can see. it does not retrieve the score from the file. and neither it will set the score value any higher than 1... any tips?
#echo off
:: User check
:usercheck
set /p usrn=Username:
set score=0
if exist "D:\General_menus\users\%usrn%\playerdb.dll" (
for /f %%A in ("D:\General_menus\users\%usrn%\playerdb.dll") do set score=%%A
echo Welcome back %usrn%
goto addscore
) else (
echo Hello new player %usrn%
set score=0
)
pause>nul
:: user check end
::====================================================
::add score
:addscore
set /a score+=1
echo your score is %score%
echo saving data
goto scorecheck
::add score end
::===================================================
:scorecheck
echo %score%
echo %score% > "\General_menus\users\%usrn%\playerdb.dll"
pause>nul
goto usercheck
Problem solved by: -#Thilo
fix: remove the bunny ears from line:" for /f %%A in ("D:\General_menus\users\%usrn%\playerdb.dll") do set score=%%A"

Windows shell-scripting - 'set variable' does not set current value but takes previous one

If I run this script several times it would take previous value to be set.
SET /P VALUE_FROM_FILE= < app.pid
echo %VALUE_FROM_FILE%
So If I run it first time it prints me "333"
Then I go to app.pid file and change its content to "444"
run this script again, it prints "333"
What is the proper way to handle that kind of behavior?
--
Update: I've rewrited my script like that to behave properly
for /f %%r in (result.txt) do (
echo %%r
)
that would only display the first word in each line .. to make it get all text in file use this instead..
for /f "tokens=*" %%r in (filename.extension) do (
echo %%r
)
that should do .. hope it helps ...

Resources