Batch - Search for part/exact name and copy line f

2019-07-21 06:19发布

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

I also have a variable called %file_name% in the batch file already defined.

So lets say that is it is %file_name% = V001-video_folder_6.mp4

As you can see there is some more extra information, V001- and .mp4.

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%.

I think I would use findstr however I have been playing around and not getting the best results.

3条回答
不美不萌又怎样
2楼-- · 2019-07-21 06:54

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%

Open a command prompt window, enter the command for /?, hit key RETURN or ENTER and read output help to understand this little 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
查看更多
Ridiculous、
3楼-- · 2019-07-21 07:06

The problem with the methods that use findstr is that they are slow, because they require to execute findstr.exe (a ~30KB file) each time. A simpler/faster solution is to use just internal Batch commands with the aid of an array. If the number of names to process is large, the difference in time between the two methods may be marked.

@echo off
setlocal EnableDelayedExpansion

rem Load the lines from text file into an array with the last part as index:
for /F "delims=" %%a in (test.txt) do (
   set "line=%%a"
   for %%b in (!line:/^= !) do set "lastPart=%%b"
   set "folder[!lastPart!]=%%a"
)

set "file_name=V001-video_folder_6.mp4"

rem Get the folder from file_name:
for /F "tokens=2 delims=-." %%a in ("%file_name%") do set "folder_path=!folder[%%a]!"

echo Folder path is: %folder_path%
查看更多
欢心
4楼-- · 2019-07-21 07:15

This should work:

::file_name=V001-video_folder_6.mp4
::file containing folder paths is called paths.txt

for /f "tokens=2 delims=-." %%a in ("%file_name%") do set FN=%%a
for /f %%a in ('findstr /E /L "%FN%" "paths.txt"') do set folder_path=%%a
echo %folder_path%

Which does what you want in effectively two lines.

查看更多
登录 后发表回答