- 1 year ago
Check file size in Windows batch script
Comment
1 / 1
To check the file size in a Windows batch script, you can use the FOR
command to loop through the files in a directory and use the %%~z
variable modifier to get the size of each file. Here's an example of how to do it:
batch@echo off
set "filename=myfile.txt" REM Replace with the name of your file
REM Check if the file exists
if not exist "%filename%" (
echo File "%filename%" does not exist.
exit /b 1
)
REM Get the file size using the FOR command
for %%F in ("%filename%") do set "filesize=%%~zF"
echo Size of file "%filename%": %filesize% bytes
Replace "myfile.txt"
with the path to the file you want to check. The script will output the size of the file in bytes.
Note that the FOR
command in batch scripts uses double percent signs (%%
) when used in a script. If you run the command directly in the command prompt, you should use a single percent sign (%
).