I need to load different numeric values from external config.txt file and write them to %variables% in batch file. Example - config.txt file should looks as following:
====================
Setting1=1
Setting2=0
Setting3=1
====================
I need to assign first value (1) lets say into variable %1%, second value (0) into variable %2% and so on.
Could you please help me how to do this?
Thanks.
try this:
#echo off&setlocal
for /f %%i in (config.txt) do set "%%i" 2>nul
set "setting"
Do you know how many variables there will be? If you don't then go with Endoro's answer. If you do know how many variables there will be and you want to set them to a custom name you could do this:
#echo off
< config.txt (
set /p var1=
set /p var2=
set /p var3=
)
echo %var1%
echo %var2%
echo %var3%
pause
You shouldn't use %1% or plain numbers for variables, it can mess it up.
If config.txt had those '=' signs then you would have to skip add two more lines for the var's.
The reason you should use this for custom variables is because you could name the variables by there specific meaning which may make it easier to remember when coding.
Ex.
#echo off
< config.txt (
set /p name=
set /p pizza=
set /p car=
)
echo %name%
echo %pizza%
echo %car%
pause
Related
Making batch which generate previews (everything is fine with this part of code) and also rename files deleting everything after "_" in filename. For example ABAB_abab.png > ABAB.png
My code does not see a variable yy in the string: set zz=!xx:yy=! Perceives it like just two letters yy, not a variable. How to fix that?
Here is the script
setlocal enabledelayedexpansion
for %%a in ("*.png") do (
set xx=%%~na
set yy=_!xx:*_=!
set zz=!xx:yy=!
echo xx= !xx! #rem (okay, returns ABAB_abab)
echo yy= !yy! #rem (okay, returns _abab)
echo zz= !zz! #rem (wrong, returns ABAB_abab without any substitutions)
pause
)
endlocal
Thank you for help
Here's a quick example to show you a method of achieving another layer of expansion:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For %%G In ("*.png") Do (
Set "xx=%%~nG"
SetLocal EnableDelayedExpansion
Set "yy=_!xx:*_=!"
For %%H In ("!yy!") Do Set "zz=!xx:%%~H=!"
Echo xx = "!xx!"
Echo yy = "!yy!"
Echo zz = "!zz!"
EndLocal
Pause
)
The doublequotes are included in the Echo commands only for better visualization should there be any spaces in your strings, they're not needed for any other purpose.
Please note, that this will not achieve your intention with any .png files whose basename begins with one or more underscores, _.
I want a text file with just a number that is getting iterated every time i use the batch file. I tried this so far but its not working...
set laufende_nr=0
if not exist echo %laufende_nr% > %def_log_dir%\laufende_nr.tmp
for /L %%i in (%def_log_dir%\laufende_nr.tmp) set laufende_nr=%%i
echo %laufende_nr%
set /a laufende_nr+=laufende_nr
echo %laufende_nr_neu% > %def_log_dir%\laufende_nr.tmp
Here's some quick example code for you to study:
Set "laufende_nr="
If Exist "%def_log_dir%\laufende_nr.tmp" (
Set/P "laufende_nr="<"%def_log_dir%\laufende_nr.tmp"
)
If Not Defined laufende_nr Set "laufende_nr=0"
Echo %laufende_nr%
Set/A laufende_nr+=1
(Echo %laufende_nr%)>"%def_log_dir%\laufende_nr.tmp"
Enter Set/?, If/? & Echo/? as necessary for respective information.
EditIf you have a relative directory named Set which contains an executable file named either A or P, then you would be best advised to change Set/A to Set /A and/or change Set/P to Set /P as necessary.
Is there a way I can use a set command to isolate a few words from a variable given and then save that as another variable to use later?
If I say cd C:\users, can I take something like the C:\users and save that as a variable?
:inputstage
set /p input=Enter command:
if /i {%input%}=={get} (goto :get)
:get
set %input%==%getfile%
echo %getfile% >>Files to get.txt
call getfiles.bat
goto: inputstage
I input the following when it asks me to enter a command: "get index.html" and it then will take index.html and save it to a file called "files to get.txt" then it'll call up a piece of code that will run the command ftp -s:ftpreceive.bat that ftp receive file will then go and read the "files to get.txt" and see index.html and place that into the following ftp code get index.html(or whatever file I specify)
Can I get only a part of the input variable to become the variable getfile?
you seem to try to separate words from input or separate into <command> and <whatever>. In both cases, for is the way to go:
#echo off
setlocal enabledelayedexpansion
set "input=get something special"
echo method 1 (get "first word" and "rest"):
for /f "tokens=1*" %%a in ('echo %input%') do (
set "command=%%a"
set "param=%%b"
)
echo %command% : %param%
echo/
echo method2 (get every word):
set i=0
for %%a in (%input%) do (
set "_var[!i!]=%%a"
set /a i+=1
)
set _var
I'm trying to make a reminder system within batch in which there are different lines of reminders. My batch program will write to different lines in a .txt file, but it isn't working. Could you please help and try to find the issues?
#echo off
echo Enter slot # for reminder
set /p n=
cls
echo Please type in the assignment name
set /p a=
echo ----------------------------------
echo Please type in the class
set /p c=
echo ----------------------------------
echo Please type in the date due
set /p d=
cls
if %n%==1 goto l1
if %n%==2 goto l2
if %n%==3 goto l3
if %n%==4 goto l4
if %n%==5 goto l5
if %n%==6 goto l6
:l1
echo Reminder for %c% Homework! %a%,%d% > Reminder.txt
:end
:l2
echo Reminder for %c% Homework! %a%,%d% >> Reminder.txt
:end
:l3
echo Reminder for %c% Homework! %a%,%d% >>> Reminder.txt
:end
:l4
echo Reminder for %c% Homework! %a%,%d% >>>> Reminder.txt
:end
:l5
echo Reminder for %c% Homework! %a%,%d% >>>>> Reminder.txt
:end
:l6
echo Reminder for %c% Homework! %a%,%d% >>>>>> Reminder.txt
:end
Hints to fix what you've got:
The > character won't let you write to specific lines, and there's no native support in Windows batch to do such a thing.
There are two operators that use the > character: >, which redirects output to a file (replacing any existing content), and >>, which appends (adds to the end of) a file.
You've got multiple instances of :end, but that's invalid. :end is a label, which is a unique reference to that point in the code. When you add more than one, some get ignored and you get undefined behaviors, which is bad.
It looks like you're trying to use :end to exit. Use goto :EOF for that. It jumps to the built-in label :EOF, short for End Of File.
You need to handle the case where n is none of the predefined values. Currently if someone entered 7 for n, your program would get to the logic after :l1 and run it, which is wrong. Put a goto :EOF there just in case.
How to approach solving this type of issue with batch:
The only way I can think of off the top of my head to modify a specific line is to iterate through all lines using a for /f loop, rewriting each line (to a temporary file) until you encounter the one you want to change, then write your new content instead of the existing content. Then when you're done iterating, you can replace the original file with that temporary file.
You would have to do this each time you wanted to change a new line. Batch is a really simple language that doesn't have useful constructs like arrays, or the many external tools that a shell scripting language like Bash would have. It's also got some really unsophisticated runtime evaluation.
Here's a partial solution that you can combine with a few lines from your code above to achieve what you want. It prompts you for a line number, then puts the content of the newContent variable (replace with your implementation) into the file at the specified line:
REM suppresses the echo of the commands in the program
#ECHO OFF
REM sets a feature that overcomes some of the weak runtime evaluation limitations that batch has
setlocal ENABLEDELAYEDEXPANSION
REM The name of your file
set fname=file.txt
REM If our file doesn't already exist, make a new one with 6 empty lines since that's all we want for now.
if EXIST "%fname%" goto alreadyExists
for /l %%b in (1,1,6) do echo.>>"%fname%"
:alreadyExists
REM The name of a temp file
set tfile=f2.txt
REM A counter to track the line number
set counter=0
REM Input to get the line number you wish to replace
set /p replacementLine=Type the line number that should be replaced:
REM The content that goes on the replaced line
set newContent=New entry
REM Read the file, iterate through all lines.
for /f "tokens=*" %%a in (file.txt) do (
REM Add one to the counter
set /a counter=!counter!+1
REM Use the redirect '>' operator for the first line
if "!counter!"=="1" (
if "!counter!"=="%replacementLine%" (
REM We're on the line we wish to replace, so use the replacement line content
echo.%newContent% >f2.txt
) else (
REM We're NOT on the line we wish to replace, so use the original line content
echo.%%a >f2.txt
)
) else (
REM else for lines other than the first, use the append redirect '>>'
if "!counter!"=="%replacementLine%" (
REM We're on the line we wish to replace, so use the replacement line content
echo.%newContent% >>f2.txt
) else (
REM We're NOT on the line we wish to replace, so use the original line content
echo.%%a >>f2.txt
)
)
)
REM Delete the original file
del "%fname%"
REM Replace it with the modified copy
ren "%tfile%" "%fname%"
You can replace a few lines at the top get the functionality you want.
you can't write to a specific line in a file with batch. Instead you have to rewrite the complete file.
Steps: a) read the file. b) change the desired line. c) (over)write the file with the new data.
#echo off
setlocal enabledelayedexpansion
if not exist reminder.txt call :InitFile :: create the file, if it doesn't exist
set /p "n=Enter slot # for reminder: "
set /p "a=type in the assignment name: "
set /p "c=type in the class: "
set /p "d=type in the date due: "
cls
call :ReadFile
set "_Line[%n%]=Reminder for %c% Homework: %a%,%d%"
call :WriteFile
type Reminder.txt
goto :eof
:ReadFile
set x=0
for /f "delims=" %%i in (reminder.txt) do (
set /a x+=1
set "_Line[!x!]=%%i"
)
goto :eof
:WriteFile
set x=0
(for /f "tokens=2 delims==" %%i in ('set _Line[') do echo %%i)>Reminder.txt
goto :eof
:InitFile
(for /l %%i in (1,1,6) do echo Reminder %%i [empty])>Reminder.txt
goto :eof
(Note: this would make trouble with more than 9 lines because of the alphabetical sorting with set _line[, but as you need only 6 lines, this should not be a problem for you)
Note: your input shouldn't contain !
I am reading two text files to set the values of two variables (u,l). Now I want to write script to run multiple files. When it is reading first file it will set the variables from the respective files but when it is reading second file it will set the same values of those variables.
#echo on
set /p u=< ul.txt
set /p l=< ll.txt
echo %u%-%l%
I tried SETLOCAL/ENDLOCAL option but in that case it is not reading variables values and getting error that ECHO is off. Even I wrote set u= and set l= at the initial of the script but not working in my case.
Your code, as given, works fine. However, I'm guessing it is code from inside an if statement, or for loop. If that is the case, you should use delayed expansion. You can use delayded expansion like this:
This is an example, not the exact code you need:
#echo on
setlocal EnableDelayedExpansion
if 1 equ 1 (
set /p "u=< ul.txt"
set /p "l=< ll.txt"
echo !u!-!l!
)
pause
FOR /L %%G IN (1,1,1) DO (
set /p "u=< ul.txt"
set /p "l=< ll.txt"
echo !u!-!l!
)
pause
set /p u=< ul.txt
set /p l=< ll.txt
echo %u%-%l%
pause
Note that inside the if statement and for loop, you replace % signs, when they are around variable names, with !. So %someVar% becomes !someVar!, but %%F stays %%F.
Outside of if statements and for loops, so outside of (), you use the normal %someVar%