重命名使用批处理文件 [英] Rename files using batch

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

问题描述

我有与我只需要放下一切到左下划线并从这么下划线的我留下了blah.blah ho.hum hi.ho <文件x_blah.blah y_ho.hum z_hi.ho一个文件夹/ p>

解决方案

这应该做的伎俩:

  SETLOCAL enabledelayedexpansion对于%% i的(* _ *)做(
   设置OLD_NAME = %%我
   集NEW_NAME = OLD_NAME:* _ =!
   动!OLD_NAME! !NEW_NAME!

有关解释:


  • SETLOCAL enabledelayedexpansion 使所谓的延迟的变量扩充,该功能例如允许你解引用变量内循环

  • 为%% i的(* _ *)做在当前目录中的所有文件名,至少有一个开始一个循环_ 和文件名,分配给循环变量 %%我

  • 设置OLD_NAME = %%我分配循环变量的命名有规律的变量内容 OLD_NAME

  • 设置NEW_NAME = OLD_NAME:!* _ = 确实对变量的内容了一些不错的字符串替换 OLD_NAME ,第 _ _ 本身什么也没有取代之前的所有字符。结果存储在 NEW_NAME 。见SET命令的详细信息,帮助(类型帮助集在命令行上)。

  • 动!OLD_NAME! !NEW_NAME!最后是颁发给从旧的每个文件重命名为新名称的命令

更新:

要经过文件中的所有子文件可以使用的的 FOR / R 变化的循环。要开始在当前目录中,环头更改为类似:

 为/ R %% i的(* _ *)做(

但你还需要考虑到循环变量现在包含文件名的完全合格的路径,所以你也必须改变循环体了一下,只替换文件名:

 为/ R %% i的(* _ *)做(
   集FILE_PATH = %%〜DPI
   设置old_file_name = %%〜NXI
   集new_file_name = old_file_name:* _ =!
   动!FILE_PATH !! old_file_name! !FILE_PATH !! new_file_name!

希望有所帮助。

I have a folder with files x_blah.blah y_ho.hum z_hi.ho which I just need to drop everything to left of the underscore and the underscore from so I'm left with blah.blah ho.hum hi.ho

解决方案

This should do the trick:

setlocal enabledelayedexpansion

for %%i in (*_*) do (
   set old_name=%%i
   set new_name=!old_name:*_=!
   move "!old_name!" "!new_name!"
)

For explanation:

  • setlocal enabledelayedexpansion enables the so called delayed variable expansion, a feature that e.g. allows you to dereference variables inside loops
  • for %%i in (*_*) do starts a loop over all file names inside the current directory that at least have one _ and assigns that file name to the loop variable %%i
  • set old_name=%%i assigns the content of the loop variable to a regular variable named old_name
  • set new_name=!old_name:*_=! does some nice string substitution on the content of the variable old_name, replacing all characters before the first _ and the _ itself with nothing. The result is stored in new_name. See the help of the SET command for more details (type help set on the command line).
  • move "!old_name!" "!new_name!" finally is the command issued to rename each file from its old to its new name

Update:

To go through the files in all sub folders you can use the FOR /R variation of the for loop. To start in the current directory, change the loop header to something like:

for /r %%i in (*_*) do (

But you also need to take into account that the loop variable now contains the fully qualified path of the file name, so you also have to change the loop body a bit to only substitute the file name:

for /r %%i in (*_*) do (
   set file_path=%%~dpi
   set old_file_name=%%~nxi
   set new_file_name=!old_file_name:*_=!
   move "!file_path!!old_file_name!" "!file_path!!new_file_name!"
)

Hope that helps.

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

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