问题
I have an input CSV file, ttt.csv, which is comma delimited, each field may include double quote and comma:
Here is the contents of ttt.csv:
"CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA,aaa@email.com
"CN=Boo\\,Ryan,OU=Users,OU=Headquarters,DC=CORP",Boo,Ryan,"Boo,Ryan",BABBBB,bbb@email.com
I would need to loop this file, for each line, I would need to get each of the 6 values and create my SQL insert statement to database.
In my case for Line 2 I would need to get:
Value1= CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP
Value2= Boo
Value3= Ryan
Value4= Boo,Ryan
Value5= BABBBB
Value6= bbb@email.com
I used delimiter which includes double quotes and it does not seems working:
set str2="CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA,aaa@email.com
echo %str2%
for /f "tokens=1 delims=(,")" %%a in ("!str2!") do ( set newstr2=%%a )
echo !newstr2!
回答1:
As I commented above, just use a plain for
loop -- no /f
, no /r
, no /d
, no /l
, just a plain, simple for
loop. It'll handle CSV delimiters while treating quoted stuff as a single token.
@echo off
setlocal enabledelayedexpansion
set str2="CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA,aaa@email.com
echo %str2%
set idx=0
for %%a in (%str2%) do (
set "newstr[!idx!]=%%~a"
set /a idx += 1
)
set newstr
Output:
C:\Users\me\Desktop>test.bat "CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA, aaa@email.com
newstr[0]=CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP
newstr[1]=Bar
newstr[2]=Alex
newstr[3]=Barziza,Alex
newstr[4]=BARAAA
newstr[5]=aaa@email.com
If your csv data contains unquoted spaces that should not be treated as token delimiters, you can temporarily convert spaces to underscores before splitting, then convert back like this:
@echo off
setlocal enabledelayedexpansion
set str2="CN=Ryan\\,David Paul,OU=Users,OU=Singapore,DC=GLOBAL,DC=CORP",Ryan,David Paul,"Ryan, David Paul",RPAUL123,David@aaad.com
echo %str2%
set idx=0
for %%a in (%str2: =_%) do (
set "str=%%~a"
set "newstr[!idx!]=!str:_= !"
set /a idx += 1
)
set newstr
You can read more on substring substitution if you wish. Output:
C:\Users\me\Desktop>test.bat
"CN=Ryan\\,David Paul,OU=Users,OU=Singapore,DC=GLOBAL,DC=CORP",Ryan,David Paul,"Ryan, David Paul",RPAUL123,David@aaad.com
newstr[0]=CN=Ryan\\,David Paul,OU=Users,OU=Singapore,DC=GLOBAL,DC=CORP
newstr[1]=Ryan
newstr[2]=David Paul
newstr[3]=Ryan, David Paul
newstr[4]=RPAUL123
newstr[5]=David@aaad.com
Of course, if your data already contains underscores, then use a character it doesn't contain -- a backtick, a tilde, a dollar sign, or something else.
回答2:
@echo off
(
echo "CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA,aaa@email.com
echo "CN=Boo\,Ryan,OU=Users,OU=Headquarters,DC=CORP",Boo,Ryan,"Boo,Ryan",BABBBB,bbb@em
)>%tmp%\tmp.csv
for /f tokens^=^1*^ delims^=^" %%i in (%tmp%\tmp.csv) do (
echo value0= "%%i"
for /f tokens^=^1-6^ delims^=^=^,^" %%a in ("%%j") do (
echo value1= %%a&echo value2= %%b&echo value3= %%c,%%d
echo value4= %%e&echo value5= %%f&echo:
)
)
output:
value0= "CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP"
value1= Bar
value2= Alex
value3= Barziza,Alex
value4= BARAAA
value5= aaa@email.com
value0= "CN=Boo\,Ryan,OU=Users,OU=Headquarters,DC=CORP"
value1= Boo
value2= Ryan
value3= Boo,Ryan
value4= BABBBB
value5= bbb@em
来源:https://stackoverflow.com/questions/32961605/in-window-batch-how-do-i-parse-csv-file-where-fields-include-comma-and-double