用于重命名文件夹参数中的文件的批处理脚本

发布于 2024-10-07 10:53:47 字数 231 浏览 0 评论 0原文

我找不到任何参考资料来解释如何循环遍历作为参数传递的文件夹并重命名每个文件。我见过的所有示例都假设脚本在文件将被重命名的文件夹中运行,或者在循环中对文件夹路径进行硬编码。我该怎么做?这是我正在尝试做的事情的一个例子:

for /f %%a in (%1) do call :RenameFiles

:RenameFiles
Rename %%a "new filename"
Goto :EOF

I can't find any references explaining how to loop through a folder passed as an argument and rename each file. All examples I've seen assume the script is running in the folder where files will be renamed or hardcode the folder path in the loop. How can I do this? Here is an example of what I'm trying to do:

for /f %%a in (%1) do call :RenameFiles

:RenameFiles
Rename %%a "new filename"
Goto :EOF

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2024-10-14 10:53:47

以下代码将前缀“renamed-”添加到指定为命令行参数的目录中的每个文件:

for %%F in (%~1\*) do ren "%%~F" "renamed-%%~nxF"

编辑 无限循环问题的最简单解决方案是分两个阶段工作:

  1. 准备列表运营
  2. 执行计划。

-

set OpList=%TEMP%\%~n0%RANDOM%.bat
copy nul "%OpList%"
for %%F in (%~1\*) do echo ren "%%~F" "renamed-%%~nxF" >> "%OpList%"
call "%OpList%"
del "%OpList%"

The following adds prefix "renamed-" to every file in the directory specified as a command line argument:

for %%F in (%~1\*) do ren "%%~F" "renamed-%%~nxF"

EDIT The simplest solution to the problem of the infinite loop is to work in two stages:

  1. prepare the list of operations
  2. execute the plan.

-

set OpList=%TEMP%\%~n0%RANDOM%.bat
copy nul "%OpList%"
for %%F in (%~1\*) do echo ren "%%~F" "renamed-%%~nxF" >> "%OpList%"
call "%OpList%"
del "%OpList%"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文