`!` operator different usages in batch script? and nested DelayedExpansions with slicing operator`~` issue

52 views Asked by At

my ffind.bat is simple script to search a file/dir in whole pc partitions via simple method: a search command dir "\*%toFind%*" /s that runs once for each partition using a for loop.

  • issue: although I used @echo off to suppress Commands not their o/p but the batch keeps printing its content to terminal only replacing the variables with seemingly their last iteration values. and keeps re printing couple of times!

  • note: this command was working previously but as a cmder alias i.e.(doskey) and only searches inside one drive the drive am at now in the terminal e.g.(c:) alias : ffind=dir "\*$**" /s

  • ffind.bat script:


@echo off
setlocal EnableDelayedExpansion
set oldpath=%cd%

set "toFind=%1"
set alphabet= !"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

@for /L %%a in (65,1,90) do (

   rem chr %%a > tmpFileChrBat.txt
   rem set /p crntDrive= < tmpFileChrBat.txt
   rem del tmpFileChrBat.txt 
   
   rem following 2 commands does almost what `chr.bat` was doing
   set /a tmp=%%a - 32
   set crntDrive=!alphabet:~!tmp!,1!
       
   if exist !crntDrive!: (
       
       cd /d !crntDrive!:
       
       dir "\*%toFind%*" /s
           
   )
)

cd /d %oldpath%
endlocal

EDIT : updated my script after reading those:

  1. How to set a variable inside a loop for /F
  2. Variables are not behaving as expected

also I don't call any other .bat from this batch script now to eliminate any other factors

now it stopped! it's not doing anything at all when I execute it and pass args to it

EDIT2 : script is now working after help in comments section!

here it is if you want to try/use it: https://ideone.com/OYzFJ6 (in echo format the symbol `` is just Esc char used for coloring echo o/p see

0

There are 0 answers