When I try to run this command.

This command finds a line of code that contains 213 and put it into a variable.

for /f "delims=" %a in ('findstr /R /I 213 N:\****\*****\DateMod_SBDM.txt') do @set sbdmcurdate=%a

In a .bat file it says.

\****\*****\DateMod_SBDM.txt') was unexpected at this time.

However it works when entered into the command line manually.
Maybe its a problem with the syntax but I'm pretty sure its correct.
Please help.

Dani AI

Generated

As found, this was the classic command-line vs. batch-file parsing difference: an interactive CMD FOR variable uses a single percent sign, while the same FOR inside a .bat must use a doubled percent. The @ prefix only suppresses command echoing and is not required to make the FOR work; the real parser issue comes from the percent-sign syntax used in a script file.

A few practical notes to make the snippet more robust and easier to debug:

  • Use the set "name=value" form when assigning to avoid accidental trailing spaces or embedded quote problems.
  • Enable delayed expansion when a variable set inside a FOR block needs to be read later in the same parenthesized block; that uses EnableDelayedExpansion and !var! for runtime expansion.
  • The error message "was unexpected at this time" often indicates a parsing problem (mismatched parentheses, stray special characters, or incorrect FOR syntax), so echoing the command or running the inner command directly at an interactive prompt helps isolate whether the problem is parsing or the external tool output.

Example pattern (different variable names shown here to avoid repeating the original verbatim):

@echo off
setlocal EnableDelayedExpansion

for /f "tokens=* delims=" %%G in ('findstr /I "213" "N:\path\DateMod_SBDM.txt"') do (
  set "sbdm_line=%%G"
)

echo Result: !sbdm_line!
endlocal

Extra caution: mapped drives (like N:) may not exist in a different service/user context (scheduled tasks); use a UNC path (\server\share...) or ensure the mapping exists for the account running the script.

Just figured it out.
Needed a double % sign for the variables. And no @ sign infront of set
So the new code would be.

for /f "delims=" %%a in ('findstr /R /I 213 N:\****\*****\DateMod_SBDM.txt') do set sbdmcurdate=%%a
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.