Converting text/csv files to UTF-8 format in batch

2019-08-02 21:13发布

问题:

I am coding a batch file where WMIC command gives an output which is written to a temp file

WMIC /node:%%A Volume get systemname,caption,freespace,capacity > temp.txt

Temp file is generated but its encoding is UCS-2 Little Endian.

So, when I am trying to read the text file using for loop,

for /f "tokens=1" %%B in (temp.txt) do (
        echo %%B
    )

This doesnot displays any output because of its encoding format.

Once I convert this Temp.txt to UTF-8/ANSI format the for loop reads the Temp file.

Can I know how can I get the output of WMIC command in UTF-8/ANSI or if not how can I convert UCS-2 Little Endian to UTF-8/ANSI

Regards

回答1:

You can filter the output of the wmic to convert the file

WMIC /node:%%A Volume get systemname,caption,freespace,capacity | find /v "" > temp.txt

Or you can convert the file with the type command

WMIC ..... > temp1.txt
type temp1.txt > temp.txt

Or you can directly retrieve the information in the for loop

for /f "tokens=1-4 delims=," %%a in (
    'wmic /node:%%A Volume get systemname^,caption^,freespace^,capacity'
) do echo %%a %%b %%c %%d