Batch file tokens ignore empty delimiters

后端 未结 2 1788
情深已故
情深已故 2021-01-07 06:12

I am trying to load a column from a CSV file into a variable. My CSV file contains empty columns so can have ,, which seems to be ignored by the following:

2条回答
  •  情歌与酒
    2021-01-07 07:04

    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET "sourcedir=U:\sourcedir"
    SET "filename1=%sourcedir%\q36372429.txt"
    SET "oneline="
    
    FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
     SET "line=%%a"
     FOR /f "tokens=3delims=," %%h IN (" !line:,= , ! ") DO (
      SET "column=%%h"
      SET "oneline=!oneline! !column:~1,-1!"
      ECHO(!column:~1,-1!
     )
    )
    ECHO %oneline:~1%
    
    GOTO :EOF
    

    You would need to change the settings of sourcedir and filename1 to suit your circumstances.

    I used a file named q36372429.txt containing your data for my testing.

    Unfortunately, you haven't shown us a realistic data sample. I'm very suspicious of your use of date as the destination variable.

    date is a "magic variable" - maintained by the system as the current date. It can be overridden, but it's not advisable to do so.

    It's not really clear what you want out of this procedure. Perhaps you want the entries from column 3 all on line line as you state, or perhaps you want to list only column 3 or perhaps you'd want the output transferred to a new file.

提交回复
热议问题