批处理文件无法创建txt文件

时间:2016-01-25 16:53:43

标签: batch-file if-statement

我似乎无法想出这个,这是一个相当简单的脚本。我的if语句没有检查文件是否存在,而else命令没有在appdata目录中创建文件。

reg.exe工作正常,已添加密钥。

任何指导都会很棒。

@echo
IF EXIST "%APPDATA%\outlookclean.txt" (exit
) 
else 
( 
% reg.exe add HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\OutlookTest
@echo Done > "%APPDATA%\outlookclean.text"
)

2 个答案:

答案 0 :(得分:0)

语法非常具体。 else及其前后括号必须与空格位于同一物理行上。

也适用于exit之前的括号......

@echo
IF EXIST "%APPDATA%\outlookclean.txt" ( exit
) else ( 
 % reg.exe add HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\OutlookTest
 @echo Done > "%APPDATA%\outlookclean.text"
)

不确定为什么reg.exe行开始% - 不应该在那里。

答案 1 :(得分:0)

使用旧的 help if 可以强烈显示您的代码可能出现的问题:

根据已发现的文档,else命令必须if命令在同一行,因为批处理是一种糟糕的语言,因此您的代码应该看起来像这样:

@echo off REM did you want to turn the output off?
IF EXIST "%APPDATA%\outlookclean.txt" (exit
) else (
REM notice the else being between the parenthesis? yeah, batch is terrible
REM also whats the deal with the % at th start of the next line?
reg.exe add HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\OutlookTest
@echo Done > "%APPDATA%\outlookclean.text"
)
相关问题