批处理 ren 命令以保留文件扩展名并容纳带有“.”的文件名
Batch ren command to keep file extension and accommodating filenames with "."
我有一个重命名文件的脚本,其文件扩展名如下:
ren path\one.* two.*
ren path\three.* four.*
对于上面没有“.”的文件名来说效果很好。以它的名义,但是当我
有一个名称为例如“does.not.work”的文件。
使用脚本:
ren path\does.not.work.* test.*
它将重命名为“test.not.work.EXT”。
ren 命令是在别处自动生成的,用于重命名大量不同的文件。
所以手动将脚本更改为:
ren path\does.not.work.txt test.txt
不切实际。
我如何构造 ren 代码以容纳包含“.”的文件?
使用 for 循环:
@echo off
for %%i in (does.not.work.*) do ren "%%~i" "test%%~xi"
显然,您可以使用单个 for 循环来实现所有文件重命名,有几种使用 if
语句的方法,这意味着您将在 for 循环中设置变量,此处一种方法是利用 findstr
和生成的 errorlevel
根据它找到的内容重命名,您需要对其进行微调,因为搜索 su
也会匹配 sudo
所以请继续阅读 findstr /?
以了解如何使用正则表达式或单词边界来获得准确的搜索结果:
@echo off
for %%i in (*) do (
echo %%~ni | findstr "one" && ren "%%~i" "two%%~xi"
echo %%~ni | findstr "three" && ren "%%~i" "four%%~xi"
echo %%~ni | findstr "does.not.work" && ren "%%~i" "test%%~xi"
)
我有一个重命名文件的脚本,其文件扩展名如下:
ren path\one.* two.*
ren path\three.* four.*
对于上面没有“.”的文件名来说效果很好。以它的名义,但是当我 有一个名称为例如“does.not.work”的文件。 使用脚本:
ren path\does.not.work.* test.*
它将重命名为“test.not.work.EXT”。
ren 命令是在别处自动生成的,用于重命名大量不同的文件。 所以手动将脚本更改为:
ren path\does.not.work.txt test.txt
不切实际。 我如何构造 ren 代码以容纳包含“.”的文件?
使用 for 循环:
@echo off
for %%i in (does.not.work.*) do ren "%%~i" "test%%~xi"
显然,您可以使用单个 for 循环来实现所有文件重命名,有几种使用 if
语句的方法,这意味着您将在 for 循环中设置变量,此处一种方法是利用 findstr
和生成的 errorlevel
根据它找到的内容重命名,您需要对其进行微调,因为搜索 su
也会匹配 sudo
所以请继续阅读 findstr /?
以了解如何使用正则表达式或单词边界来获得准确的搜索结果:
@echo off
for %%i in (*) do (
echo %%~ni | findstr "one" && ren "%%~i" "two%%~xi"
echo %%~ni | findstr "three" && ren "%%~i" "four%%~xi"
echo %%~ni | findstr "does.not.work" && ren "%%~i" "test%%~xi"
)