How to read properties file from batch file - batch-file

I have a long test.properties file that contains the following at the very top:
property1=cheese
property2=apple
property3=bread
# many more properties
I can read those properties from the command line by changing the working directory to the one containing test.properties and running the following command:
> FOR /F "tokens=1,2 delims==" %A IN (test.properties) DO
IF "%A"=="property1" SET firstitem=%B
> FOR /F "tokens=1,2 delims==" %A IN (test.properties) DO
IF "%A"=="property2" SET seconditem=%B
> echo %firstitem%
cheese
> echo %seconditem%
apple
However, when I try to put this code in a batch file stored in the same directory, it fails and I cannot work out why:
FOR /F "tokens=1,2 delims==" %A IN ("%~dp0\test.properties") DO
(IF "%A"=="property1" SET firstitem=%B)
FOR /F "tokens=1,2 delims==" %A IN ("%~dp0\test.properties") DO
(IF "%A"=="property2" SET seconditem=%B)
Running the batch file from the command line gives me this:
> "C:\folder\testbatch.bat"
~dp0\test.properties") DO IF "B was unexpected at this time.
What can I do to read the properties using the batch file, so that they are stored in variables that can be used in the rest of the script?
EDIT: Problem solved; working code below.
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%~dp0\test.properties") DO
(IF "%%A"=="property1" SET firstitem=%%B)
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%~dp0\test.properties") DO
(IF "%%A"=="property2" SET seconditem=%%B)

There's two problems here:
In the batch file version you need two percent signs for the iterator variables.
In the batch file version you erroneously put quotes around the file name, causing the string itself to be tokenized, not the contents of the file specified by the string. Update: #Stephan correctly noted the use of the usebackq modifier for a more general solution. But since you're talking about "batch file stored in the same directory", you might as well drop the path prefix %~dp0 altogether.
Corrected version:
#ECHO OFF
FOR /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
IF "%%A"=="property1" SET firstitem=%%B
)
FOR /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
IF "%%A"=="property2" SET seconditem=%%B
)
ECHO %firstitem%
ECHO %seconditem%
Returns:
cheese
apple

"What can I do to read the properties using the batch file, so that
they are stored in variables that can be used in the rest of the
script?"
That is surprisingly easy in your case if you are willing to use the strings from the file:
FOR /F "usebackq tokens=*" %%A IN ("%~dp0\test.properties") DO set %%A
echo property2 is %property2%
Note: in case, your path or filename contains spaces, you need the quotes. Tell for not to treat it as a string with usebackq

Related

Batch: read file from path

I want to read a .txt file using batch script.
Each line should be stored in a variable.
My problem: I have to give the command a file path to the .txt file. Unfortunately this hasn't worked so far. The solution is probably very simple, but I haven't found it yet.
for /f "tokens=*" %%a in ("%FilePath%backup\packs.txt") do (
Set /a count+=1
Set url[!count!]=%%a
)
echo %url[2]%
for /f loops can process three different types of data - files, strings, and commands. Each of these is indicated differently when you call the for command.
A file is processed by not using quotes in the set area: for /f %%A in (file.txt) do (
A command is processed by using single quotes: for /f %%A in ('find "abc" file.txt') do (
A string is processed by using double quotes: for /f %%A in ("hello world") do (
Of course, sometimes you need to process a file with a space in the path, and that's when you'd use the usebackq option. This option will still process all three types of data, but the indicators will be different.
A file is processed by using double quotes: for /f "usebackq" %%A in ("some file.txt") do (
A command is processed by using backticks: for /f "usebackq" %%A in (`find "don't" file.txt`) do (
A string is processed by using single quotes: for /f "usebackq" %%A in ('19^" screen') do (
Either removing the quotes from your file path or adding the usebackq option will set the variables for you.
#echo off
setlocal enabledelayedexpansion
set "FilePath=.\test_path\"
set "count=0"
for /f "usebackq tokens=*" %%A in ("%FilePath%backup\packs.txt") do (
set /a count+=1
set "url[!count!]=%%A"
)
echo %url[2]%

How to trim string in command prompt?

I have a lot of shortcut urls in a directory.
C:\Users\Owner\Desktop\ReadItLaters>dir *.url /b
aaa.url
bbb.url
ccc.url
...
zzz.url
I want to pickup those url.
so I wrote command like this.
for %i in (*.url) do #type "%i" | find "URL="
that outputs like this.
URL=https://www.example.com/aaa.html
URL=https://www.example.com/bbb.html
URL=https://www.example.com/ccc.html
...
URL=https://www.example.com/zzz.html
It tastes nice. but I want to get url strings WITHOUT "URL=".
I wish to output like this.
https://www.example.com/aaa.html
https://www.example.com/bbb.html
https://www.example.com/ccc.html
...
https://www.example.com/zzz.html
How can I replace "URL=" to empty?
You can use substring, skipping the first 4 characters in your string: %_url:~4%
cmd/bat
for /f %%i in ('type *.url^|find "URL="')do set "_url=%%~i" && call echo/%_url:~4%
command-line
for /f %i in ('type *.url^|find "URL="')do set "_url=%~i" && call echo/%_url:~4%
Some further reading:
[√] Set
[√] For Loop
[√] For /F Loop
[√] Conditional Execution || && ...
[√] Substring in Set command (Refer: %_url:~4%)
use a for /f loop to process the lines:
for /f "tokens=2 delims==" %%a in ('type *.url 2^>nul^|find "URL="') do #echo %%a
or a bit saver (in case the URLs contain =)
for /f "tokens=1,* delims==" %%a in ('type *.url 2^>nul^|findstr /b "URL="') do #echo %%b
See for /? for details.
(Note: this is batch file syntax. If you want to use it directly on the command line, replace each %% with a single %)
You should be able to get the information for multiple URL files, without the need to nest two for loops. You can take advantage of FindStr's ability to search through multiple files directly.
batch-file:
#For /F "Tokens=1*Delims==" %%G In ('""%__AppDir__%findstr.exe" /IR "^URL=http" "*.url" 2>NUL"')Do #Echo %%H
cmd:
For /F "Tokens=1*Delims==" %G In ('""%__AppDir__%findstr.exe" /IR "^URL=http" "*.url" 2>NUL"')Do #Echo %H

Extract a variable value from a text

it's quite complicated....using a batch script on windows platform, I need to extract a variable string from an output file, editing the final result towards another file.
Example, the file text.log comes to me filled in this way:
C:Windows\xfile\CODSSD1.XXX
C:Windows\xfile\RPDDFSS.XXX
C:Windows\xfile\XCCLDJW.XXX
I need to catch only the last word in all rows, editing them in this manner:
XXXCODSSD1
XXXRPDDFSS
XXX......
I've tried different commands but I'm not an expert.
Thank you very much!
Kind regards.
Joe
Given your output file named text.log, I would suggest something similar to the following:
#Echo Off
For /F "UseBackQ Delims=" %%A In ("text.log"
) Do For /F "Tokens=* Delims=." %%B In ("%%~xA%%~nA") Do Echo %%B
Pause
I have split the For loop over two lines only to maintain line length within 80 characters. Whilst it isn't a requirement, It's a best practice preference.
Edit
To test it at the command prompt, you can use this single line:
For /F "UseBackQ Delims=" %A In ("text.log") Do #For /F "Tokens=* Delims=." %B In ("%~xA%~nA") Do #Echo %B
This is a pretty simple one line FOR command using the /F option to read the file. You can use the FOR command modifiers to break up the file name into its individual parts. The x modifier is for the file extension. The n modifier is for the base file name.
#echo off
setlocal enabledelayedexpansion
FOR /F "usebackq delims=" %%G IN ("text.log") DO (
set "var=%%~xG%%~nG"
echo !var:~1!
)
pause

how to compare two files on names and remove the common name and corresponding value from first file

I have two .properties files as follows
first.properties second.properties
----------------- ---------------------
firstname=firstvalue fourthname=100100
secondname=secondvalue sixthname=200200
thirdname=thirdvalue nineththname=ninethvalue
fourthname=fourthvalue tenthname=tenthvalue
fifthname=fifthvalue
sixthname=sixthvalue
seventhname=seventhvalue
i want to compare two files with the names and need to remove common name and corresponding value from first.properties. The output file should be as
third.properties.
------------------
firstname=firstvalue
secondname=secondvalue
thirdname=thirdvalue
fifthname=fifthvalue
seventhname=seventhvalue
i used the following code,but it is giving Cartesian product scenario.can you please help me out to achieve the above.
for /F "tokens=1,2 delims==" %%E in (first.properties) do (
for /F "tokens=1,2 delims==" %%G in (second.properties) do (
if "%%E" NEQ "%%G" echo %%E=%%F>>!HOME!\Properties\third.properties
)
)
try this:
#echo off
(for /f "delims==" %%i in (second.properties) do echo(%%i.*)>temp.properties
findstr /rvg:temp.properties first.properties>third.properties
del temp.properties
..output in third.properties is:
firstname=firstvalue
secondname=secondvalue
thirdname=thirdvalue
fifthname=fifthvalue
seventhname=seventhvalue
at the request of the OP I add a (much slower) solution without a temp file:
#echo off
(for /f "tokens=1,2delims==" %%i in (first.properties) do findstr /r "%%i.*" second.properties >nul||echo(%%i=%%j)>third.properties
type third.properties
The Batch file below does not create temporary files nor use external commands (like findstr.exe), so it run faster.
#echo off
setlocal EnableDelayedExpansion
rem Create list of names in second file
set "secondNames=/"
for /F "delims==" %%a in (second.properties) do set "secondNames=!secondNames!%%a/"
rem Copy properties of first file that does not appear in second one
(for /F "tokens=1* delims==" %%a in (first.properties) do (
if "!secondNames:/%%a/=!" equ "%secondNames%" (
echo %%a=%%b
)
)) > third.properties
I used a slash to delimit property names. If this character may appear in the names, just select a different one in the program.
Previous solution eliminate any exclamation mark from the files; this detail may be fixed, if needed.

How to read the variables from a file and set them in batch file?

I have a text file which has two variables CVERSION=1.0.0.0 and PVERSION=1.0. I want the bach script to read these two variables to variables in batch file say, DVERSION and PVERSION respectively. How can I do that?
You could use findstr in a combination with a FOR /F loop.
FOR /F "usebackq tokens=2 delims==" %%V in (`FINDSTR CVERSION myFile.txt`) DO (
set "myCVersion=%%V"
)
echo %myCVersion%

Resources