-2
如何在文本文件中逐行搜索字符串,並在找到匹配項時將發現匹配項的整行復制到變量中?在批處理腳本中搜索文本文件中的字符串
基本上,我有一個文本文件,其中包含文件夾中所有子文件夾的地址/路徑。我想在這個文本文件中搜索一個字符串(該字符串只匹配一行的一部分),如果有匹配,我想將其複製到整行的變量中。
該字符串來自文件名,一旦文本文件匹配,我想使用子文件夾地址在那裏移動文件。
這是我到現在爲止完成:
@ECHO off
::Reads all the folders and subfolders existing in the "root1" folder and writes the path for each foleder/subfolder into the "1.txt" file
dir /b /s /a:d "...\root1" > "...\1.txt"
::Reads all the files existing in "root2" folder and writes the path of each file into the "2.txt" file
dir /s /b "...\root2\" > "...\2.txt"
SETLOCAL EnableDelayedExpansion
::reads the last file path from the "2.txt" and asign it to a variable
for /f "delims=" %%x in (...\2.txt) do set Build=%%x
set "source=%Build%"
echo %source%
::from (...\...\...\...\a_b.txt) reads the name of the file (a_b.txt) without extension (a_b) and swap the letter and outputs (b\a) into text file "7.txt"
FOR /F "tokens=6 delims=\" %%G IN (...\2.txt) DO echo %%G > ...\3.txt
FOR /F "tokens=1 delims=." %%G IN (...\3.txt) DO echo %%G > ...\4.txt
FOR /F "tokens=1 delims=_" %%G IN (...\4.txt) DO echo %%G > ...\5.txt
FOR /F "tokens=2 delims=_" %%G IN (...\4.txt) DO echo %%G > ...\6.txt
FOR /F %%G IN (...\5.txt) DO set "two=%%G"
FOR /F %%H IN (...\6.txt) DO set "one=%%H"
echo %one%\%two% > ...\7.txt
::assign the content (one line) from "7.txt" file to a variable
FOR /F %%G IN (...\7.txt) DO set "third=%%G"
echo %third%
::should search the string from "third" variable in the "1.txt" and when found a match copy the respective line into a variable and put it into "8.txt"
FOR /F %%a IN ('FINDSTR /x "%third%" ...\1.txt') DO set "xz=%%b"
echo %xz% > ...\8.txt
endlocal
您能否提供一些示例文件並逐步描述您想要使用這些示例文件看到的情況? –
'for/F「delims =」%% M in('find/I「search string」^ <「textfile.txt」')do echo %% I' - 每行包含一個匹配項由'%%我在''for'循環中](http://ss64.com/nt/for.html);而不是['find'](http://ss64.com/nt/find.html),你也可以使用['findstr'](http://ss64.com/nt/findstr.html)... – aschipfl
這沒有幫助。編輯您的問題以添加有用的詳細信息並將其格式化爲可讀性代碼。 –