looking for a fastest running batch that looks for certain file extensions or vice versa

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

looking for a fastest running batch that looks for certain file extensions or vice versa

#1 Post by nnnmmm » 01 Sep 2017 22:58

AA.bat has

Code: Select all

@ECHO OFF
SET P1=C:\WINDOWS\NOTEPAD.EXE
SET P2=C:\Programs\IrfanView\i_view32.exe

SetLocal EnableExtensions EnableDelayedExpansion

FOR /F %%V IN ('ECHO %1 ^|FIND /I /C ".BAT"') DO (SET K1=%%V
   IF !K1!==1 (EndLocal& %P1% %1 ) & (GOTO :EOF)
)

FOR /F %%V IN ('ECHO %1 ^|FIND /I /C ".TXT"') DO (SET K1=%%V
   IF !K1!==1 ( EndLocal& %P1% %1 ) & (GOTO :EOF)
)

FOR /F %%V IN ('ECHO %1 ^|FIND /I /C ".TRU"') DO (SET K1=%%V
   IF !K1!==1 ( EndLocal& %P1% %1 ) & (GOTO :EOF)
)

FOR /F %%V IN ('ECHO %1 ^|FIND /I /C ".BMP"') DO (SET K1=%%V
   IF !K1!==1 ( EndLocal& %P2% %1 ) & (GOTO :EOF)
)

FOR /F %%V IN ('ECHO %1 ^|FIND /I /C ".JPG"') DO (SET K1=%%V
   IF !K1!==1 ( EndLocal& %P2% %1 ) & (GOTO :EOF)
)



* what is the fastest running batch for AA.BAT? because | Find, pipe and Find command are known to run too slow.

* or conversly speaking, what is the fastest running batch for a case like this?
if the file extensions are one of the followings , then abort and take no action go to :EOF
because AA.BAT was originally meant for a text editor, but its shell opens anything and sometimes causes a PC freeze. so i figured it out by replacing it with BAT and it only passes %1, one variable. which is a full file path name.

SET Fext=*.EXE *.BMP *.MP4 *.AVI *.MP3

batch must handle filenames with "!"'s in them

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#2 Post by penpen » 02 Sep 2017 01:22

I'm unsure, so you should note that you maybe are contradicting yourself:
Your given code says open ".BMP" files using IrfanView, but your Text says the batch file should " abort and take no action go to :EOF",
because it is one of those file extensions "SET Fext=*.EXE *.BMP *.MP4 *.AVI *.MP3".
I stick to your code, so i open IrfanView in such a case.


I think the fastes way should be:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set /a ".bat=.txt=.tru=.bmp=.jpg=0"
set "%~x1=1"
set /a ".ext1=.bat+.txt+.tru,.ext2=.bmp+.jpg"

if "1" == "%.ext1%" "C:\WINDOWS\NOTEPAD.EXE" "%~1"
if "1" == "%.ext2%" "C:\Programs\IrfanView\i_view32.exe" "%~1"

goto :eof


penpen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#3 Post by nnnmmm » 02 Sep 2017 03:27

Code: Select all

@ECHO OFF
SET P1=START "" NOTEPAD.EXE
REM SET P2=START "" C:\Programs\IrfanView\i_view32.exe
SET P2=START "" C:\Programs\ACDSee32\ACDSee32.exe

Setlocal EnableExtensions DisableDelayedExpansion
SET /A       ".TXT=.BAT=.TRU=.M=.SRT=.SMI=.SSA=.ZLS=.ZTM=.CMD=.VBS=.REG=.INF=.NFO=.INI=.CFG=.CONF=.HTM=.HTML=.BB2=.PS=.TEX=.LOG=.BAK=.OLD=.BA_=.ZL_=.IN_.BMP=.JPG=.PNG=0"
SET "%~X1=1"
SET /A ".EXT1=.TXT+.BAT+.TRU+.M+.SRT+.SMI+.SSA+.ZLS+.ZTM+.CMD+.VBS+.REG+.INF+.NFO+.INI+.CFG+.CONF+.HTM+.HTML+.BB2+.PS+.TEX+.LOG+.BAK+.OLD+.BA_+.ZL_+.IN_,.EXT2=.BMP+.JPG+.PNG"

IF "1" == "%.EXT1%" %P1% "%~1"
IF "1" == "%.EXT2%" %P2% "%~1"

GOTO :EOF


thanks so much, it worked so well.
i had to use start.exe, or it would have hung the host software that provided the shell for other applications until they are released

is it my list too long? i listed all of my text files i think. except for a few like abc.ol~, abc.ba~

but i have one more tricky extension. that actually has no extension, is it possible to include in the list? ex) abc.

i made the "vice versa" part, no worry
all of the above but supply them with new extensions + replace with IF NOT "%.EXT1%"=="1"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#4 Post by penpen » 02 Sep 2017 07:55

nnnmmm wrote:is it my list too long? i listed all of my text files i think. except for a few like abc.ol~, abc.ba~
No, the list could easily could much more; commands in batch could be 8191 characters long.

But the tilde character ('~') is a special operator for the "set/A"-command.
The only way to avoid this is to use a for loop to handle the problematic characters and the empty extension; i also added a blacklist:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
SET P1=START "" NOTEPAD.EXE
REM SET P2=START "" C:\Programs\IrfanView\i_view32.exe
SET P2=START "" C:\Programs\ACDSee32\ACDSee32.exe


:: blacklisted files (abort)
for %%a in (
   ".EXE"
) do (
   if /I "%%~a" == "%~x1" goto :eof
)

:: text editor
for %%a in (
   ".TXT" ".BAT" ".TRU" ".ZLS" ".ZTM"  ".CMD"  ".VBS"  ".REG"
   ".INF" ".NFO" ".INI" ".CFG" ".CONF" ".HTM"  ".HTML" ".BB2"
   ".LOG" ".BAK" ".OLD" ".BA_" ".ZL_"  ".IN_"
   ".ol~" ".ba~"
   ""
) do (
   if /I "%%~a" == "%~x1" %P1% "%~1"
)

:: image viewer
for %%a in (
   ".BMP" ".JPG"
) do (
   if /I "%%~a" == "%~x1" %P2% "%~1"
)

goto :eof


penpen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#5 Post by nnnmmm » 03 Sep 2017 01:01

Code: Select all

AA=
SET /A       ".TXT=.BAT=.TRU=.M=.SRT=.SMI=.SSA=.ZLS=.ZTM=.CMD=.VBS=.REG=.INF=.NFO=.INI=.CFG=.CONF=.HTM=.HTML=.BB2=.PS=.TEX=.LOG=.BAK=.OLD=.BA_=.ZL_=.IN_.BMP=.JPG=.PNG=0"
SET "%~X1=1"
SET /A ".EXT1=.TXT+.BAT+.TRU+.M+.SRT+.SMI+.SSA+.ZLS+.ZTM+.CMD+.VBS+.REG+.INF+.NFO+.INI+.CFG+.CONF+.HTM+.HTML+.BB2+.PS+.TEX+.LOG+.BAK+.OLD+.BA_+.ZL_+.IN_,.EXT2=.BMP+.JPG+.PNG"


Code: Select all

BB=
for %%a in (
   ".TXT" ".BAT" ".TRU" ".ZLS" ".ZTM"  ".CMD"  ".VBS"  ".REG"
   ".INF" ".NFO" ".INI" ".CFG" ".CONF" ".HTM"  ".HTML" ".BB2"
   ".LOG" ".BAK" ".OLD" ".BA_" ".ZL_"  ".IN_"
   ".ol~" ".ba~"
   ""
) do (



which one is faster AA= or BB=?
i dont need to know exact answer, if you think the one may be faster than the other? can you give me some idea how many percent maybe faster even if you are wrong, it is ok, your feeling is good enough. the speed is crucial for the application i am using, because i am bypassing the normal way that the designer meant

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#6 Post by pieh-ejdsch » 03 Sep 2017 04:33

Hello,

Code: Select all

@echo off
setlocal
SET P1=START "" NOTEPAD.EXE
 REM SET P2=START "" C:\Programs\IrfanView\i_view32.exe
SET P2=START "" C:\Programs\ACDSee32\ACDSee32.exe

call :%~x1 "%~1" || echo keine auswahl vorhanden!
pause
exit /b


 rem text editor
:.TXT
:.BAT
:.TRU
:.ZLS
:.ZTM
:.CMD
:.VBS
:.REG
:.INF
:.NFO
:.INI
:.CFG
:.CONF
:.HTM
:.HTML
:.BB2
:.LOG
:.BAK
:.OLD
:.BA_
:.ZL_
:.IN_
:.ol~
:.ba~
%P1% %1
exit /b

 rem image viewer
:.BMP
:.JPG
%P2% %1
exit /b


Phil

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#7 Post by penpen » 03 Sep 2017 06:49

nnnmmm wrote:which one is faster AA= or BB=?
i dont need to know exact answer, if you think the one may be faster than the other? can you give me some idea how many percent maybe faster even if you are wrong, it is ok, your feeling is good enough.
I didn|t know it for sure, so i've tested it (i also tested pieh-ejdsch's code as method CC).
For that i removed any output and subcall, removed extensions unique to a method (for example ""), and called each method 1000 times in block with 10 different extensions:
".TXT", ".TRU", ".CMD", ".INF", ".CFG", ".HTML", ".BAK", ".ZL_", ".ba~", ".JPG".
(Note that ".ba~" in this case is unknown because it is not possible in method AA.)

Result in whole consumed time and relations for the complete test on my pc:

Code: Select all

AA=5.35 seconds, 100.00%,  98.89%,  97.63%
BB=5.41 seconds, 101.12%, 100.00%,  98.72%
CC=5.48 seconds, 102.43%, 101.29%, 100.00%
The result might vary if you add more extensions, or run it on different computers or under different windows versions.

So all of these methods are pretty fast and the difference is small enough, so it is just a matter of taste which one you choose.


@pieh-ejdsch
I've found a minor bug (just add "2>nul " in front of the call to fix it).
If the file extension is unknown, then you get the error message:

Code: Select all

Das Sprungziel - <unknown label> wurde nicht gefunden.

If you want to support the special case "no extension", you might add an additional character so the label is not empty.


penpen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#8 Post by nnnmmm » 03 Sep 2017 23:05

>If you want to support the special case "no extension", you might add an additional character
is there a way to add something for "no extesnion" in AA= without IF /I "%~x1"=="" %P1% "%~1"? i tried :. it didnt work.

does :.TXT execute faster than :.IN_, when the list gets long?

I chose AA= simply because i can edit them better, but i might go back to FOR in () DO solution because i can group extensions by line, since the execution time between all three solutions were almost the same.

AA=

Code: Select all

IF /I "%~x1"=="" %P1% "%~1"

2>nul CALL :%~x1 "%~1" || ECHO KEINE AUSWAHL VORHANDEN!
EXIT /B

:.TXT
:.BAT
:.TRU
:.M
:.F
:.SRT
:.IN_
%P1% %1
EXIT /B

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#9 Post by nnnmmm » 03 Sep 2017 23:17

>Your given code says open ".BMP" files using IrfanView, but your Text says the batch file should " abort and take no action go to :EOF",
is that what it said in ZZ=? i didnt know, i meant if the code finds the "BAT" string then... just run the program as quickly as possible and quit the script and make sure not go to the next line to check TXT then TRU all the way to the end of the list.

ZZ=

Code: Select all

FOR /F %%V IN ('ECHO %1 ^|FIND /I /C ".BAT"') DO (SET K1=%%V
   IF !K1!==1 (EndLocal& %P1% %1 ) & (GOTO :EOF)
)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#10 Post by penpen » 04 Sep 2017 06:57

nnnmmm wrote:>If you want to support the special case "no extension", you might add an additional character
is there a way to add something for "no extesnion" in AA= without IF /I "%~x1"=="" %P1% "%~1"? i tried :. it didnt work.

I'm sure pieh-ejdsch knows what i mean, but i better should have formulated it clear to everyone.
Here is an example how to add a character - for example 'x', so the label ":x" is associated with an empty file extension:

Code: Select all

@echo off
setlocal
SET P1=START "" NOTEPAD.EXE
 REM SET P2=START "" C:\Programs\IrfanView\i_view32.exe
SET P2=START "" C:\Programs\ACDSee32\ACDSee32.exe

call :x%~x1 "%~1" || echo keine auswahl vorhanden!
pause
exit /b


 rem text editor
:x.TXT
:x.BAT
:: (...)
:x.ba~
:x
%P1% %1
exit /b

::(...)


nnnmmm wrote:does :.TXT execute faster than :.IN_, when the list gets long?
Of course. The goto-command (which is executed internally by "call :label") scans the batch file line by line, until it finds, what is needed.

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#11 Post by nnnmmm » 04 Sep 2017 08:29

AA=

Code: Select all

2>nul CALL :z%~x1 "%~1" || ECHO KEINE AUSWAHL VORHANDEN!
EXIT /B

:z.TXT
:z.BAT
:%P1% %1
EXIT /B


BB=

Code: Select all

2>nul CALL :z%~x1 "%~1" || ECHO KEINE AUSWAHL VORHANDEN!
EXIT /B

:z.TXT
%P1% %1
EXIT /B

:z.BAT
%P1% %1
EXIT /B


lastly, you call AA= labeling, if %P1% %1 is the same, correct?
then BB= can be reduced in notations.

thanks for this.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: looking for a fastest running batch that looks for certain file extensions or vice versa

#12 Post by penpen » 04 Sep 2017 12:37

nnnmmm wrote:lastly, you call AA= labeling, if %P1% %1 is the same, correct?
I'm unsure how the question is meant.
I always call the following a label, no matter which name it has, or which source code is following that line:

Code: Select all

:name


nnnmmm wrote:then BB= can be reduced in notations.
AA= and BB= are different in functionality, although the resulting programs are equivalent.
So, that's not question of notation.
In case "%P1% %1" are the same in both examples, the source of BB= can be reduced to the source of AA=.


penpen

Post Reply