cmd批量重命名子文件夹/文件 [英] cmd batch rename subfolders/files

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

问题描述

如何批量重命名包含子文件夹的文件夹中的文件和具有此格式名称filename_ex.doc的文件,并使它们看起来像filename.doc?

how can i batch rename the files from folder A which contains subfolders and files with this format name filename_ex.doc and make them look like filename.doc?

i一直试图这样做一段时间,这是一个史诗般的失败。请帮助。

i've been trying to do this for awhile now and it's been an epic fail. please help.

推荐答案

我假设你想重命名子文件夹中的文件,你的第二个评论中的方向。我猜你尝试了DIR / S选项的代码。但你是这么近:-)你只需要使用2个循环!

I presume you want to rename the files in the subfolders as well, which is where you had problems with the direction in your 2nd comment to Martin James. I'm guessing you tried that code with the DIR /S option. But you were so close :-) You just needed to use 2 loops!

编辑 - 固定代码输出看起来是正确的,放弃ECHO以使其正常工作。

Edit - fixed code Once the output looks correct, drop the ECHO to make it functional.

@echo off
for /r %%D in (.) do (
  pushd "%%~fD"
  for /f "tokens=1-3 delims=_." %%A in ('dir /b *_ex.ext 2^>nul') do echo ren "%%A_%%B.%%C" "%%A.%%C"
  popd
)

只要每个文件名没有任何 _ ,而不是 _ex.ext 中出现的内容。路径中的字符不应该有任何问题,只是文件名是一个问题。

The above will work as long as each file name does not have any _ or . other than what appears in _ex.ext. Characters in the path should not be any problem, just the file name is a concern.

这里是一个更健壮的解决方案,应该使用任何文件名(unicode名称除外) 。它也明显更快。它使用子字符串操作,并且您必须知道要从名称中剥离的字符数。在您的示例中,它是3个字符。

Here is a more robust solution that should work with any filename (except unicode names). It is also significantly faster. It uses a substring operation, and you must know the number of characters to strip from the name. In your example it is 3 characters. Again, remove ECHO once the resultant commands look correct.

@echo off
setlocal disableDelayedExpansion
for /f "delims=" %%F in ('dir /b /s *_ex.ext') do (
  set "old=%%F"
  set "new=%%~nF"
  setlocal enableDelayedExpansion
  echo ren "!old!" "!new:~0,-3!.ext"
  endlocal
)

这篇关于cmd批量重命名子文件夹/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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