Search code examples
batch-filebatch-rename

Rename CSV file with value from 1st column with batch


I would like to rename my CSV file with the value from the 1st column. How can I do that?

Example CSV:

123ABCQ;TEST1;TEST2;.....;;;;;

previous file name:

output.csv

new file name:

123ABCQ.csv

Solution

  • With tokens=* you tell for to take the whole line (in short terms), but you want the first element only. So instead of tokens=*, use "tokens=1 delims=;". You can omit tokens=1, as it's the default anyway. You then have to prevent it to (try to) rename the file again with the first token of each other line. & goto :cont breaks the loop, so it runs just for the first line.

    for /f "delims=;" %%a in (output.csv) do ren "output.csv" "%%~a.csv" & goto :cont
    :cont