批处理脚本来重命名文件夹中的文件的说法 [英] Batch script to rename files in folder argument

查看:262
本文介绍了批处理脚本来重命名文件夹中的文件的说法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到通过作为参数传递的文件夹解释如何循环的任何引用和重命名每个文件。我见过的所有示例假设脚本文件夹中运行的位置的文件将被重新命名或硬code回路中的文件夹路径。我怎样才能做到这一点?这里是什么我试图做一个例子:

  FOR / F %%一中(%1)不要拨打:RenameFiles:RenameFiles
重命名%%一个新文件名
转到:EOF


解决方案

下面增加了preFIXrenamed-在指定作为命令行参数的目录,每个文件:

 为%% f由于(%〜1 \\ *)不仁%%〜F,更名 -  %%〜NXF

修改以无限循环的问题,最简单的办法是分两个阶段的工作:


  1. prepare操作的列表

  2. 执行计划。

-

 设置OPLIST =%TEMP%\\%〜N0%RANDOM%蝙蝠
复制NUL%OPLIST%
为%% f由于(%〜1 \\ *)做回声仁%%〜F,更名 - %%〜NXF>> %OPLIST%
所谓的%OPLIST%
德尔%OPLIST%

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

解决方案

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%"

这篇关于批处理脚本来重命名文件夹中的文件的说法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆