Coding⏱️ 2 min read📅 2026-06-19

How to Fix: file names starting with a string in the format of *.txt give error in FOR

Windows batch script error fixing for file names starting with a string in the format of *.txt

Quick Answer: Use wildcards to match file names, e.g. `for /f %%C in ('Find /V /C "" ^< %SFTP_INDIR% emp*.txt') do set count=%%C`

The issue you are experiencing is frustrating because it prevents your batch scriptfrom running as expected. When you try to run the script with a file name starting with 'Location*.txt', it fails due to an error in the FOR loop.

This issue can be resolved by using a different approach to find files based on their names, rather than relying on the original method that is causing the problem.

🛑 Root Causes of the Error

  • The primary reason for this error is that the FIND command in batch scripts does not support regular expressions or globbing patterns. The ^* wildcard character in the file name is being interpreted as a literal asterisk, rather than a wildcard character.
  • Another possible cause could be the presence of spaces in the file name or the directory path, which can prevent the FIND command from working correctly.

🚀 How to Resolve This Issue

Using the FINDSTR Command with Wildcard Characters

  1. Step 1: To resolve this issue, you can use the FINDSTR command instead of the FIND command. The FINDSTR command supports wildcard characters and allows you to search for files based on their names.
  2. Step 2: Open a Command Prompt as an administrator and navigate to the directory where your batch script is located.
  3. Step 3: Use the following command to find all files starting with 'Location' and having '.txt' extension: `findstr /v /c:

Alternative Advanced Fix

    🎯 Final Words

    Did this fix your problem?

    If not, try searching for specific error codes.

    🔍 Search Error Database

    ❓ Frequently Asked Questions