我正在尝试制作一个隐藏文件夹,文件等的批处理脚本。但是,当文件名没有空格时,我只能让它工作。我列出了我到目前为止的代码。
我需要一些帮助才能让它与空间一起工作。请记住,我对批处理脚本有点新意,但对我正在做的事情非常了解。另外,请解释一下您的解决方案(如果有的话)。
>@echo off
title File hider
:start
cls
Echo Do You Want to Hide or Unhide a Folder?
echo.
echo.
set/p cho=Choice:
if %cho% equ Hide goto a
if %cho% equ hide goto a
if %cho% equ Unhide goto b
if %cho% equ unhide goto b
if %cho% neq Hide goto c
:c
cls
echo That Is An Invalid Choice
pause
goto start
:a
cls
echo Enter the name of the folder you want to hide:
echo.
echo.
set/p name=Folder Name:
if EXIST %name% goto file
if NOT EXIST %name% goto 99
:file
attrib +h +s +r %name%
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
echo Job Completed
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
exit
:b
cls
echo Enter The Name Of The Folder You Want To See:
echo.
echo.
set/p name=Folder Name:
if EXIST %name% goto bellow
if NOT EXIST %name% goto 98
:bellow
attrib -h -s -r %name%
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
echo Job Completed
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
goto question
:question
cls
echo Do you want to open this folder/file?
echo.
echo.
set/p yn=Answer[y:n]
if %yn% equ y goto 1
if %yn% equ n goto 2
if %yn% equ Y goto 1
if %yn% equ n goto 2
if %yn% neq y goto 3
:1
start %name%
exit
:2
exit
:3
cls
echo Invalid Choice
pause
goto question
exit
:99
cls
echo That file name does not exist
pause
goto a
:98
cls
echo that file name does not exist
pause
goto b
答案 0 :(得分:2)
你需要这样的双引号:
set/p "name=Folder Name: "
if EXIST "%name%" goto file
if NOT EXIST "%name%" goto 99
:file
attrib +h +s +r "%name%"
并且/i
使测试用例不敏感。
if /i "%cho%" equ "Hide" goto a
答案 1 :(得分:1)
只要您%var%
和var
包含空格,就需要使用"%var%"
。将字符串括在引号中意味着空格不会被解释为分隔符。如果您使用IF
语句,则必须在比较运算符的任一侧平衡引号。
答案 2 :(得分:0)
不完全确定这是否有用,但是当我询问用户是否也想运行另一个批处理文件时,这就是我在批处理文件中使用的内容(我有一个更新程序从服务器解压缩文件夹,另一个复制数据文件。所以我在每个结尾使用它来询问用户是否想要运行其他更新程序。)
它还会显示无效条目的警告。
set /p entry=INSERT INSTRUCTION HERE, Type OPTION(S), then press the enter key:
IF /I NOT %entry%==N IF /I %entry%==Y GOTO M1
IF /I NOT %entry%==Y IF /I %entry%==N GOTO M2
IF /I NOT %entry%==N IF /I NOT %entry%==Y GOTO M3
:M1
echo This is where 'Y' directs to.
pause >nul
GOTO END
:M2
echo This is where 'N' directs to.
pause >nul
GOTO END
::The below option is used to revert back to the BEG and is meant for any invalid entries, thus preventing inccorrect entry. This can also prevent the code from continuing until a valid entry is made.
::For a bit of visual fun, it is made to flash red when an ivalid entry is made. To remove, just delete the lines above the GOTO
:M3
cls
^
echo Please try again...^
color ca
ping 127.0.0.1 -n 2 -w 100 >nul
color 4a
ping 127.0.0.1 -n 2 -w 500 >nul
color ca
ping 127.0.0.1 -n 2 -w 500 >nul
color 4a
ping 127.0.0.1 -n 2 -w 500 >nul
color 0a
GOTO BEG
:END
答案 3 :(得分:0)
我注意到您编写的代码有些问题,所以我为您制作了更好的版本!
原始代码:
@echo off
title File hider
:start
cls
Echo Do You Want to Hide or Unhide a Folder?
echo.
echo.
set/p cho=Choice:
if %cho% equ Hide goto a
if %cho% equ hide goto a
if %cho% equ Unhide goto b
if %cho% equ unhide goto b
if %cho% neq Hide goto c
:c
cls
echo That Is An Invalid Choice
pause
goto start
:a
cls
echo Enter the name of the folder you want to hide:
echo.
echo.
set/p name=Folder Name:
if EXIST %name% goto file
if NOT EXIST %name% goto 99
:file
attrib +h +s +r %name%
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
echo Job Completed
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
exit
:b
cls
echo Enter The Name Of The Folder You Want To See:
echo.
echo.
set/p name=Folder Name:
if EXIST %name% goto bellow
if NOT EXIST %name% goto 98
:bellow
attrib -h -s -r %name%
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
echo Job Completed
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
goto question
:question
cls
echo Do you want to open this folder/file?
echo.
echo.
set/p yn=Answer[y:n]
if %yn% equ y goto 1
if %yn% equ n goto 2
if %yn% equ Y goto 1
if %yn% equ n goto 2
if %yn% neq y goto 3
:1
start %name%
exit
:2
exit
:3
cls
echo Invalid Choice
pause
goto question
exit
:99
cls
echo That file name does not exist
pause
goto a
:98
cls
echo that file name does not exist
pause
goto b
修改版本:
@echo off
title File hider
:start
cls
Echo Do You Want to Hide or Unhide a Folder?
echo.
echo.
set/p cho=Choice:
if %cho% equ Hide goto :a
if %cho% equ hide goto :a
if %cho% equ Unhide goto :b
if %cho% equ unhide goto :b
else goto :c
:c
cls
echo That Is An Invalid Choice
pause
goto start
:a
cls
echo Enter the name of the folder you want to hide:
echo.
echo.
set/p name=Folder Name:
if EXIST %name% goto :file
if NOT EXIST %name% goto :99
:file
attrib +h +s +r %name%
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
echo Job Completed
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
exit
:b
cls
echo Enter The Name Of The Folder You Want To See:
echo[
echo[
set/p name=Folder Name:
if EXIST %name% goto :bellow
if NOT EXIST %name% goto :98
:bellow
attrib -h -s -r %name%
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
echo Job Completed
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
ping localhost -5 > nul
goto :question
:question
cls
echo Do you want to open this folder/file?
echo.
echo.
set/p yn=Answer[y:n]
if %yn% equ y goto 1
if %yn% equ n exit
if %yn% equ Y goto 1
if %yn% equ n exit
else goto :3
:1
start %name%
:3
cls
echo Invalid Choice
pause
goto :question
:99
cls
echo That file name does not exist
pause
goto :a
:98
cls
echo that file name does not exist
pause
goto :b
希望能为你清除一些东西,如果代码有任何错误(这个XD中有很多行)请在下面告诉我们!