【问题标题】:Move files to a folder based on part of the name using a bat使用 bat 根据部分名称将文件移动到文件夹
【发布时间】:2016-05-12 00:52:57
【问题描述】:

我在一个文件夹中有很多文件,我想将它们移动到与其名称的一部分匹配的文件夹中。

例子:

[PartOfItsName] Season01Episode02 (04.04.16).mp4
[PartOfAnotherName] Season02Episode02 (05.02.16).mp4
[AndAnotherOne] Season03Episode04 (02.01.16).mp4

After moved I want something like this:

C:/[PartOfItsName]/[PartOfItsName] Season01Episode02 (04.04.16).mp4
C:/[PartOfAnotherName]/[PartOfAnotherName] Season02Episode02 (05.02.16).mp4
C:/[AndAnotherOne]/[AndAnotherOne] Season03Episode04 (02.01.16).mp4

文件名不会被修改,只需要创建括号中的文件夹,然后将文件移动到各自的文件夹中。

【问题讨论】:

标签: file batch-file directory


【解决方案1】:

在 PowerShell 中,您可以这样做:

dir *.mp4 | mv -Dest { [regex]::Match($_, '\[.*\]').Value }

这可能在批处理文件中起作用:

dir /b *.mp4 > tmp
for /f "tokens=1,2 delims=]" %%f in (tmp) do (
    mkdir "%%f]"
    move "%%f]%%g" "%%f]"
)
del tmp

注意。对于这两个版本,每个 .mp4 文件都必须有一个 [..] 部分,并且名称中的其他任何地方都不能有方括号。

【讨论】:

  • 非常感谢,效果很好!你能解释一下它是如何工作的吗?
【解决方案2】:

即使在开头 [ 之前还有其他字符,此批处理脚本也能正常工作。

@echo off
for %%F in (*[*]*.mp4) do for /f "delims=[] eol=[ tokens=2" %%A in ("x%%F") do (
  md "c:\[%%A]" 2>nul
  move "%%F" "c:\[%%A]" >nul
)

它可以很容易地在命令行上作为一个非常长的单行来完成:

for %F in (*[*]*.mp4) do @for /f "delims=[] eol=[ tokens=2" %A in ("x%F") do @md "c:\[%A]" 2>nul&move "%F" "c:\[%A]" >nul

【讨论】:

  • 另外,谢谢,你的代码也很有魅力:D 你能解释一下它是如何工作的吗?
猜你喜欢
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多