1

The original post for this would be:

Batch - Search for part/exact name and copy line from text file into batch as var

The solution provided was:

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

However after testing for a few days, it works but under a very strict criteria. I would like to change that.

Here is a group of folder:

/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

This file V001-video_folder_6.mp4 is moved the appropriate folder i.e. /var/www/xxx/html/videos/video_folder_6.

However. If I try these variations it does not work:

  1. V001-video_folder_6.mp4 - WORKS
  2. V001 - video_folder_6.mp4 - DOES NOT WORK
  3. V001-video_folder_6.com.mp4 - DOES NOT WORK

The - and . are paramount for this to work.

Is there a way to get the batch code to look for the string video_folder_6 regardless of the stuff around it. e.g. V001 - Hello ___ video_folder_6 ok.....com.mp4.

Community
  • 1
  • 1
Arthor
  • 666
  • 2
  • 13
  • 40

2 Answers2

1

A small modification of my fast solution given in that question solve your new request:

EDIT: New method developed as reply to comment

When the path_folder is not separated from the stuff around by the standard delimiters, then the elements in folder array must be searched in the whole file name until one of them is found. The Batch file below now includes this additional part; however, if both the number of paths in the file and the number of file_names with no standard delimiters are large, then this method may take a long time.

@echo off
setlocal EnableDelayedExpansion

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

rem Process file names stored in test2.txt file (as example)
for /F "delims=" %%f in (test2.txt) do (
   set "file_name=%%f"
   call :GetFolder
)
goto :EOF


:GetFolder
echo/
echo File name is: "%file_name%"
set "folder_path="

rem Separate file name in parts delimited by "-. "
rem and use each part to test the element in folder array
set "name=%file_name%"
:nextPart
for /F "tokens=1* delims=-. " %%a in ("%name%") do (
   if not defined folder[%%a] (
      set "name=%%b"
      goto nextPart
   ) else (
      set "folder_path=!folder[%%a]!"
      goto pathFound
   )
)

rem Search the elements of folder array in the file_name
for /F "tokens=2,3 delims=[]=" %%a in ('set folder[') do (
   if "!file_name:%%a=!" neq "%file_name%" (
      set "folder_path=%%b"
      goto pathFound
   )
)
echo ERROR: Folder path not found
exit /B

:pathFound
echo Folder path is: "%folder_path%"
exit /B

This solution assume that the folder name is separated form the stuff around by dash, period or space. These delimiters may be changed if necessary.

Output example:

File name is: "V001-video_folder_6.mp4"
Folder path is: "/var/www/xxx/html/videos/video_folder_6"

File name is: "V001 - video_folder_6.mp4"
Folder path is: "/var/www/xxx/html/videos/video_folder_6"

File name is: "V001-video_folder_6.com.mp4"
Folder path is: "/var/www/xxx/html/videos/video_folder_6"

File name is: "V001 - Hello ___ video_folder_6 ok.....com.mp4"
Folder path is: "/var/www/xxx/html/videos/video_folder_6"

File name is: "V001 - Hello ___ video_folder_6_ok.....com.mp4"
Folder path is: "/var/www/xxx/html/videos/video_folder_6"

File name is: "V001 - Hello ___ video_folder_6-ok.....com.mp4"
Folder path is: "/var/www/xxx/html/videos/video_folder_6"

File name is: "V001 - Hello ___-video_folder_6----ok.....com.mp4"
Folder path is: "/var/www/xxx/html/videos/video_folder_6"

File name is: "V001 - Hello ___ video_folder_X-ok.....com.mp4"
ERROR: Folder path not found
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • @Aacinin - First of all, thank you. As you stated the file name MUST have space on its left and right. but for example, if it were: `set "file_name=V001 - Hello ___ video_folder_6_ok.....com.mp4"` or `set "file_name=V001 - Hello ___ video_folder_6-ok.....com.mp4"` or `set "file_name=V001 - Hello ___-video_folder_6----ok.....com.mp4"`. I think the problem is that there are so many possibilities. I am not sure this is possible, but is there anyway to make it search for the text it self. e.g. Like searching an array. I think batch does not support an array. Thank you – Arthor Apr 19 '15 at 09:59
  • 1
    Your _new_ request was added to the solution, check it. PS - It is not a good idea to develop a program via a series of small requests (now this change, and now this change, etc). It is easier to develop a complete program with full requirements from the very beginning, because _modifications_ are much more difficult to write... `:(` – Aacini Apr 19 '15 at 17:25
  • First of all, thank you. I am going to test it out. Second of all, I have had other stack members tell me that asking for a complete solution is a bad idea as there are many sections to it. I really appreciate the hard work. My humble thanks. +1. would + more if I could. – Arthor Apr 20 '15 at 00:07
0

use the variable %file_name% for the file

Set "file_name=V001-video_folder_6.com.mp4"

Set "file_name_new=%file_name:~+4%"
Set "file_name_new=%file_name_new: =%"
Set "file_name_new=%file_name_new:-=%"
Set "file_name_new=%file_name_new:video=%"
Set "file_name_new=%file_name_new:_=%"
Set "file_name_new=%file_name_new:folder=%"
Set "file_name_new=%file_name_new:.com=%"

for /F "tokens=1 delims=-." %%A in ("%file_name_new%") 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%

If you wanted to remove anything around, I made this script, however there is a problem it does not remove numbers.

@setlocal enableextensions enabledelayedexpansion

Set "file_name=V001-video_folder_4.com.mp4"
set file_name_new=!file_name:~0,-4!
Set "file_name_new=%file_name_new:~+4%"
Set "file_name_new=%file_name_new: =%"
Set "file_name_new=%file_name_new:A=%"
Set "file_name_new=%file_name_new:B=%"
Set "file_name_new=%file_name_new:C=%"
Set "file_name_new=%file_name_new:D=%"
Set "file_name_new=%file_name_new:E=%"
Set "file_name_new=%file_name_new:F=%"
Set "file_name_new=%file_name_new:G=%"
Set "file_name_new=%file_name_new:H=%"
Set "file_name_new=%file_name_new:I=%"
Set "file_name_new=%file_name_new:J=%"
Set "file_name_new=%file_name_new:K=%"
Set "file_name_new=%file_name_new:L=%"
Set "file_name_new=%file_name_new:M=%"
Set "file_name_new=%file_name_new:N=%"
Set "file_name_new=%file_name_new:O=%"
Set "file_name_new=%file_name_new:P=%"
Set "file_name_new=%file_name_new:Q=%"
Set "file_name_new=%file_name_new:R=%"
Set "file_name_new=%file_name_new:S=%"
Set "file_name_new=%file_name_new:T=%"
Set "file_name_new=%file_name_new:U=%"
Set "file_name_new=%file_name_new:V=%"
Set "file_name_new=%file_name_new:W=%"
Set "file_name_new=%file_name_new:X=%"
Set "file_name_new=%file_name_new:Y=%"
Set "file_name_new=%file_name_new:Z=%"
Set "file_name_new=%file_name_new:.=%"
Set "file_name_new=%file_name_new:_=%"
Set "file_name_new=%file_name_new:-=%"


for /F "tokens=1 delims=-." %%A in ("%file_name_new%") 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%
endlocal
Humberto Freitas
  • 461
  • 1
  • 6
  • 21