Humberto Freitas
Humberto Freitas

Reputation: 483

Search in specific txt

I'm making a batch script where the User do the scanning of a folder and batch list the files of the same to a TXT and after it is done scanning the TXT list looking for files with the same name as a list, predetermined, however I have problems after

1 - I have not the slightest idea of how do I show file that was found in the separate list

Example:

My list:

VideoTeste
Family
Myfavoritesong
My Song

Result in Documentos.txt:

Videoteste.avi
Family.exe
family.jpg
MY_1231dFamilyxzsaxad.jpg
MyFavoritesong.mp3
Folder_Family
My Song.mp3



Result in Downloads.txt:

Videoteste.rar
Family_235.dat
MyFavoritesong_&.mp3
My Song.mp3


Result in Temp.txt:

Videoteste.rar
Family_235.dat
MyFavoritesong_&.mp3
My Song.mp3

2 - This list is not fixed as well as local and this would in a way where the local and items could not cause errors to the script as spaces or &.

Note:

1 - The lists are in the temp folder in several .txt files

2 - If possible if it were not found anything in a .txt file information appeared that nothing was found in the output file.

Upvotes: 0

Views: 64

Answers (1)

Mofi
Mofi

Reputation: 49096

Here is a batch file to

  • scan in each folder listed at top of the batch file
  • for files and folders in a list file with fixed name
  • and write just the names of all found files and folders into a text file with same name as the scanned folder.

An information message is output if no file/folder from list was found in a specified folder.

So the batch file does not search in text files for names of files and folders in list file.
But I suppose the text files in temp folder were just created before also with batch code.

@echo off
call :SearchFromList "C:\Temp\Documentos"
call :SearchFromList "C:\Temp\Downloads"
call :SearchFromList "C:\Temp\Temp"
goto :EOF

:SearchFromList
set "Folder=%~1"
set "ListFile=C:\Temp\%~nx1.txt"

echo Scan folder "%Folder%" for files in "My List.txt"
echo and write found files and folders to "%ListFile%".
echo.

if exist "%ListFile%" del /F "%ListFile%"
for /F "usebackq delims=" %%N in ("C:\Temp\My List.txt") do (
    for /F "delims=" %%F in ('dir /B "%Folder%\*%%N*" 2^>nul') do echo>>"%ListFile%" %%~nxF
)

if not exist "%ListFile%" (
    echo>"%ListFile%" No file found from list.
    echo No file found from list in folder "%Folder%".
    echo.
)
goto :EOF

See Batch creation of a list of folders : can't echo accented characters on what to do to get file and folder names with local characters written with local GUI code page into a text file.

Upvotes: 1

Related Questions