Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Rileyh
- Posts: 147
- Joined: 01 Sep 2011 03:54
- Location: Perth, Western Australia
#1
Post
by Rileyh » 14 Oct 2011 23:14
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
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#2
Post
by Ed Dyreen » 14 Oct 2011 23:34
'
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 ()
-
Rileyh
- Posts: 147
- Joined: 01 Sep 2011 03:54
- Location: Perth, Western Australia
#3
Post
by Rileyh » 19 Oct 2011 00:19
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
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#4
Post
by Ed Dyreen » 19 Oct 2011 15:10
'
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:
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.