Page 1 of 1

asterisk not working in file name

Posted: 21 Mar 2013 16:20
by pditty8811
I am searching *.map files for string (findstr), the string is var1

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "delims=" %%A in (Marks.txt) do (
  set var1=!var1!%%A

  findstr /e "!var1!" "*.map"

  if not errorlevel 1 (
    for /f "tokens=2 delims==,." %%B in ('findstr /e "!var1!" *.map') do (
      set var2=!var2!%%B

      set /a linecount+=1
      if !linecount! GEQ %maxlines% GOTO part2
    )

:part2
    for /f "tokens=4 delims==,." %%C in ('findstr /e "!var1!" *.map') do (
      set var3=!var3!%%C
      if !linecount! GEQ %maxlines% GOTO exitloop
    )
)
)
  :exitloop



Re: asterisk not working in file name

Posted: 21 Mar 2013 16:54
by mfm4aa
var1 is empty?

Re: asterisk not working in file name

Posted: 21 Mar 2013 16:56
by pditty8811
nope
echoed it and it equals Mark 1

Re: asterisk not working in file name

Posted: 24 Mar 2013 10:35
by trebor68
The command FINDSTR will perfect work with more as one file.

Code: Select all

FINDSTR "example" file_1.txt
FINDSTR "example" file_1.txt file_2.txt file_3.txt
FINDSTR "example" file_*.txt"
FINDSTR /F:file_list.txt "example"

The first row will only use one file. The output of this command is the row with the word "example".
The other rows will work with more than one file. The output of this commands are:
file_1.txt:Row with the word "example".

If you not want the file name in front of the row please use this:

Code: Select all

for %%a in (file_*.txt) do (
  echo %%a
  findstr "example" "%%a"
  echo.
)

###

Example for FINDSTR:
FINDSTR /E "word_1 word_2" file.txt

FINDSTR will only rows with "word_1" or "word_2" at the end of the row. If the row have a space at the end of the row after the word "word_1" then will not find this row.
You can test this with a file like this:

Code: Select all

Row with a space at the end.<space>
Row without a space at the end.
Test this one with: FINDSTR /E "end." file.txt

###

I'm not sure what you will search?

If the file "marks.txt" have this values:

Code: Select all

Anton
Berta
Charly One
Dora

And your code is:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set var1=
for /f "delims=" %%a in(marks.txt) do (
  echo %%a
  set var1=!var1!%%a
  echo !var1!
  echo.
)
Then is the result:

Code: Select all

Anton
Anton

Berta
AntonBerta

Charly One
AntonBertaCharly One

Dora
AntonBertaCharly OneDora

Re: asterisk not working in file name

Posted: 25 Mar 2013 02:40
by trebor68
Now I have more time to write here.

If the variable set before the command SETLOCAL in the batch then:

Code: Select all

@echo off
set var1=Trebor68
setlocal enabledelayedexpansion
for /f "delims=" %%a in (test34.txt) do (
  echo %%a
  set var1=!var1!%%a
  echo !var1!
  echo.
)
endlocal
echo %var1%

Here the output:

Code: Select all

Anton
Trebor68Anton

Berta
Trebor68AntonBerta

Charly One
Trebor68AntonBertaCharly One

Dora
Trebor68AntonBertaCharly OneDora

Trebor68


And now combinated with the command FINDSTR.
For the search I use this file Test34a.txt:

Code: Select all

Anton
Smith, Anton
Berta
Berta Doe
Charly
One Two
Two Charly
Charly One
Iris One
Dora


Here the batch Test34a.bat:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set var1=
for /f "delims=" %%a in (test34.txt) do (
  echo.
  set var1=%%a
  echo Search string: !var1!
  echo.
  findstr /e "!var1!" Test34a.txt
)
echo.
echo ### End of search. ###

Here the output of the batch:

Code: Select all

Search string: Anton

Anton
Smith, Anton

Search string: Berta

Berta

Search string: Charly One

Charly
Two Charly
Charly One
Iris One

Search string: Dora

Dora

### End of search. ###


If you want to search only "Charly One" the use the command: FINDSTR /E /C:"Charly One" Test34a.txt
The other strings with "Charly" or "One" at the end of the row will ignored.