繁体   English   中英

我们如何在批处理文件中使用带有“ren”命令的当前目录路径(%~dp0)?

[英]How can we use the current directory path(%~dp0) with 'ren' command in a batch file?

我想重命名子目录中存在的文件,文件名的已知部分是“ .log ”。 当我尝试类似下面的东西时

ren subdirectory\\*.log* file2.txt

或者

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

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

  1. 我是否总是需要在ren命令中为 file1 提供完整路径?

  2. 如果是,那么如果我不想对其进行硬编码,我们如何提供完整路径?

我知道我们可以使用 ' %~dp0 ' 来获取当前目录路径,但我不确定如何将它与 'ren' 一起使用。

如果您想在 Windows 中也使用相对路径/不正确,请改用\\

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

例子:

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

更新

ren subdirectory\\*.log* file2.txt

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

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

更新

小批量查看您的面罩效果:

@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

添加引号将起作用:

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

或者

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

谢谢wOxxOm

使用相对路径删除所有文件 *.log 的扩展名

rename ".\folder1\subfolder1\*.log" *.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM