Page 1 of 1

using variables in "find

Posted: 14 Oct 2011 23:14
by Rileyh
Hi,
I am making a batch file that makes the user input a search term and then the batch file searches for the input (the variable) in a text file. Here is my code:

Code: Select all

[color=#FF0000]find /C /I "%b%" %drive%\DOS7\%user%\username.txt[/color]
if %errorlevel%==0 (goto :pass)
echo Incorrect Username.


The highlighted part is the part I am having trouble with. I posted the rest to help put the code in perspective for you.

Any help would be appreciated.
Rileyh

Re: using variables in "find

Posted: 14 Oct 2011 23:34
by Ed Dyreen
'

Code: Select all

@echo off

find /C /I "%b%" "%drive%\DOS7\%user%\username.txt" &&(
    goto :pass
) ||echo.Incorrect Username.

pause
exit /b 0
Goto is no Goto, we don't like to spaghetti things:

Code: Select all

@echo off

:main ()
::(
   find /C /I "%b%" "%drive%\DOS7\%user%\username.txt" &&(
       call :pass
   ) ||echo.Incorrect Username.

   pause
::)
exit /b %$error%

:pass ()
::(
   echo.you passed :^)
::)
exit /b %$error%
However, sometimes Goto is Good for breaking a loop:

Code: Select all

:BreakFor ()
::(
   set "$WorkFile="
   %_forC% (

      1, 1, 10

   ) do if defined $WorkFile (
      ::
      goto :BreakFor "()"

   ) else >nul 2>&1 reg query "!$ED.Path!" /v "$WorkFile.%%~!" ||(
      ::
      set /a $WorkFile = %%~!
   )
::)
:BreakFor ()

Re: using variables in "find

Posted: 19 Oct 2011 00:19
by Rileyh
Sorry Ed,
I am a novice so I did not understand what you were talking about. I have a limited understanding of the cmd commands that exist, and you have used commands that I do not understand.


Sorry again, but could you please use simpler commands or explain what the uncommon commands mean?

Thank you,
Rileyh

Re: using variables in "find

Posted: 19 Oct 2011 15:10
by Ed Dyreen
'
You should always quote your paths !, Maybe it will work if u change this:

Code: Select all

find /C /I "%b%" "%drive%\DOS7\%user%\username.txt"
and this:

Code: Select all

if %errorlevel% equ 0 goto :pass
What I was trying to explain in the previous post is that most programmers hate the word goto.
It's bad style, that's all.. And that it's usually replaced with call :label.