@echo off
set tempfile=C:\Users\*\Desktop\test case\temp\tempfile.txt
if not exist %tempfile% (
echo if branch
echo %TIME%
type nul>%tempfile%
) else (
echo else branch
echo %TIME%
echo file has already exist!
echo =============%DATE% %TIME%==========>>%tempfile%
echo 123>>%tempfile%
)
答案1
我建议你养成以下习惯:
- 环境
%userprofile%
变量而不是c:\users\%username%\*\...
- 进一步阅读如何在 Windows 中打开用户配置文件夹
"%tempfile%"
由于您的文件包含空格,因此请为该变量添加双引号。
因此你的代码可以这样写:
@echo off
set tempfile=%userprofile%\Desktop\test case\temp\tempfile.txt
if not exist "%tempfile%" (
echo if branch
echo %TIME%
type nul>"%tempfile%"
) else (
echo else branch
echo %TIME%
echo file has already exist!
echo =============%DATE% %TIME%==========>>"%tempfile%"
echo 123>>"%tempfile%"
)
pause>nul