Read a file by line by line and store some specific values in another text file through bat file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

Read a file by line by line and store some specific values in another text file through bat file

#1 Post by naraen87 » 12 Jan 2018 04:18

Say for Example

I'm having my sample.txt file as

Code: Select all

Environment=non-clusters
Server1=xx.xx.xx.xxx
Server2=xx.xx.xx.xxx
GDrvie_Build=\\xx.xx.xx.xxx\Users\vissas\Desktop\WIServerSetup\CAM8\CAM8_6.20.8.2
Build_Artifact_File=Hotfix_CAM8_6.20.8.2.7z
Newer_Version=UAT2_v6.18.13.1
Property_File_Name=MCSysProp.properties
Property_File_Location=C:\narayana\Enviromnet_Setup\WIServerSetup\UAT\MDrive\BaNCSFS\BancsProduct\Intranet\properties\InputFiles
Xml_File_Location=C:\narayana\Enviromnet_Setup\WIServerSetup\UAT\MDrive\BaNCSFS\BancsProduct\Extranet\Bancs.ear\Bancs.war\WEB-INF
Common_Path_For_Extraction=Users\Public
7zip_exe_Location="C:\Program Files\7-Zip\7z.exe"

From the above file I need only the data exists after = in a new file, my new created file needs to look like the below one

Code: Select all

non-clusters
xx.xx.xx.xxx
xx.xx.xx.xxx
\\xx.xx.xx.xxx\Users\vissas\Desktop\WIServerSetup\CAM8\CAM8_6.20.8.2
Hotfix_CAM8_6.20.8.2.7z
UAT2_v6.18.13.1
MCSysProp.properties
C:\narayana\Enviromnet_Setup\WIServerSetup\UAT\MDrive\BaNCSFS\BancsProduct\Intranet\properties\InputFiles
C:\narayana\Enviromnet_Setup\WIServerSetup\UAT\MDrive\BaNCSFS\BancsProduct\Extranet\Bancs.ear\Bancs.war\WEB-INF
Users\Public
"C:\Program Files\7-Zip\7z.exe"

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Read a file by line by line and store some specific values in another text file through bat file

#2 Post by Squashman » 12 Jan 2018 08:47

So you already know how to read a file line by line with the FOR /F command based on the advice you were given at StackOverFlow.

Now all you need to do is add to that code. The DELIMS option allows you to break up the line into multiple variables known as TOKENS to the FOR command. In your case you are asking for the second token that is delimited by an equals symbol.

Code: Select all

FOR /F "tokens=2 delims==" %%G IN (propertyfile.txt) do echo %%G >>newpropertyfile.txt

naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

Re: Read a file by line by line and store some specific values in another text file through bat file

#3 Post by naraen87 » 17 Jan 2018 04:44

Thanks for your reply @Squashman. It working perfectly awesome.

Post Reply