【问题标题】:Move files into folders if any one word identical in name如果名称中的任何一个单词相同,则将文件移动到文件夹中
【发布时间】:2016-09-03 10:59:47
【问题描述】:

我想移动这些文件(扩展名无关紧要)

cane pazzo.txt
cane.torrent
incredibile cane s03.xx

进入文件夹

cane

我有这个代码

@echo off
setlocal enabledelayedexpansion
pushd %1
for /F "USEBACKQ tokens=*" %%a in (`dir /b /a:-d`) do (
    set "_file=%%a"
    for /D %%b in (*) do (
        if NOT "x!_file:%%b=!" == "x!_file!" (
            move %%a %%b
        )
    )
)
popd

但我只能移动与文件夹同名的文件

cane.torrent --> cane

取而代之的是这些文件

cane pazzo.txt
incredibile cane s03.xx

没有被移到里面,因为文件名中有多个单词。为什么??

.bat 脚本有什么解决方案吗?

【问题讨论】:

  • 在 PowerShell 中为foreach ($dir in gci -Directory) { gci | where Name -match $dir.Name | mv -Destination $dir }
  • 所以文件夹cane已经存在了,它的名字就是文件名中要搜索的词?
  • @aschipfl 是的,它是正确的..但我不知道我必须如何使用 powershell。我复制命令,但它告诉我无效..

标签: batch-file filenames directory


【解决方案1】:

你的逻辑是正确的。唯一的一点是move 命令应该将文件括在引号中,以便正确处理包含空格的名称:

move "%%a" "%%b"

我也会去掉比较中的“x”,因为没有必要:

if NOT "!_file:%%b=!" == "!_file!" (

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-16
    • 2023-01-08
    • 2023-03-17
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多