2
echo off
set "MasterFolder=C:\Users\Development\test_folder"
SetLocal EnableDelayedexpansion
@FOR /f "delims=" %%f IN ('dir /b /s "%MasterFolder%\*.txt"') DO (
    set /a "idx+=1"
    set "FileName[!idx!]=%%~nxf"
    set "FilePath[!idx!]=%%~dpFf"
)

for /L %%i in (1,1,%idx%) do (
    echo [%%i] "!FileName[%%i]!"

    set "loc=%MasterFolder%\!FileName[%%i]!"
    echo path: %MasterFolder%\!FileName[%%i]!
    echo loc: !loc!
    FOR /F "tokens=1,2 delims=:" %%1 in ('findstr /o "^" "%MasterFolder%\test_1.txt"') DO (
        set new_pos=%%1
        echo new_pos !new_pos!
    )
)

The above code works, However, the following codes do not work.

echo off
... 
FOR /F "tokens=1,2 delims=:" %%1 in ('findstr /o "^" "%MasterFolder%\!FileName[%%i]!"') DO (
        set new_pos=%%1
        echo new_pos !new_pos!
    )
)
echo off
...
    FOR /F "tokens=1,2 delims=:" %%1 in ('findstr /o "^" "!loc!"') DO (
        set new_pos=%%1
        echo new_pos !new_pos!
    )
)

All I did was replacing test_1.txt to !FileName[%%i]!, or use a new variable and replace all as "!loc!".

But the last two codes do not work, and I need this manner, since it get file names in the folder automatically by the first for loop.

I guess there is an error either in findstr, or variable expansion order, but it is hard for me to figure it out.

Joey Cho
  • 359
  • 1
  • 7
  • `, but it is hard for me to figure it out.` Perhaps a simple `echo` of your `findstr` command before the loop could help – jeb Mar 04 '20 at 13:35
  • @jeb Hi jeb, again! Yes, has self destruction for a while, but was a different issue in naming. I named the batch file, as 'findstr.bat' first time. Only get ``` FINDSTR: No search strings ``` error now from the last 2 codes. – Joey Cho Mar 04 '20 at 13:40
  • Defensive programming might use `"%__APPDIR__%\findstr.exe"`. – lit Mar 04 '20 at 13:44
  • You should examine your variables. `echo %%i` and `set Filename` – jeb Mar 04 '20 at 13:44
  • @jeb What do you mean with *echo*? Yes, I checked the whole command *findstr* command with echo, there is no problem, same as in the first code. – Joey Cho Mar 04 '20 at 13:44
  • @lit still have **FINDSTR: No search strings** error message. – Joey Cho Mar 04 '20 at 13:47
  • @jeb I checked *%%i* variable, there is no issue in there, I am not sure about *set Filename* suggestion. Do you mean reassign Filename variable? – Joey Cho Mar 04 '20 at 13:49

1 Answers1

2

It's obvious!

When using !file..., you introduce an exclamation mark into the line.

This triggers phase 5 (delayed epxansion phase) of the parser.
In that phase the variables are expanded and carets are used to escape the next character, independent where they are (quoted or not)!

Therefore you your search string "^" is trasformed to "".

Simply double the caret to findstr /o "^^" !File...

The exclamation effect is a bit unexpected

setlocal EnableDelayedExpansion
echo One Caret "^"
echo One Caret "^" and a bang ^^!

Output

One Caret "^"
One Caret "" and a bang !

Community
  • 1
  • 1
jeb
  • 70,992
  • 15
  • 159
  • 202
  • Yes, wow, it was obvious, I had to more focus on the error message itself. Digging in a wrong part, or partially related, as you mentioned, **carets are used to escape the next character**. Thanks again! – Joey Cho Mar 04 '20 at 13:55
  • 1
    @JoeyCho Okay, to be fair. It's not so obvious, I had to test it, too, before I recognized the problem. – jeb Mar 04 '20 at 13:57