Author Topic: Bat file for searching  (Read 4023 times)

Magnum

  • Member
  • *****
  • Posts: 2379
Bat file for searching
« on: January 14, 2013, 05:59:31 PM »
It took around 4.5 seconds to find 2 identical search.bat files in different directories using s*.bat as the search pattern.


A future improvement is search.bat s*.asm without the search pattern being hard coded into the bat file.

I recall using %1.

:: Search.bat Dave Benham,mousio,
::

cls
@echo off
echo.
echo Timer has started.
echo.
call :StartTimer
::
:: Add your script functionality here
::
@echo off
setlocal disableDelayedExpansion
set "searchRoot=."
set "fileMask=s*.bat"

set "prior=noMatch"
for /f "tokens=1,2 delims=?" %%A in (
  '(for /r "%searchRoot%" %%F in ("%fileMask%"^) do @echo %%~zF:%%~nxF?%%~fF^)^|sort'
) do (
  set "current=%%A"
  setlocal enableDelayedExpansion
  if !prior! equ !current! (
    if defined priorFile (
      echo(
      echo !priorFile!
    )
    endlocal
    echo %%B
    set "priorFile="
  ) else (
    endlocal
    set "prior=%%A"
    set "priorFile=%%B"
  )
)

call :StopTimer
call :DisplayTimerResult
pause
goto :EOF

:StartTimer
:: Store start time
set StartTIME=%TIME%
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %StartTIME: =0%`) do set /a Start100S=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100
goto :EOF

:StopTimer
:: Get the end time
set StopTIME=%TIME%
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %StopTIME: =0%`) do set /a Stop100S=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100
:: Test midnight rollover. If so, add 1 day=8640000 1/100ths secs
if %Stop100S% LSS %Start100S% set /a Stop100S+=8640000
set /a TookTime=%Stop100S%-%Start100S%
set TookTimePadded=0%TookTime%
goto :EOF

:DisplayTimerResult
:: Show timer start/stop/delta
echo Started: %StartTime%
echo Stopped: %StopTime%
echo Elapsed: %TookTime:~0,-2%.%TookTimePadded:~-2% seconds
goto :EOF
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

jj2007

  • Member
  • *****
  • Posts: 13302
  • Assembly is fun ;-)
    • MasmBasic
Re: Bat file for searching
« Reply #1 on: January 14, 2013, 06:36:33 PM »
A future improvement is search.bat s*.asm without the search pattern being hard coded into the bat file.

@echo off
echo *** Using first char and extension of %1 ***
set spat=%~n1
set spat=%spat:~0,1%*%~x1
echo.
echo %spat%
echo.
pause

 :icon14:

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Bat file for searching
« Reply #2 on: January 14, 2013, 11:32:05 PM »
hi Andy,

i often start batch files that use parameters by testing to see that the user entered a parameter

Code: [Select]
@echo off
if "x%1"=="x" goto ascusage
if exist %1.asm goto ascasm
:ascusage
echo Usage: asc asmfile
echo "asmfile" = asmfile.asm
goto batchexit
:ascasm
;
;
in that example, i make sure there is a file, otherwise i show a "usage" message and exit

but, in your case,
Code: [Select]
if "x%1"=="x" goto ascusageis the main line of interest
"x%1"=="x" would evaluate true if %1 is a null string

that avoids a bunch of error type messages if you plod through the batch file with no parameter

Magnum

  • Member
  • *****
  • Posts: 2379
Re: Bat file for searching
« Reply #3 on: January 15, 2013, 03:22:36 AM »
Thanks gentlemen.

I will look it over.
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org