0

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

orsALSAQF
  • 3
  • 6
  • What happens if you copy this file to a new name and then execute that named file instead of with the doskey alias? I've never seen behavior you're describing in almost 40 years of writing batch files; I'm wondering if the alias is executing a different copy of the file? – Br.Bill Jul 26 '23 at 23:35
  • `chr` isn't a valid batch command, so I'm curious to know how this is running at all. – SomethingDark Jul 26 '23 at 23:55
  • @SomethingDark this is one of my main concerns that may cause the issue an not expert in batch script but yes chr is a usermade batch file to converte ascii code to its char symbol `(65~90) -> (A~Z)` for mor on chr : https://helloacm.com/the-chr-function-implementation-in-windows-pure-batch-script/ – orsALSAQF Jul 27 '23 at 00:22
  • @Br.Bill am not using the alias to execute it.I normally do that after making sure the new `.bat` is working ok. but now I just execute it directly by saving a path in `env. vars.` to a folder i.e.(`bin`) holding most of my `.bat` scripts then just type in terminal `ffind.bat`. also am sure there is no other copy. cuz changes in `ffind.bat` is visible when re-running it you can. try run it. here is img of portion of the o/p ( I tried it via normal cmd terminal and cmder. both through windows terminal preview app) o/p img: https://imgur.com/jbRuFo0 – orsALSAQF Jul 27 '23 at 00:28
  • i have edited my code and replaced `chr` batch file also : thanks to https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected https://stackoverflow.com/questions/13805187/how-to-set-a-variable-inside-a-loop-for-f now @echo off works again but still my `ffind.batch` not running – orsALSAQF Jul 27 '23 at 01:11
  • You're setting `tmp` but then using `var` in the subsequent substring command. Also, you'd need to use delayed expansion in order to actually use that value, which would require an additional `for` loop. On a related note, unless you're hiding other parts of the script from us, `%alphabet%` is *FAR* too long and you only need `ABCDEFGHIJKLMNOPQRSTUVWXYZ` so you can get rid of the math entirely and have `for /L` go from 0 to 25 instead. – SomethingDark Jul 27 '23 at 01:24
  • edited the `tmp` and `var` and I will try to apply what you said and then see. thanks! no that almost all of the code. I only deleted echo and comment statements! but I tried to merge what was inside `chr.bat` which convert ascii to char as I said . also would you elaborate more on the additional for loop that I need? – orsALSAQF Jul 27 '23 at 01:32
  • `for /f %%b in ("!var!") do set crntDrive=!alphabet:~%%b,1!` but again, `%alphabet%` is **FAR** too long and the `)` is most likely what is breaking your code in the first place. Or the `!`, the `"`, the `&`, the `>`, the `=`, or the `<`. My point is that you should only be iterating over letters. – SomethingDark Jul 27 '23 at 01:35
  • I see what you meant now I'll try fix it locally then update question ! – orsALSAQF Jul 27 '23 at 01:39
  • it worked ! thanks alot @SomethingDark , since am new to stackoverflow i will change the title cuz old one now doesnt relate to the question but do i remove the old script in question and but the edited working one? or leave it – orsALSAQF Jul 27 '23 at 01:49
  • The question has technically been closed for a while, so you can leave the question the way it was if you'd like. – SomethingDark Jul 27 '23 at 02:03
  • Is this not what you're trying to do? Replace everything after **line 2.** of your code with these two lines: **new line 3.** ```For /L %%G In (65 1 90) Do (%SystemRoot%\System32\cmd.exe /D /C Exit /B %%G```, **new line 4.** ```PushD "!=ExitCodeAscii!:\" 2>NUL && (Dir "*%~1*" /A /S & PopD))```. – Compo Jul 27 '23 at 03:03
  • @Compo for sure that's a more clever approach thanks for the code! this `PushD` and stack usage is nice idea. I've added '/a' switch my code script it's already working here if you want to check it. only added small formatting and coloring : https://ideone.com/OYzFJ6 btw I was using batch long time only as some commands in cmder terminal but lately i started using more complex doskeys then started making .bat scripts ... I still don't know the internal and didnt study it as a whole language only as windows controlling commands via CLI . – orsALSAQF Jul 28 '23 at 00:57
  • @Compo but very lately am really into learning it fully and decided to give it time and respect it deserved by studying this small playlist for basics https://www.youtube.com/playlist?list=PL8WTXLSrtyWrQ5Tl50zkTeRc3bATyHRbX would really appreciate any recommendations or sources to grasp basics not in command way but like learning internals , datatypes, looping , branching , methods etc... (I mainly code in C/C++/python/PHP) – orsALSAQF Jul 28 '23 at 00:58

0 Answers0