Reputation: 2280
I am trying to loop through files in a folder and get all files greater than 100MB.
Now, if I do this, I can get the size of the files to output in bytes:
for %%f in (%FOLDER_PATH%\*) do (
set /p val=<%%f
echo %%~z%f
)
While I can echo that value, I cannot assign it to a variable or run a comparison with it:
for %%f in (%FOLDER_PATH%\*) do (
set /p val=<%%f
if %%~z%f GTR 104857600(
echo file meets criteria
)
)
The above code returns:
The syntax of the command is incorrect.
Upvotes: 1
Views: 1791
Reputation: 49086
Why is the first line of current file assigned with the command line set /p val=<%%f
to environment variable val
if the task is just to find and somehow process files in specified folder having a file size of more than 100 MiB?
The syntax error is in %%~z%f
as the loop variable is f
in your code and the modifier to reference the file size of the file assigned to this loop variable is ~z
. The correct reference of loop variable f
with modifier to get file size is %%~zf
.
Well, lower case f
is a bad choice for a loop variable as there is also ~f
which is the modifier to reference the entire name of the file (file name + extension) with full path. %%~f
could be interpreted by Windows command interpreter as referencing value of loop variable f
with surrounding double quotes removed or as incomplete loop variable reference with modifier ~f
for name of file with full path missing the loop variable.
Loop variables are in comparison to environment variables case-sensitive. Therefore it is in general better to use upper case letters as loop variables to avoid a confusion with modifiers which are all lower case.
This code shows how to reference the name of the file and its file size using loop variable I
.
@echo off
if not defined FOLDER_PATH set "FOLDER_PATH=%SystemRoot%"
for %%I in ("%FOLDER_PATH%\*") do (
echo File "%%~I" has a file size of %%~zI bytes.
if %%~zI GTR 104857600 echo File "%%~I" is greater 100 MiB.
)
set "FOLDER_PATH="
The file size can be assigned to an environment variable with set "FileSize=%%~zI"
.
But on assigning a value to an environment variable which is referenced in same command block starting with (
and ending with matching )
it is necessary to use delayed expansion because Windows command interpreter replaces all %variable%
environment variable references already by current value of variable
before running the FOR command. Therefore all %variable%
environment variable references do not update during loop execution.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
if not defined FOLDER_PATH set "FOLDER_PATH=%SystemRoot%"
for %%I in ("%FOLDER_PATH%\*") do (
set "FileSize=%%~zI"
if !FileSize! GTR 104857600 echo File "%%~I" is greater 100 MiB.
)
endlocal
The environment variable FileSize
is referenced with using exclamation marks instead of percent signs to make use of delayed expansion as enabled at top of the batch file because of by default only command extensions are enabled. The command extensions are also needed for GTR
operator of IF command.
But please note that both batch files posted above do not work for files with 2 GiB or more because Windows command interpreter supports only 32-bit signed integers. Additional code would be necessary to find out if a file is larger than 2 GiB first before running the IF command. Well, in this case this is quite easy to achieve as any file with a file size with more than 9 digits is definitely larger than 100 MiB.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
if not defined FOLDER_PATH set "FOLDER_PATH=%SystemRoot%"
for %%I in ("%FOLDER_PATH%\*") do (
echo File "%%~I" has a file size of %%~zI bytes.
set "FileSize=%%~zI"
if not "!FileSize:~9!" == "" (
echo File "%%~I" is greater 100 MiB.
) else if !FileSize! GTR 104857600 (
echo File "%%~I" is greater 100 MiB.
)
)
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
if /?
set /?
setlocal /?
Upvotes: 0
Reputation:
Except for the superfluous %
between z and f your code is valid.
Just getting the first line of content from file %%f and then not using this var puzzles me.
for %%f in (%FOLDER_PATH%\*) do (
set /p val=<%%f
Echo %%f:1:!val!
if %%~zf GTR 104857600(
echo file meets criteria
)
)
Upvotes: 2