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.
Let us assume the posted lines are in file
Test.txt
in current working directory.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 offindstr.exe
after first found line containing the folder path of interest.Perhaps better in case of searched folder is not found in text file:
The problem with the methods that use
findstr
is that they are slow, because they require to executefindstr.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.This should work:
Which does what you want in effectively two lines.