Search code examples
batch-filecmdwmic

Can't read text file content generated by `wmic logicaldisk get name > D:\sample.txt` using batch script


I want to read the text file generated by wmic logicaldisk get name > D:\sample.txt using batch script. The problem is that it doesn't print the content on the screen. But when I try to copy its content and paste on a new txt file, it displays the content. Here is my batch script code.

@echo off 
FOR /F "usebackq tokens=1,2* delims=" %%G IN ("D:\sample.txt") DO echo %%G
pause

Solution

  • Instead of piping to file, why not do the output from command directly though?

    @for /F "skip=1" %%i in ('wmic logicaldisk get name') do @echo %%i
    

    Or to have it in a row instead of list:

    @echo off
    for /F "skip=1" %%i in ('wmic logicaldisk get deviceid') do <nul Set /P "=%%i"