我们如何在batch file中使用当前目录path(%〜dp0)和'ren'命令?

我想重命名一个存在于子目录中的文件,文件名的已知部分是' .log '。 当我尝试下面的东西

ren subdirectory\*.log* file2.txt

要么

ren .\subdirectory\*.log* file2.txt

我得到错误: The syntax of the command is incorrect. 我在这里有两个问题:

  1. 我总是需要在ren命令中指定file1的完整path吗?

  2. 如果是的话,如果我不想硬编码的话,我怎么能给出完整的path?

我知道我们可以使用' %〜dp0 '来获取当前的目录path,但我不知道如何将它与'ren'一起使用。

添加引号将工作:

 ren "subdirectory\*.log*" file2.txt 

要么

 ren ".\subdirectory\*.log*" file2.txt 

谢谢wOxxOm !

如果你想使用相对路径在Windows中也是不正确的,请使用\来代替。

如果要搜索哪个相对路径是正确的,请使用命令dir

例:

 dir .\ dir ..\..\ dir \ dir . dir .. 

更新

ren subdirectory\*.log* file2.txt

你的重命名掩码似乎很危险

我不知道你是否可以用掩码通配符将许多文件重命名为相同的名称。 我认为它会覆盖每个文件到一个,或者至少它将连接成单个文件,但不知道。 如果您想查看重命名掩码的一些示例,请参阅: Windows RENAME命令如何解释通配符?

UPDATE

小批量看到你的面具在行动:

 @echo off md Somefolder echo some content into _file1.log>_file1.log echo some content into _file2.log>_file2.log echo some content into _file3.log>_file3.log echo some content into _file999.log>_file999.log dir Somefolder pause>NUL REM Correct ren Somefolder\_file???.log _file???.txt dir Somefolder pause>NUL REM Incorrect because it override _file???.txt into one _file1.log ren Somefolder\_file???.txt _file1.log dir Somefolder pause