Batch Treat special characters as input errors - windows

I want all non-numeric characters to be considered as input error. Alphabetic characters are well considered but not certain special characters like colons (and others ones).
Any idea how to achieve this ?
#echo off
setlocal EnableDelayedExpansion
set /a UserProfileNo=0
FOR /F "delims=" %%I IN ('dir /a:d /b "%SystemDrive%\Users\"') do (
set "UserProfileName=%%~I"
set /a UserProfileNo+=1
set "UserProfileName[!UserProfileNo!]=!UserProfileName!"
)
FOR /L %%k IN (1, 1, %UserProfileNo%) DO echo %%~k !UserProfileName[%%~k]!
set /p choice=Enter a Profile Number:
FOR %%f IN (%choice%) DO if "!UserProfileName[%%~f]!" == "" (
echo Error in the input
goto end
)
echo NO Error in the input
:end
pause
exit
Thank you

I'd offer an expansion of the code I've provided for you before, for performing this task.
The following code will list all standard local users and allow you to choose one from the list, by its numerical index. Only valid index numbers, (no other normal characters), will be accepted, including !, ", %, ^, &, ), =, |, <, and >
Using this method, it does not limit output only to user profiles located in the hardcoded path %SystemDrive%\Users, nor does it depend upon the directory name for the profile matching the account holder's name, (if a user changes their account name, their profile directory is not renamed to reflect that change).
#Echo Off&SetLocal EnableExtensions DisableDelayedExpansion
Set "i=0"&For /F "Delims==" %%G In ('"(Set User[) 2>NUL"')Do Set "%%G="
For /F "Delims==" %%G In ('%__AppDir__%wbem\WMIC.exe OS Call /?
^| %__AppDir__%find.exe "=="') Do Set "#=%%G"
For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where
"LocalAccount='TRUE'" Assoc:List /ResultRole:SID 2^>NUL'
)Do For /F Tokens^=1* %%H In ('%__AppDir__%wbem\WMIC.exe UserAccount Where
"Name='%%G'" Get SID /Value 2^>NUL^|%__AppDir__%find.exe "="'
)Do For %%I In (%%H)Do For /F "Tokens=1*Delims==" %%J In (
'%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where
"SID='%%I' And Special!='TRUE' And LocalPath Is Not Null" Get LocalPath /Value
2^>NUL^|%__AppDir__%find.exe "="')Do For /F "Tokens=*" %%L In ("%%K")Do (
Set /A i+=1&SetLocal EnableDelayedExpansion&Echo !i!.%#:~-1%%%G
For %%M In (!i!)Do EndLocal&Set "User[%%M]Name=%%G"&Set "User[%%M]Path=%%L")
If Not Defined User[1]Name (Echo No standard users found, Press a key to end.
GoTo :EOF)
:Pick
Echo(&Set "#="
Set /P "#=Enter the number for your chosen Account Name>"
Set "#=%#:"=%"
(Set User[)|%__AppDir__%findstr.exe /BL "User[%#%]Name=">NUL||GoTo Pick
SetLocal EnableDelayedExpansion&For /F UseBackTokens^=1^,3Delims^=^" %%G In (
'"!User[%#%]Name!" "!User[%#%]Path!"')Do (EndLocal&Set "User[Name]=%%G"
Set "User[Profile]=%%H"&Set "#=")
(For /L %%G In (1,1,%i%)Do For %%H In (Name Path)Do Set User[%%G]%%H=)&Set "i="
Echo(&Echo You Selected %User[Name]% - [profile location is %User[Profile]%]
%__AppDir__%timeout.exe /T 5 /NoBreak>NUL
The last two lines are for demonstration purposes only, you would replace those with your own code, only after testing. Their inclusion was to demonstrate that a valid input entry will create two new local variables, %User[Name]%, (the %UserName% for the input index number), and %User[Profile]%, (the %UserProfile% for the input index number).
Please note: The code above should only be run and tested exactly as it exists above, with no omissions, additions, or modifications. If it fails to work as intended, I will accept changing all instances of %__AppDir__% with %SystemRoot%\System32\, before asking for support.

You can use findstr regex to restrict characters:
#Echo off
call :input rv[1] 0123456789 "enter a number"
call :input rv[2] a-zA-Z "enter a alphanumerical string"
call :input rv[3] "a-zA-Z " "enter an alpha sentence"
set rv[
Goto :Eof
:input [ReturnVar] [CharacterString] [prompt]
Setlocal EnableExtensions EnableDelayedExpansion
:retry
Set "input="
Set /P "input=%~3: "
If "!input!" == "" Goto :retry
Echo("!input!"| %__APPDIR__%findstr.exe /RXC:"[\"][%~2]*[\"]" >nul || Goto :retry
Endlocal & Set "%~1=%input%"
Exit /B 0

Related

Need advise on the Batch Script with the output:

Need your expert advice on the below:
Here is the code:
Note: serial is set to value of 100 which is default and it is pulled from the another script where all the server details are stored.
setlocal enabledelayedexpansion
SET serverlist=
SET env=TBD
if /I "%2" EQU "D" (set env=dev&& set env_dir=dev)
if /I "%2" EQU "U" (set env=uat&& set env_dir=uat)
if /I "%2" EQU "P" (set env=prod&& set env_dir=prod)
echo Here are the server details for %env% %1:
echo These are for DEV:
FOR /l %%a IN (1,1,50) DO (if defined %env%_%serial%_DEVser_%%a_ (for /f "tokens=2,3,4 delims==, " %%a in ('set %env%_%serial%_DEVser_%%a_') do (if %%c NEQ dup (echo %%a ^(%%b^,%%c^) & set serverdetails=!serverdetails! %%a %%b %%c) )))
echo These are for UAT:
FOR /l %%a IN (1,1,50) DO (if defined %env%_%serial%_UATser_%%a_ (for /f "tokens=2,3,4 delims==, " %%a in ('set %env%_%serial%_UATser_%%a_') do (if %%c NEQ dup (echo %%a ^(%%b^,%%c^) & set serverdetails=!serverdetails! %%a %%b %%c) )))
echo These are for PROD:
FOR /l %%a IN (1,1,50) DO (if defined %env%_%serial%_PRODser_%%a_ (for /f "tokens=2,3,4 delims==, " %%a in ('set %env%_%serial%_PRODser_%%a_') do (if %%c NEQ dup (echo %%a ^(%%b^,%%c^) & set serverdetails=!serverdetails! %%a %%b %%c) )))
echo details of all the server:
echo %serverdetails%
Current output:
These are for DEV:
D00123 (testing1)
D00456 (testing2)
D00789 (testing3)
These are for UAT:
UAT001 (UAT-env1)
UAT002 (UAT-env2)
UAT003 (UAT-env3)
These are for PROD:
PRD001 (PRD-env1)
PRD002 (PRD-env2)
PRD003 (PRD-env3)
details of all the server:
D00123 testing1 D00456 testing2 D00789 testing3 UAT001 UAT-env1 UAT002 UAT-env2 UAT003 UAT-env3 PRD001 PRD-env1 PRD002 PRD-env2 PRD003 PRD-env3
Question:
For the details of all the server output: I would like to get the output below
D00123
D00456
D00789
UAT001
UAT002
UAT003
PRD001
PRD002
PRD003
If the Dev server details are requested, then UAT / PROD details should not be visible
similarly, if PROD is requested, then DEV and UAT should not be visible.
can you please help me with this?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: Dummy data established outside of routine
SET "serial=100"
SET "ser_=xyz"
SET "dev_%serial%_dev%ser_%1_=D00123 something1,testing1"
SET "dev_%serial%_dev%ser_%16_=D00456 something2,testing2"
SET "dev_%serial%_dev%ser_%22_=D00789 something3,testing3"
SET "dev_%serial%_dev%ser_%35_=D00987 something4,dup"
SET "uat_%serial%_uat%ser_%1_=UAT123 something11,testing4"
SET "uat_%serial%_uat%ser_%16_=UAT456 something12,testing5"
SET "uat_%serial%_uat%ser_%22_=UAT789 something13,dup"
SET "uat_%serial%_uat%ser_%35_=UAT987 something14,testing7"
SET "prod_%serial%_prod%ser_%1_=PRD123 something21,dup"
SET "prod_%serial%_prod%ser_%16_=PRD456 something22,testing9"
SET "prod_%serial%_prod%ser_%22_=PRD789 something23,testing10"
SET "prod_%serial%_prod%ser_%35_=PRD987 something24,testing11"
SET |FIND /i "something"
IF /i "%1"=="d" SET "env=dev"
IF /i "%1"=="u" SET "env=uat"
IF /i "%1"=="p" SET "env=prod"
for %%e in (dev uat prod) do (
SET "report="
if /i "%%e"=="%env%" SET "report=Y"
IF DEFINED report echo These are for %env%:
FOR /l %%o IN (1,1,50) DO if defined %%e_%serial%_%%e%ser_%%%o_ (
for /f "tokens=2,3,4 delims==, " %%u in (
'set %%e_%serial%_%%e%ser_%%%o_'
) do if %%w NEQ dup set "%%eserverdetails=!%%eserverdetails! %%u"&IF DEFINED report echo %%u
)
SET "serverdetails=!serverdetails! !%%eserverdetails!"
)
echo These are for ALL:
FOR %%e IN (%serverdetails%) DO ECHO %%e
GOTO :EOF
Still not really clear on the values in the variables. Test setup shown. Routine accepts d, u or p as first param.
Don't know what ser_ is set to.
Your code contains for..%%a within a for..%%a - decidedly not good practice.
Speaking of which, a couple of proved "good practice" principles:
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.
Prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables) ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs (See for/f from the prompt for documentation)
So...
Having established env, process the variables for each of the three possibilities (in %%e). Filter the values for %%e _ %serial% _ %%e %ser_% %%o _ where %%o is 1..50 and append to %%eserverdetails if the third token of the variable is not dup. If %%e matches %env% then set the report flag which controls whether the data is echoed.
Finally, list the serverdetails data.
Of course, it would also be possible to list !%env%serverdetails! for the individual lists, which would make the report flag and reporting the data within the %%u loop redundant.
Append each %%eserverdetails to serverdetails after each %%e is processed

List all mp4 files with for loop and select one with choice

I was able to list mp4 files in a directory with a for loop, and depending on how many files are listed, a parameter is passed to the choice command. What I wasn't able to do is to select a file from a list that has been created with a for loop? This way I hope to use the selected file in another command as a parameter.
Here my code:
#echo off
setlocal enableextensions enabledelayedexpansion
set /a count = 0
echo.
for %%f in (*.mp4) do (
set /a count += 1
echo [!count!] %%f
)
for /l %%a in (1,1,%count%) do (
call set "cOpt=%%cOpt%%%%a"
)
echo.
choice /C %cOpt% /M "Select input video:"
echo %errorlevel% # selected option is shown here but no filename
endlocal
#Echo Off & SetLocal EnableExtensions
:Menu
For /F "Delims==" %%G In ('(Set File[^) 2^> NUL') Do Set "%%G="
For /F "Tokens=1,* Delims=[]" %%G In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe ".":"*.mp4" 2^> NUL ^| %__AppDir__%find.exe /N /V ""') Do Set "File[%%G]=%%~nxH" & Echo %%G. %%~nxH
If Not Defined File[1] GoTo :EOF
:Select
Set "#="
Set /P "#=Choose a file from the list by entering its item number. "
For /F "Tokens=1,* Delims==" %%G In ('(Set File[%#%]^) 2^> NUL ^| %__AppDir__%find.exe "="') Do Set "File[#]=%%H"
If Not Defined File[#] GoTo Select
:Main
Echo You chose item number %#% which was assigned to "%File[#]%" & Pause
You would obviously modify the last line to be your required command with parameter.
If you wish to include the full paths, simply change both instances of %%~nxH on line 5, to %%H. Additionally if you wish to stipulate a location, instead of the current directory, change ".", on the same line to e.g. "Y:\our\Path".

How to remove names from a list in batch

I'm using windows bath, I have a list of names that I can add to but I don't know how to remove a name from the list.
So far my code is:
#echo off
setlocal enabledelayedexpansion
set allchoices=123456789
set "names=Bob,Steven,Harry"
set amount=6 ::max limit of list
set list=0
:start
::echoes a list of all names in the list
for /l %%i in (1; 1; %amount%) do (
call :sub %%i
)
goto check
:sub
for /f "tokens=%1 delims=," %%a in ("%names%") do (
echo %%i. %%a
set /a list=list+1
)
goto :eof
:check
::Remove a name from the list
choice /c !allchoices:~0,%list%! /m "What name do you want to remove?"
if errorlevel 3 (
for /f "tokens=3 delims=," %%a in ("%names%") do (
echo you have choosen to remove %%a
::remove third name in the list
goto start
)
)
if errorlevel 2 ::remove second name in the list
if errorlevel 1 ::remove first name in the list
I've tried using del but that turns out to delete a file in your folder.
I've tried renaming a specific name using set name[%%a]="" but that did nothing.
Any ideas?
Have a look at this example. There are many ways.
#echo off
setlocal enabledelayedexpansion
set names="Bob","Steven","Harry"
for %%i in (%names%) do (
set /a num+=1
set "!num!=%%~i"
)
for /l %%a in (1,1,%num%) do (
set choices=!choices!%%a
echo !num!.!%%a!
)
choice /c 123 /m "please select name to remove"
for /l %%a in (1,1,%num%) do if not "!%%a!"=="!%errorlevel%!" set new_names=!new_names! !%%~a!
echo %new_names:~1%
It can be done without the last for loop as well.. but I opted for it.
Here is some example code, for you to run, and then try to comprehend, I hope it helps rather than confuses:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "Delims==" %%A In ('Set # 2^>NUL') Do Set "%%A="
Set "i=0"
For /F "Delims=:" %%A In ('FindStr "^::" "%~f0" 2^>NUL') Do (
Set /A i+=1
Set "#!i!=%%A"
Echo= !i!. %%A
)
:Ask
Set # 1>NUL 2>&1
If ErrorLevel 1 Echo= Empty names list&Timeout 3 1>NUL&Exit /B
Echo=&Set /P "Option= Choose a name to remove>"
Set #|Findstr "^#%Option%=" 1>NUL||GoTo :Ask
Set "Name=!#%Option%!"
Echo= You've chosen to remove "%Name%"
Timeout 2 1>NUL
Set "#%Option%="
ClS
For /F "Tokens=1*Delims=#=" %%A In ('Set # 2^>NUL') Do Echo= %%A. %%B
GoTo Ask
::Alan
::Beth
::Cleo
::Dale
::Eric
::Faye
::Greg
::Hugh
::Inga
Important note:Please ensure, before saving the above content as a Windows Command Script, that there is a line return, (blank line), at the end.

Create a numbered list based on a given list of strings

windows cmd batch
I have a text file that looks like this:
Dog
Cat
Frog
Bat
Bird
Mouse
I want to attribute a number to each string, each line.
So that it becomes
1 Dog
2 Cat
3 Frog
4 Bat
5 Bird
6 Mouse
Then I want to ask the user to input a number, and then have the corresponding string stored in the variable.
So if the user inputs 1, then the variable is set to the string Dog
So when the user inputs 1 the program stores/outputs Dog
set /p var1="number? " so var1 becomes the string, not the number.
This does the first part of the task kinda, but now I need the second part, storing the strings in avariable.
#echo off
set TEXT_T="list.txt"
set /a c=0
setlocal ENABLEDELAYEDEXPANSION
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
)
endlocal
pause
Here below is an updated answer, thanks to LotPings.
With a small tweak to ask for the folder by string
This provides an easier way to use Megatools from CMD
https://megatools.megous.com/man/megals.html
https://github.com/megous/megatools
#echo off
:start:
megals /Root/
set /p var1="dir? " & megals /Root/%%var1%%
for /f "tokens=1,* delims=:" %%A in ('megals -n /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)
for /f "tokens=1,* delims=:" %%A in ('megals -n -e /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
)
set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%
set "trimmedlink="
for %%h in (%Link%) do if not defined trimmedlink set "trimmedlink=%%h"
Megadl %trimmedlink% && goto :start:
pause
Edit: Had to trim %Link% to just the first word, i.e just the link
The output of Megals -e /Root/misc looks like this:
The asterisk are the unique link ids for the files
/Root/misc
https://mega.nz/#!********!********************* /Root/misc/File1
https://mega.nz/#!********!********************* /Root/misc/File2
https://mega.nz/#!********!********************* /Root/misc/File3
With the batch script above it looks like:
1 File1
2 File2
3 File3
Select #: <------input file number
Edit2 number listing is fixed
Edit3 Parentheses in filenames crash the program
i.e
1 File(1)
The chosen file number then gets passed to Magadl as the corresponding link
Megadl Link
The batch script allows you to download the link by just entering the corresponding file number so you don't have to type out the long link id in a standard cmd window.
To use megals output directly and avoid delayedexpansion (which removes the !)
Findstr /n will do the numbering.
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
set Item[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Echo Choice:%%Item[%choice%]%%
Using a (pseudo-)call with doubled percent signs is an old fashioned method of realizing delayed expansion without the problem with the !.
In programming/scripting you need to adapt techniques to fit your needs.
Without knowing the exact output of your megatools,
this could do the job :
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
set Folder[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Set Folder=%%Folder[%choice%]%%
for /f "tokens=1,* delims=:" %%A in ('megals -e %Folder% ^|findstr /n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%
megadl %Link%
As compo advised, please edit your question to contain all necessary information - don't force others to gather it from unnecessary answer and comments.
Just use an array:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set TEXT_T="list.txt"
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
echo !string[%number%]!
pause
For further details, see this answer.
What about making the output of a command into a list, instead of a text file into a list?
so the line with var1 here would be turned into a numbered list (the files listed in that directory), and Var2 is the number the users enters to create the returned string that gets passed to a command. i.e
https://megatools.megous.com/man/megals.html
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
(megals -e /Root/) <--------list all folders in root
(set /p var1="dir? " && megals -e /Root/!var1!) <-- select folder + list files
(set /p var2="Megalink? " && Megadl !var2!) <--- enter the file name for download
ENDLOCAL
pause
I want to be able to enter a number for the download, instead of the long file name. So this way the user can enter the number that corresponds to the link that they want to download. So if they enter 1 then the program does Megadl Dog
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
megals -e /Root/
set /p var1="dir? "
megals -e /Root/!var1! > rlist.txt
set TEXT_T="rlist.txt"
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
Megadl !string[%number%]!
Endlocal
pause
This kills the first part of the link because it removes everything in between and including exclamations. !dasasdasd!
all megalinks start with #!something!
This didn't work
I need output of Call Echo %%Item[%choice%]%% passed to Megadl
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/anime ^|findstr /n "." ') do (
set Item[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Echo %%Item[%choice%]%%
for /F "tokens=*" (`%%Item[%choice%]%%`) do (
set "var1=%%A"
Megadl !Var1!
)
pause

Getting a list of common strings with first occurance mark

I've got bunch of text files with some content. First I wanted to number the lines globally. Then I extracted all lines that are duplicated somewhere (occur in any of given files at least twice). But now I need to mark all of these lines with the filename and line number of the first occurrence of this line. And now the funny part - it needs to be a windows batch file, using native windows tools. That's why I've got this problem to begin with.
So, to sum it up:
I have a file A with unique strings/lines, each of them is said to occur at least twice in given set of files.
I need to search these files and mark all occurrences of given line from A file with
-file name in which the line first occured
-line number in this file
This is my code with effort to number lines and format files.
#echo off
setlocal EnableDelayedExpansion
set /a lnum=0
if not [%1]==[] pushd %1
for /r %%F in (*.txt) do call :sub "%%F"
echo Total lines in %Files% files: %Total%
popd
exit /b 0
:Sub
set /a Cnt=0
for /f %%n in ('type %1') do (
set /a Cnt+=1
set /a lnum=!lnum!+1
echo ^<!lnum!^> %%n >> %1_ln.txt && echo ^<!lnum!^> >> %1_ln.txt && echo. >> %1_ln.txt
)
set /a Total+=Cnt
set /a Files+=1
echo %1: %Cnt% lines
#echo off
setlocal EnableDelayedExpansion
set lnum=0
if not "%~1" == "" pushd %1
rem "I've got bunch of text files..." (%%F is file name)
for /r %%F in (*.txt) do call :sub "%%F"
echo Total lines in %Files% files: %lnum%
popd
exit /b 0
:Sub "filename"
set Cnt=0
rem "... with some content." (%%n is line contents)
(for /f "usebackq delims=" %%n in (%1) do (
set /a Cnt+=1
rem "First I wanted to number the lines globally."
set /a lnum+=1
echo ^<!lnum!^> %%n
rem "Then I extracted all lines that are duplicated somewhere" (that were defined before)
if defined line[%%n] (
rem "I need to mark all of these lines with the filename and line number of the first occurrence of this line."
echo ^<!line[%%n]!^>
echo/
) else (
REM (Store the first occurrence of this line with *local* line number and filename)
set line[%%n]=!Cnt!: %1
)
)) > "%~PN1_ln.txt"
set /A Files+=1
echo %1: %Cnt% lines
exit /B
The above Batch program ignore empty lines in the input files and fail if they contain special Batch characters, like ! & < > |; this limitation may be fixed if required.
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%s IN (A) DO (
SET searching=Y
FOR /f "delims=" %%f IN (
'dir /s /b /a-d *.txt') DO IF DEFINED searching (
FOR /f "tokens=1delims=:" %%L IN (
'findstr /b /e /n /l /c:"%%s" ^<"%%f"') DO IF DEFINED searching (
ECHO Line %%L IN "%%f" FOUND "%%s"
SET "searching="
)
)
)
Here's the meat of a routine that should do what you appear to be looking for - and that's as clear as mud.
It looks through the "A" file for each string in turn, assigns the string to %%s and sets the flag searching
Then it looks through the file list, assigning filenames to %%f
Then it executes a findstr to find the /c:"%%s" complete string %%s (including any spaces) in /l or literal mode (ie. not using regular expressions) for a line that both /b and /e begins and ends with the target (ie exactly matches) and /n numbers those lines.
The output of findstr will be in the format linenumber:linecontents so if this line is examined by the FORwith the option "delims=:" then the partion up to the first : is assigned to to %%L
So - %%L contains the line#, %%f the filename, %%s the string
Clearing searching having detected this line by setting its value to [nothing] means it's not NOT DEFINED hence no further lines will be reported from the current file, and no further filenames will be examined.
Now if you want to get a listing of ALL of the occurrences of the target lines, all you need to do is to REM-out the SET "searching=" line. Searching will then never be reset, so each line in each file is reported.
If you want some other combination, please clarify.
I have absolutely no idea whatever what you mean by "marking" a line.
#ECHO OFF & setlocal
for /f "tokens=1*delims==" %%i in ('set "$" 2^>nul') do set "%%i="
for %%a in (*.txt) do (
for /f %%b in ('find /v /c "" ^<"%%a"') do echo(%%b lines in %%a.
set /a counter=0, files+=1
for /f "usebackqdelims=" %%b in ("%%~a") do (
set /a counter+=1, total+=1
set "line=%%b"
setlocal enabledelayedexpansion
if not defined $!line! set "$!line!=%%a=!counter!=!line!"
for /f "delims=" %%i in ('set "$" 2^>nul') do (if "!"=="" endlocal)& set "%%i"
)
)
echo(%total% lines in %files% files.
for /f "delims=" %%a in (a) do set "#%%a=%%a"
for /f "tokens=2,3*delims==:" %%i in ('set "$" 2^>nul') do (
if defined #%%k echo("%%k" found in %%i at line %%j.
)
Script can handle !&<>|%, but not =.

Resources