Windows – Remove quotes in Forfiles output {cmd}

cmd.execommand linewindows

So I am running this command:

FORFILES /S /M *.dmg /C "cmd /c if @fsize equ 1595694080 echo @fname"

and the output:

"058-26174-078"

but the problem is that I want the name without the quotations

like this: 058-26174-078

Best Answer

  • How do I remove the quotes from a variable in a cmd shell?

    You need to use ~ parameter extension together with for /f to do this.

    Use the following command:

    for /f %i in ('FORFILES /S /M *.dmg /C "cmd /c if @fsize equ 1595694080 echo @fname"') do @echo %~i
    

    To set a variable, and in a batch file, use the following command:

    for /f %%i in ('FORFILES /S /M *.dmg /C "cmd /c if @fsize equ 1595694080 echo @fname"') do @set MyVariable=%%~i
    

    Notes:

    • %~1 Expand %1 removing any surrounding quotes (")
    • In a batch file replace %i with %%i and %~i with %%~i

    Further Reading

  • Related Question