批处理 - 搜索部分/确切名称,并从文本文件复制到行批处理中VAR [英] Batch - Search for part/exact name and copy line from text file into batch as var
问题描述
这下面的信息中包含一个文本文件,并格式化为这样的。
This information below is contained in a text file and formatted as such.
/var/www/xxx/html/videos/video_folder_1
/var/www/xxx/html/videos/video_folder_2
/var/www/xxx/html/videos/video_folder_3
/var/www/xxx/html/videos/video_folder_4
/var/www/xxx/html/videos/video_folder_5
/var/www/xxx/html/videos/video_folder_6
/var/www/xxx/html/videos/video_folder_7
我也有一个名为%FILE_NAME%
在批处理文件中已定义的变量。
I also have a variable called %file_name%
in the batch file already defined.
因此,可以说这是它是%FILE_NAME%
= V001-video_folder_6.mp4
So lets say that is it is %file_name%
= V001-video_folder_6.mp4
正如你可以看到有一些额外的信息, V001 -
和 .MP4
As you can see there is some more extra information, V001-
and .mp4
.
我想使用var %FILE_NAME%
来搜索文本文件,并返回整行。在这种情况下,它会返回 /无功/网络/ XXX / HTML /视频/ video_folder_6
,然后把这些信息在一个新的变种,我们可以说,%FOLDER_PATH%
。
I would like to use the var %file_name%
to search the text file and return the entire line. In this case it would return /var/www/xxx/html/videos/video_folder_6
and then put this information in a new var, let us say, %folder_path%
.
我想我会使用 FINDSTR
但是我一直在玩周围,没有得到最好的结果。
I think I would use findstr
however I have been playing around and not getting the best results.
推荐答案
让我们假设张贴线路都在文件 Test.txt的
在当前工作目录。
Let us assume the posted lines are in file Test.txt
in current working directory.
@echo off
set "file_name=V001-video_folder_6.mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto NextCommand
)
:NextCommand
echo Full folder path is: %folder_path%
打开命令提示符窗口,输入命令为/?
,命中关键<大骨节病>返回骨节病>或<大骨节病> ENTER 骨节病>和阅读输出有助于理解这个小小的code。
Open a command prompt window, enter the command for /?
, hit key RETURN or ENTER and read output help to understand this little code.
命令转到
在作为循环中立即退出中的Findstr.exe 循环处理结果输出code>后首次发现含有感兴趣的文件夹路径线。
The command goto
inside FOR loop results in an immediate exit from loop processing output of findstr.exe
after first found line containing the folder path of interest.
也许更好的搜索文件夹的情况下,是不是在文本文件中找到:
Perhaps better in case of searched folder is not found in text file:
@echo off
set "file_name=V01-VIDEOS for school (Miss Patrick).mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto FoundFolder
)
echo "%folder%" not found in file Test.txt.
pause
goto :EOF
:FoundFolder
echo Full folder path is: "%folder_path%"
pause
这篇关于批处理 - 搜索部分/确切名称,并从文本文件复制到行批处理中VAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!