1

I am trying to loop through files in a directory, and execute actions based on the file size. Files are typically larger than 2 GB. I am using DisableDelayedException to handle exclamation marks in file names correctly. The relevant part of the bigger batch looks as follows:

  for %%x in (*.ts *.mpg *.mp4 *.wmv) do (
  If Not "%RamDisk%" == "" (
      Set "Filesize=%%~zx"
      Call :GetFilesizeMB
      If %FilesizeMB% lss 7000 (
          Echo Working from RAMDisk, copying %%x with %%~zx Bytes to RAMDisk... >>"%Logfile%"
          Copy "%%x" %RamDisk%\
          %RamDisk%
          Cd \
      ) Else (
          Echo %%x is too big - %%~zx Bytes -, not working from RAMDisk. >>"%Logfile%"
      )
  )

:GetFilesizeMB
Set "FilesizeMB=%Filesize:~,-6%"
Goto :EOF

Problem is: I cannot get the "if lss" part to work: If I work with if %%~zx directly, I get an error, because the file size too big for 32 bit. So I'm trying to approximate MBs by cutting off the last 6 figures. FilesizeMB is set correctly in :GetFilesizeMB, but after returning into the for loop, FilesizeMB is empty again.

How can I execute actions on a file depending on its file size in a for loop?

i3i5i7
  • 130
  • 1
  • 2
  • 8
  • 1
    why don't just use powershell? In batch you'll need to pad zeros and compare as string – phuclv Mar 19 '21 at 09:12
  • 2
    use `@Echo on` and you will see your issue. If you know enough to mention You have delayed expansion disabled, you should be aware that code blocks execute with `%` expanded varibles having the value [ including undefined ] they had prior to the commencement of the code block. – T3RR0R Mar 19 '21 at 09:19
  • 1
    Besides lack of delayed expansion, you are missing `goto :EOF` right before `GetFilesizeMB` to not unintentionally continue execution… – aschipfl Mar 19 '21 at 13:38

0 Answers0