Use call only in batch files containing expressions from another file

Asked

Viewed 113 times

1

I have a directory with files .bat named randomly within each file. bat of this directory have unique names like:

  • nome1
  • Nome2
  • name3
  • ...

I also have, within the same directory, a file only.txt. In this file, each line is a name of the ones inside the files with the extension . bat.

I need a command to run through a Windows batch file that checks each of the lines of only.txt and call the . bat file that has this name.

Practical example:

Code from the abc.bat file

echo nome10

File code Xyz.bat

echo nome1

Text in the file only.bat:

nome1

What needs to be executed by the new batch file: call Xyz.bat

1 answer

1

Try this:

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
::***************************************************

Based on this answer https://stackoverflow.com/a/41926276/5429657

I modified it as follows, the batch file checks the names inside the file failed.txt, stores the search result in a file call.txt applies the call to the files inside the call.txt.

  • your proposal works, but it is extremely slow, every time it scans all the files. bat the directory in search of the content in the file . txt, work with about 10,000 files . bat in this directory, so waiting to find all the names, it is faster to find manual =\

  • @talnun sorry, I don’t have enough knowledge to improve this code

Browser other questions tagged

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