How to read data file? - batch-file

If I have data file.txt and I want to read data from it into variable by using the command : set
How can I do that?
How can I move one line to another?

Read two lines from a file, based on your comments:
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (file.txt) do (
set /a N+=1
set v!N!=%%a
)
set sn=!v1!
set s=!v2!
You may want to consider using a scripting language, if you want to process multiple lines.

This will give you line one in the variable:
#echo off
set /p var=<file.txt
echo "%var%"
pause

Related

Batch file variables confused with string

I am currently trying to store a certain line of a text file in a batch file with this code.
for /f "tokens=* delims= " %%a in (files.txt) do (
set /a N+=1
set v[!N!]=%%a
)
set /p id="Please choose a number(1-10):"
set number=%v[id]%
echo %number%
endlocal
However instead of printing out the line it prints out
id
Could someone help with this?
Like what Aacini said I changed the line
set number=%v[id]%
by this one
set number=!v[%id%]!
and it worked

Batch file replace line in text file with new line

I want to be able to replace a line in a properties file but i only know part of the line string at any one time
Heres the line i want to replace: mb.datasource.password=ENC(8dF45fdD)
with this: mb.datasource.password=apassword
What i have just now is this
#echo off &setlocal
set "search=mb.datasource.password="
set "replace=mb.datasource.password=apassword"
set "textfile=mb.properties"
set "newfile=mb-new.properties"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
This ends up giving me mb.datasource.password=apassword=ENC(8fFdeUdK)
I can't just find the full string it needs to only be mb.datasource.password= because the part after the equals changes
Any help would be greatly appreciated?
You can do it with batch. I put together a quick script and it worked for me:
#ECHO OFF
SETLOCAL EnableExtensions
SET SourceFile="mb.properties"
SET OutputFile="mb-new.properties"
SET "FindKey=mb.datasource.password"
SET "NewValue=apassword"
REM Basic parse for INI file format.
(FOR /F "usebackq eol= tokens=1,* delims==" %%A IN (`TYPE %SourceFile%`) DO (
REM If the key value matches, build the line with the new value.
REM Otherwise write out the existing value.
IF /I "%%A"=="%FindKey%" (
ECHO %%A=%NewValue%
) ELSE (
ECHO %%A=%%B
)
)) > %OutputFile%
REM Replace old with new.
DEL %SourceFile%
MOVE %OutputFile% %SourceFile%
ENDLOCAL
A few notes about the above:
I assumed basic INI file format.
The FOR loop will skip blank lines so they would be removed from the new output file. There are ways around this using tricks with the FIND command if the blanks are needed.
If you have special chars (% or ! especially) - this may cause some problems, but if you have just "normal" text then it should be fine.

Batch for loop cannot set variables

The task is to iterate through each line in a file named alts.txt. Then I grab the line and split it at the semicolon and print out the text before the semicolon and after the semicolon.
My file looks something like this...
username:password
username2:password2
username3:
My current code is this:
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=*" %%A in (%file%) do (
set str=%%A
set "username=%str::="^&REM #%
set "pass=%str:*:=%"
echo username=%username% pass=%pass%
)
pause
If someone would be kind enough to show me my error and EXACTLY how to fix the error it would be greatly appreciated.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=1,2 delims=:" %%A in (%file%) do (
set "$user=%%A"
set "$pass=%%B"
echo username=!$user! pass=!$pass!
)
pause
Be careful using %username%. It's a system variable. You can test writing echo %username% in the CMD prompt. You better choose another name for the Variable like i did.

Windows Batch: Loop through to echo a set of variables

I have a batch file that contains the code below which assigns a variable for each line in a text file.
So the text file might have:
RELEASE1
RELEASE2
RELEASE3
The batch file sets each line in the text file to var1, var2, var3 with the following code:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (sites.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
)
I need a method to echo all the defined variables. The number of variables will always change. So it might go up to var8 or var10 etc...
I'm pretty certain a for loop would do the trick but not sure what the best approach or how to do it? I was thinking of using vidx as the number of iterations? Thanks for your help!
Very Easy
#echo off
setlocal ENABLEDELAYEDEXPANSION
set count=0
for /F "tokens=*" %%A in (sites.txt) do (
SET /A count+= 1
set var!count!=%%A
)
Rem //:Notice That %count% still stores the last variable, use this in a "for /l" loop:
for /l %%a in (1,1,%count%) do (
Rem Below line is optional and displays variable number
<nul set /p="Variable %%a: "
Echo !var%%a!
)
That should work fine as long as none of the data contains parenthesis, in which case you'll have to escape them.
Type for /? for help on this. Ask If you want an explanation.
set var|findstr /R "var[0-9]*="
May be the shortest way....

Batch files: How to read a file?

How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode?
Under NT-style cmd.exe, you can loop through the lines of a text file with
FOR /F %%i IN (file.txt) DO #echo %%i
Type "help for" on the command prompt for more information. (don't know if that works in whatever "DOS" you are using)
The FOR-LOOP generally works, but there are some issues.
The FOR doesn't accept empty lines and lines with more than ~8190 are problematic.
The expansion works only reliable, if the delayed expansion is disabled.
Detection of CR/LF versus single LF seems also a little bit complicated.
Also NUL characters are problematic, as a FOR-Loop immediatly cancels the reading.
Direct binary reading seems therefore nearly impossible.
The problem with empty lines can be solved with a trick. Prefix each line with a line number, using the findstr command, and after reading, remove the prefix.
#echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!"
echo(!var!
ENDLOCAL
)
Toggling between enable and disabled delayed expansion is neccessary for the safe working with strings, like ! or ^^^xy!z.
That's because the line set "var=%%a" is only safe with DisabledDelayedExpansion, else exclamation marks are removed and the carets are used as (secondary) escape characters and they are removed too.
But using the variable var is only safe with EnabledDelayedExpansion, as even a call %%var%% will fail with content like "&"&.
EDIT: Added set/p variant
There is a second way of reading a file with set /p, the only disadvantages are that it is limited to ~1024 characters per line and it removes control characters at the line end.
But the advantage is, you didn't need the delayed toggling and it's easier to store values in variables
#echo off
setlocal EnableDelayedExpansion
set "file=%~1"
for /f "delims=" %%n in ('find /c /v "" %file%') do set "len=%%n"
set "len=!len:*: =!"
<%file% (
for /l %%l in (1 1 !len!) do (
set "line="
set /p "line="
echo(!line!
)
)
For reading it "binary" into a hex-representation
You could look at SO: converting a binary file to HEX representation using batch file
You can use the for command:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do #echo %i %j %k
Type
for /?
at the command prompt. Also, you can parse ini files!
One very easy way to do it is use the following command:
set /p mytextfile=< %pathtotextfile%\textfile.txt
echo %mytextfile%
This will only display the first line of text in a text file. The other way you can do it is use the following command:
type %pathtotextfile%\textfile.txt
This will put all the data in the text file on the screen. Hope this helps!
settings.ini
name="John"
lastName="Doe"
script.bat
#echo off
for /f "tokens=1,2 delims==" %%a in (settings.ini) do (
if %%a==name set %%a=%%b
if %%a==lastName set %%a=%%b
)
echo %name% %lastName%
Well theres a lot of different ways but if you only want to DISPLAY the text and not STORE it anywhere then you just use: findstr /v "randomtextthatnoonewilluse" filename.txt
Corrected code :
setlocal enabledelayedexpansion
for /f "usebackq eol= tokens=* delims= " %%a in (`findstr /n ^^^^ "name with spaces.txt"`) do (
set line=%%a
set "line=!line:*:=!"
echo(!line!
)
endlocal
pause

Resources