Get size of a file and save it to a variable in Windows console?

17,361

There are a few suggestions here. I think this is best suited to you:

for %I in (test.jpg) do @echo %~zI
Share:
17,361

Related videos on Youtube

Hito_kun
Author by

Hito_kun

Updated on September 18, 2022

Comments

  • Hito_kun
    Hito_kun over 1 year

    I'm making a windows batch script, I need to get the specific size of a file so I can Insert that and other values I already got to a database, but I just can't figure out the proper way to do it.

    Assuming the name of the file is c:\test!basename!.zip (I'm in a for loop here), how can I get the filesize and store it to a variable?

    The machine is a Win2008 Server.

    ANSWER

    EliadTech proposed a solution that I've already tried

    for %I in (test.jpg) do @echo %~zI
    

    That worked directly in the command prompt, but failed inside the script.

    EliadTech, as we already suspected, thought of checking the escaping, so we got this

    for %%I in (c:\test\!basename!.zip) do @echo yay %%~zI
    

    Works like a charm.

    Kinda dumb mistake, but bash has spoiled me beyond repair :)

  • Hito_kun
    Hito_kun almost 10 years
    I've tried that already, but inside the script, but when doig for %I in (c:\test\!basename!.zip) do @echo yay %~zI I got The following usage of the path operator in batch-parameter substitution is invalid %~zI
  • EliadTech
    EliadTech almost 10 years
    I think it's related to the DelayedExpansion - try without it, or replace the "!" with regular "%" just here.
  • Hito_kun
    Hito_kun almost 10 years
    I suspect it is precisely that (or some weird escaping im not doing). tried both ways, none worked. I'll keep on looking.
  • EliadTech
    EliadTech almost 10 years
    Seriously? it worked OOB for me (though I've tried it straight in the CMD). Have you added additional "%" before every "%"? Try running this line separately.
  • Hito_kun
    Hito_kun almost 10 years
    Yeah, running it directly from the terminal works perfectly, but from the script it fails, lemme try what you say
  • Hito_kun
    Hito_kun almost 10 years
    You were absolutely right!! this is the line that works for %%I in (c:\test\!basename!.zip) do @echo yay %%~zI