问题
All commands must be performed in Windows Command Prompt
I have a file data.txt which has several strings in it:
gargonxx**stringX**moregargon
gargonargongargongargon
gargon**stringZ**xxgargonxxxx
and for this data file, I want to create a "library" file:
stringX = informationx
stringY = informationy
stringZ = informationz
then create variables in CMD out of the "information" shown in the "library" file, ONLY of the instances in the data.txt file that match with the library file.
varx = string's informationx
and then relay this information in the command window.
echo varx
How do I go about
- Having CMD recognize the instances that match
- Relaying the instance's information in the command window
Here are my LIBRARY and DATA files
回答1:
@ECHO OFF
SETLOCAL
FOR /f "delims==" %%a IN ('set $ 2^>nul') DO "SET %%a="
FOR /f "delims=" %%a IN (data.txt) DO (
FOR /f "tokens=1*delims== " %%L IN (library.txt) DO (
IF /i "%%a"=="%%L" SET $%%a=%%M&ECHO(%%M
)
)
ECHO ==== OR =====
SET $
GOTO :EOF
use a text editor to create the library file.
This batch matches the instances that match.
Showing the information depends on what you want to do.
With 'data.txt`
lineX
lineZ
and library.txt
lineX = informationx
lineY = informationy
lineZ = informationz
the above batch showed
informationx
informationz
==== OR =====
$lineX=informationx
$lineZ=informationz
For revised specification
@ECHO OFF
SETLOCAL
FOR /f "delims==" %%a IN ('set $ 2^>nul') DO "SET %%a="
FOR /f "tokens=1*delims== " %%L IN (library.txt) DO (
FINDSTR /i /L "%%L" data.txt >nul
IF NOT ERRORLEVEL 1 SET $%%L=%%M&ECHO(%%M
)
ECHO ==== OR =====
SET $
GOTO :EOF
(same results, except line
replaced by string
)
来源:https://stackoverflow.com/questions/20966845/displaying-the-libraried-instances-of-prediefined-strings-within-a-file-in-cmd