help correcting this code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mohamad
Posts: 5
Joined: 16 Jun 2010 21:18

help correcting this code

#1 Post by mohamad » 16 Jun 2010 21:22

wassup people... i believe i got the functions here wrong... any tips to get it to do what its suppose to do

@echo off

:begin

cls

echo ^>^>^>^> Test Nisse's phone records ^<^<^<^<
echo.
echo ^<1^> search for phone numbers
echo ^<2^> enter a new phone number
echo ^<3^> remove phone number
echo ^<4^> Print records on screen
echo.
echo ^<x^> Exit

set /p val=What's your choice (1-4)

if %val% == 1 goto :type C:\records.txt
if %val% == 2 goto :edit C:\records.txt
if %val% == 3 goto :edit C:\records.txt
if %val% == 4 goto :type C:\records.txt
if %val% == x goto :exit
goto :begin

:type C:\records.txt
type C:\records.txt
pause
goto :begin

:edit C:\records.txt
edit C:\records.txt
pause
goto :begin

:edit C:\records.txt
edit C:\records.txt
goto :begin

:ps C:\records.txt
type C:\records.txt
pause
goto :begin

:exit
exit
goto :quit

:quit

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: help correcting this code

#2 Post by amel27 » 16 Jun 2010 22:56

depends on the task, for example:

Code: Select all

@echo off
:begin
cls

echo ^>^>^>^> Test Nisse's phone records ^<^<^<^<
echo.
echo ^<1^> Search for phone numbers
echo ^<2^> Enter a new phone number
echo ^<3^> Remove phone number
echo ^<4^> Print records on screen
echo.
echo ^<x^> Exit

set /p "val=What's your choice (1-4): "

if "%val%"=="1" call :sub1 "C:\records.txt"& goto :begin
if "%val%"=="2" call :sub2 "C:\records.txt"& goto :begin
if "%val%"=="3" call :sub3 "C:\records.txt"& goto :begin
if "%val%"=="4" call :sub4 "C:\records.txt"& goto :begin

if /i not "%val%"=="x" call goto begin
goto :quit

:sub1
type "%~1"
pause
goto :eof

:sub2
edit "%~1"
pause
goto :eof

:sub3
edit "%~1"
pause
goto :eof

:sub4
type "%~1"
pause
goto :eof

:quit
exit

mohamad
Posts: 5
Joined: 16 Jun 2010 21:18

Re: help correcting this code

#3 Post by mohamad » 17 Jun 2010 15:21

!!! i dont see how this is different from what i posted. ur doing the same thing i am which is wrong. what i need is for it to do what it says when 1-4 are entered

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: help correcting this code

#4 Post by aGerman » 17 Jun 2010 16:24

Try this

Code: Select all

@echo off
:begin
cls

echo ^>^>^>^> Test Nisse's phone records ^<^<^<^<
echo.
echo ^<1^> Search for phone numbers
echo ^<2^> Enter a new phone number
echo ^<3^> Remove phone number
echo ^<4^> Print records on screen
echo.
echo ^<x^> Exit

set /p "val=What's your choice (1-4): "

if "%val%"=="1" call :sub1 "C:\records.txt"& goto :begin
if "%val%"=="2" call :sub2 "C:\records.txt"& goto :begin
if "%val%"=="3" call :sub3 "C:\records.txt"& goto :begin
if "%val%"=="4" call :sub4 "C:\records.txt"& goto :begin

if /i not "%val%"=="x" call goto begin
goto :eof

:sub1
set "name="
set /p "name=Enter a name: "
if not defined name goto :eof
for /f "delims=: tokens=1*" %%i in ('findstr /i /c:"%name%" "%~1"') do echo Name: %%i  Number: %%j
pause
goto :eof

:sub2
set "name="
set /p "name=Enter the name: "
if not defined name goto :eof
set "number="
set /p "number=Enter the number: "
if not defined number goto :eof
>>"%~1" echo %name%:%number%
goto :eof

:sub3
set "name="
set /p "name=Enter the name: "
if not defined name goto :eof
move "%~1" "%temp%\tmp.txt"
for /f "delims=" %%a in ('findstr /i /v /c:"%name%" "%temp%\tmp.txt"') do >>"%~1" echo %%a
del "%temp%\tmp.txt"
goto :eof

:sub4
for /f "usebackq delims=: tokens=1*" %%i in ("%~1") do echo Name: %%i  Number: %%j
pause
goto :eof


Regards
aGerman

mohamad
Posts: 5
Joined: 16 Jun 2010 21:18

can someone correct it

#5 Post by mohamad » 25 Jun 2010 12:50

well this code is correct for some of the functions but it doesnt actually save/overwrite to the original file?!!!
can someone correct that

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: help correcting this code

#6 Post by aGerman » 25 Jun 2010 13:16

Which "original file" is meant? Names and numbers are searched/saved/removed/displayed from "C:\records.txt". What else should happen?
Sorry, but I've no idea what you mean. You should give some more verbose informations to us.

Regards
aGerman

mohamad
Posts: 5
Joined: 16 Jun 2010 21:18

Re: help correcting this code

#7 Post by mohamad » 25 Jun 2010 13:37

well im glad its you... its suppose to do exactly what you just said to the file records.txt... but when i actually ran it, it doesnt actuallt alter the file. after i run it and do some changes i open the original file and it still has the same info without any of the alterations!!! what am i doing wrong

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: help correcting this code

#8 Post by aGerman » 25 Jun 2010 13:59

Strange. It workes absolutely fine for me. So I don't know why it shouldn't work for you as well. Did you try exactly the same code?

mohamad
Posts: 5
Joined: 16 Jun 2010 21:18

Re: help correcting this code

#9 Post by mohamad » 26 Jun 2010 03:38

yes!am i not suppose to!

Post Reply