简体   繁体   中英

Extract the first quoted string of each line of a text file

I want to read through a text file with a lot of lines. in the beginning of each line, i have a string between quotes, then a coma and then the rest of the lines, Ex.:

"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",
"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""

What I want is to read through the file, and extract the first line put it in a different text file and name it with the first strings of the line that it contains. Ex.: In the above example, I should have the text file CBL003.txt that contains:

"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",

and a second file text CBL004.txt that contains :

"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""

I already have a code that read through each line :

FOR /F "tokens=*" %%a IN (C:\SourceFile.txt) DO (
    ECHO %%a 
)

But I don't know how to extract the first part of each line

You need tokens=1 because you want %%a to be set to only the first token, and you need delims=, to specify the comma as the delimiter. You can remove the quotes, if you want, by using %%~a . Type for /? for help.

FOR /F "tokens=1 delims=," %%a IN (C:\SourceFile.txt) DO (
    ECHO %%~a
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM