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
Related videos on Youtube
Author by
Hito_kun
Updated on September 18, 2022Comments
-
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 almost 10 yearsI've tried that already, but inside the script, but when doig
for %I in (c:\test\!basename!.zip) do @echo yay %~zI
I gotThe following usage of the path operator in batch-parameter substitution is invalid %~zI
-
EliadTech almost 10 yearsI think it's related to the DelayedExpansion - try without it, or replace the "!" with regular "%" just here.
-
Hito_kun almost 10 yearsI suspect it is precisely that (or some weird escaping im not doing). tried both ways, none worked. I'll keep on looking.
-
EliadTech almost 10 yearsSeriously? 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 almost 10 yearsYeah, running it directly from the terminal works perfectly, but from the script it fails, lemme try what you say
-
Hito_kun almost 10 yearsYou were absolutely right!! this is the line that works
for %%I in (c:\test\!basename!.zip) do @echo yay %%~zI