Home > other >  Is it possible to limit lines to search through in Type | Find operation
Is it possible to limit lines to search through in Type | Find operation

Time:09-17

I need to iterate over multiple text files but exclude some based on name or content. What I have right now, searches through the entire file. This takes pretty long and is unnecessary. I want the (Type "%%f" | FIND "") to only search through the first few lines.

for %%f in (*.ifc) do (
    (Echo "%%f" | FIND /I "optimized" 1>NUL) || (
        (Type "%%f" | FIND /I "Solibri IFC Optimizer" 1>NUL) || (
            echo do some stuff with file
)))

Thanks.

CodePudding user response:

This solution uses a JScript code section to limit the number of lines to just the first 10 of each file:

@if (@CodeSection == @Batch) @then

@echo off

for %%f in (*.txt) do (
   echo File: %%f
   < "%%f" CScript //nologo //E:JScript "%~F0" | find /N /V ""
   echo ================
   echo/
)
goto :EOF

@end

// JScript section: show just a given number of input lines

var fso    = new ActiveXObject("Scripting.FileSystemObject"),
    stdIn  = fso.GetStandardStream(0),
    stdOut = fso.GetStandardStream(1);

for ( var i=1; i <= 10; i   ) {
   stdOut.WriteLine(stdIn.ReadLine());
}

CodePudding user response:

Unless I've misunderstood what you're trying to do, i.e. optimize each IFC file in the current directory, which does not contain the string Solibri IFC Optimizer, and which does not have the substring _optimized, in its filename, then the obvious thing to do would be to filter everything within your for loop. It is much more efficient to filter earlier in the chain, than to do it later. For example:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "SIO=%ProgramFiles%\Solibri\IFCOptimizer\Solibri IFC Optimizer.exe"
Echo Optimizing previously unoptimized IFC files in the current directory.
For /F "Delims=:" %%G In ('%SystemRoot%\System32\findstr.exe /R "^FILE_NAME("
 *.ifc ^| %SystemRoot%\System32\findstr.exe /VRC:"_optimized\.ifc:"
 /C:"(Solibri IFC Optimizer)"') Do "%SIO%" -in="%%G"

Please remember to adjust the variable value for SIO as needed. You'll also need to adjust the command on the last line command, I've currently only included the -in parameter. I'll leave it to you to determine whether you need to include an -out file/directory parameter, and choose between a -ifc or -ifczip compression parameter. However, I will assume that you most likely will want to including the -force parameter, as it appears that you're overwriting the originals with the default -suffix of _optimized.

As a side note, you may find that because this involves invoking a seperate instance of Solibri IFC Optimizer.exe for each previously unoptimized file, that you may wish to consider creating a semi-colon seperated listing of filenames from the for loop. Then use that with the -in parameter, (-in=file1;file2;file3;…). That would only invoke a simgle instance of Solibri IFC Optimizer.exe, which would probably be more efficient.

  • Related