Coding⏱️ 3 min read📅 2026-06-11

How to Fix: batch file put if inside for loop error

Batch file error fix for zip file size check.

Quick Answer: The issue is that the variable %FileSize% is not being updated correctly. Use the call command with the "%%i" instead of a hardcoded value.

The error '1000 was unexpected at this time' occurs when trying to combine a for loop with an if block in a batch file. This issue affects users who are attempting to automate tasks involving file processing, such as checking and printing the size of zip files.

This error can be frustrating because it prevents the batch file from executing correctly, causing unexpected behavior or errors. However, by following the steps outlined below, users can resolve this issue and achieve their desired outcome.

🔍 Why This Happens

  • The primary reason for this error is that the if block is executed before the variable %FileSize% is assigned a value. In the original code, the if block was outside the for loop, allowing it to access the file size correctly. However, when moved inside the for loop, the variable %FileSize% is not yet defined until after the if block has been executed.
  • An alternative reason for this error could be due to the use of the 'call' command in batch files. The 'call' command executes a subroutine (in this case, GetFileSize.bat) and passes arguments to it. However, when using the 'call' command, the variable %FileSize% is not automatically expanded until after the subroutine has completed execution.

🚀 How to Resolve This Issue

Modifying the if Block Placement

  1. Step 1: Move the if block outside the for loop, so that it checks the file size before printing the file name. This will ensure that the variable %FileSize% is defined and accessible within the if block.
  2. Step 2: Update the batch file to reflect the changed placement of the if block, as follows: for %%i in (*.zip) do ( echo %%i set FileSize=%%~z1 > %temp% mp.txt call GetFileSize.bat "%%i" if %FileSize% GTR 1000 ( echo %FileSize% is greater than 1000 ) else ( echo %FileSize% is NOT greater than 1000 ) )

Using Command Expansion to Resolve Variable Issues

  1. Step 1: Use command expansion (%%~z1) correctly in the if block, by expanding the variable %FileSize% within the parentheses. This will ensure that the variable is evaluated correctly and not cause unexpected behavior.
  2. Step 2: Update the batch file to reflect the corrected use of command expansion, as follows: for %%i in (*.zip) do ( echo %%i set FileSize=%%~z1 > %temp% mp.txt call GetFileSize.bat "%%i" if "%%FileSize%" GTR 1000 ( echo %%FileSize% is greater than 1000 ) else ( echo %%FileSize% is NOT greater than 1000 ) )

💡 Conclusion

By following these steps, users can resolve the '1000 was unexpected at this time' error and achieve their desired outcome. Remember to carefully review your batch file and make adjustments as necessary to ensure correct execution and avoid unexpected behavior.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions