我一直在编写一个记录电话号码的小脚本,但它运行正常,但是我遇到了删除功能的问题,我得到了错误:FINDSTR:无法打开del和FINDSTR:无法打开/ s。
@echo off
:menu
cls
echo ^<^<^<^<^< Test Nisse's phone records ^>^>^>^>^>
echo ^<1^>. Search for phone number
echo ^<2^>. Enter a new phone number
echo ^<3^>. Remove phone number
echo ^<4^>. Print all of the telephone record on the screen
echo ^<x^>. Exit
set /p val="Enter your selection <1-4, x>: "
if %val%== 1 goto one
if %val%== 2 goto two
if %val%== 3 goto three
if %val%== 4 goto four
if %val%== x goto x
:one
cls
set /p n1="Number: "
findstr /r /c:%n1% telephoneregister.txt
echo.
echo ====================
SET /p =ENTER to go back to menu.
goto menu
:two
cls
set /p p1="Number: "
echo %p1% >> telephoneregister.txt
echo.
echo ====================
set /p =ENTER to go back to menu.
goto menu
:three
cls
echo Which number would you like to delete?
set /p num="Telephoneregister: "
type telephoneregister.txt | findstr /v %num% > telephoneregister1.txt del /s telephoneregister.txt
type telephoneregister1.txt > tele.txt del /s tele1.txt
set /p =ENTER to go back to menu.
goto menu
:four
cls
type telephoneregister.txt
echo.
echo ====================
set /p =ENTER to go back to menu.
goto menu
:x
exit
答案 0 :(得分:2)
在:二,你在每个电话号码后写一个空格:
echo %p1% >> telephoneregister.txt
将其更改为:
echo %p1%>> telephoneregister.txt
更改:三:到:
:three
cls
echo Which number would you like to delete?
set /p num="Telephoneregister: "
type telephoneregister1.txt | findstr /v %num% telephoneregister.txt > telephoneregister1.txt
copy /y telephoneregister1.txt telephoneregister.txt > nul
del telephoneregister1.txt
set /p =ENTER to go back to menu.
goto menu
“findstr / v”部分会将与该数字不匹配的所有行写入备份文件(telephoneregister1.txt)。然后我们将该文件复制回telephoneregister.txt(覆盖它)并删除备份文件。
答案 1 :(得分:1)
单行上的命令之间需要&
,否则cmd
会假设第二个命令是第一个命令的额外参数。
type telephoneregister.txt | findstr /v %num% > telephoneregister1.txt del /s telephoneregister.txt
应该成为
type telephoneregister.txt | findstr /v %num% > telephoneregister1.txt&del /s telephoneregister.txt
甚至更容易
type telephoneregister.txt | findstr /v %num% > telephoneregister1.txt
del /s telephoneregister.txt
或者,更容易
type telephoneregister.txt | findstr /v %num% > telephoneregister1.txt
move /y telephoneregister1.txt telephoneregister.txt >nul
其中move
命令将使用源覆盖目标文件。 /y
表示不会提示您覆盖&#34;并且1 file(s) moved
消息被重定向到nul
答案 2 :(得分:0)
我稍微修改了您的代码,以便解决一些细微的问题,或者使其变得更简单。这是:
@echo off
:menu
cls
echo ^<^<^<^<^< Test Nisse's phone records ^>^>^>^>^>
echo ^<1^>. Search for phone number
echo ^<2^>. Enter a new phone number
echo ^<3^>. Remove phone number
echo ^<4^>. Print all of the telephone record on the screen
echo ^<x^>. Exit
choice /C 1234x /M "Enter your selection: "
if %errorlevel% equ 5 exit /B
cls
call :option-%errorlevel%
echo.
echo ====================
SET /p =ENTER to go back to menu.
goto menu
:option-1 Search
set /p n1="Number: "
findstr /i /r /c:%n1% telephoneregister.txt
exit /B
:option-2 Enter
set /p p1="Number: "
set /P "=%p1%" < NUL >> telephoneregister.txt
echo.>> telephoneregister.txt
exit /B
:option-3 Remove
echo Which number would you like to delete?
set /p num="Telephoneregister: "
findstr /v %num% telephoneregister.txt > telephoneregister1.txt
move /Y telephoneregister1.txt telephoneregister.txt
exit /B
:option-4 Print
more telephoneregister.txt
exit /B
顺便说一句,您也可以使用完全相同的代码管理电话号码后的姓名;只需“输入数字后跟名称”和“输入要搜索的数字或名称”。