查看文件是否存在并使用 Windows 命令行重命名

Ale*_*ex 1 windows shell command-line cmd if-statement

我花了很多时间试图弄清楚这一点,但我感到非常沮丧。

我想要做的就是查看文件是否存在(确实存在),然后重命名它。我在 Windows 10 的命令行中工作。

IF exist C:\content\info.txt (
    ren "C:\content\info.txt" "C:\content\info_new.txt"
) ELSE (
    echo "Couldn't find file."
)
Run Code Online (Sandbox Code Playgroud)

我不断收到错误“命令的语法不正确”。我知道该文件存在,因为我之前运行了以下内容。

if exist C:\content\info.txt echo "info.txt is in C:\content\info.txt."
Run Code Online (Sandbox Code Playgroud)

我将所有这些直接输入到命令提示符中,而不是从 .bat 文件中调用它。我在这里错过了什么吗?任何帮助,将不胜感激。

Sub*_*baz 5

您不必在重命名文件时提供完整位置。只需这样做:

 IF exist C:\content\info.txt (
  ren "C:\content\info.txt" "info_new.txt"
 ) ELSE (
 echo "Couldn't find file."
)
Run Code Online (Sandbox Code Playgroud)