重命名和批量-DOS移动文件 [英] Rename and move files in batch-dos

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

问题描述

欲重命名文件和使用批处理文件以下面的方式将它们转移到库

I want to rename files and transfer them into a library using a batch file in the following way:

c:\jg_Folder_xy>
blabla01_000.bla --> blabla01_001.bla
blabla02_000.bla --> blabla02_001.bla
blabla03_000.bla --> blabla03_001.bla

c:\sj_Folder_mx>
blabla01_000.bla --> blabla01_002.bla
blabla02_000.bla --> blabla02_002.bla
blabla03_000.bla --> blabla03_002.bla

c:\an_Folder_kj>
blabla01_000.bla --> blabla01_003.bla
blabla02_000.bla --> blabla02_003.bla
blabla03_000.bla --> blabla03_003.bla

和比全部转移到一个新的文件夹

and than transfer all into a new folder

c:\New_Folder>
blabla01_001.bla
blabla01_002.bla
blabla01_003.bla
blabla02_001.bla
blabla02_002.bla
blabla02_003.bla
blabla03_001.bla
blabla03_002.bla
blabla03_003.bla

有人知道什么是最有效的方法是什么?

anyone knows what is the most efficient way?

请注意:文件夹的名字都只有一个共同的子字符串

NOTE: folders name have only one sub string in common

推荐答案

下面是分批一个简单的解决方案:

How to do it

Here's a simple solution in batch:

@echo off
setlocal enabledelayedexpansion
set "SRC_PATH=c:\jg_Folder_xy"
set "DEST_PATH=c:\New_Folder"
for %%f in ("%SRC_PATH%"\*.bla) do (
    set fname=%%~nf
    set fbase=!fname:~0,-4!
    set findex=!fbase:~-2!
    move %%f "%DEST_PATH%"\!fbase!_0!findex!%%~xf
)

注意:此脚本要对每一个源文件夹分别运行(改变 SRC_PATH 的每一次)。或者,您可以修改循环双环,像这样:

Note: this script has to be run on each source folder separately (changing SRC_PATH each time). Alternatively, you can modify the for loop to a double loop, like so:

for /d %%d in (*_Folder_*) do for %%f in ("%%d"\*.bla) do (

,以便它会自动在所有文件中的所有所需的文件夹循环(即 SRC_PATH 线将不再需要)。

so that it'll iterate automatically over all files in all desired folders (the SRC_PATH line won't be needed).

在源文件夹中的文件的循环迭代,并指定文件名(带完整路径) %%˚F在每个迭代。

The for loop iterates over the files in the source folder, and assigns the filename (with the full path) to %%f in each iteration.

设置FNAME = %%〜NF 提取文件名和下降的扩展。

set fname=%%~nf extracts the filename and drops the extension.

设置f基准= FNAME:!〜0,-4 获得所有,但最后4个字符(的滴_000) 。

这实质上提取基本文件名,这是blabla01,blabla02......在你的例子。

set fbase=!fname:~0,-4! obtains all but the last 4 characters (i.e. drops the "_000").
This essentially extracts the base filename, which is "blabla01", "blabla02"... in your example.

设置findex = f基准:!〜-2 提取从 f基准,即最后2个字符的运行指标。

假定运行索引正好两个字符。

set findex=!fbase:~-2! extracts the last 2 characters from the fbase, i.e. the running index.
It is assumed that the running index is exactly two characters.

移动命令重命名文件并将其移动到目标文件夹。

The move command renames the files and moves them into the destination folder.

希望这有助于。

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

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