Is it possible to optimize this code?

Asked

Viewed 128 times

2

I asked a question here and sent me a suggestion of a code that does a search on the contents of all files . bat in a directory using as criteria, names that are in a file . txt.

set "File2Read=falhou.txt"
set "File3Read=call.txt"
If Not Exist "%File2Read%" (Goto :Error)
rem This will read a file into an array of variables and populate it 
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%File2Read%"') do (
    set /a count+=1
    set "Line[!count!]=%%a"
)
rem Display array elements
For /L %%f in (1,1,%Count%) do (
    FOR %%G IN (*.bat) do (findstr /m "!Line[%%f]!" "%%G")>>call.txt
)

for /f "delims=" %%a in ('Type "%File3Read%"') do (
    set /a count+=1
    set "Line[!count!]=%%a"
)
rem Display array elements
For /L %%c in (1,1,%Count%) do (
    call "!Line[%%c]!"
)

pause
Exit
::***************************************************
:Error
cls & Color 4C
echo(
echo   The file "%File2Read%" dos not exist !
Pause>nul
exit /b
::***************************************************

The code works, the problem is that it does a very long search, because I have 10 thousand files . bat to be swept in search of each of the names, every time a name found the search resumes from the first file.

It would be possible to improve this in some way?

  • 2

    This is a script, why you need to optimize?

  • 1

    @Maniero it is taking about 4 hours to find 20 names out of the 10 thousand files, it would be faster to do a manual search with Notepad ++

  • 2

    The optimization is to use a programming language made to be optimized and not make a script command-line.

  • @talnun Do you need to know which file contains the searched name? An idea is, before the search, create a temporary file with the contents of all the files and do the search inside this temporary file.

  • @Dherik this could cause problems with giant files and little disk space, but this concern does not apply (probably) to this case

  • exactly @Dherik, I intend to search the contents of several files, unique names, the problem is that the above script comes back to scan all the files when it finds the first name, the second, so on...

  • @talnun, your difficulty is to have an idea of what to do or how to do it? The idea I gave is not useful to you?

  • the difficulty is how to do @Dherik, I’m going to try this way: I created a file . txt only with the 10,000 names, which are unique in the same order as the . bat files are named, so the name50 is on the 50 line of that . txt, fulano100 is on line 100 of . txt, I’m looking for how to know the line corresponding to the name and so "concatenate" the call function to call it... a few more days searching and I should be able to

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.