批量复制/移动文件到同名文件夹 [英] batch copy/move files to folders with same name

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

问题描述

我每个月都会生成一堆.xlsx文件.我希望能够将文件批量移动到名称基本相同的文件夹中.

I have a bunch of .xlsx files that are generated every month. I would like to be able to batch move the files to folders which have basically the same name.

示例:文件为123456 Action.xlsx,123456 RC.xlsx,123456 PF.xlsx.该文件夹将是123456 Random Center.

Example: 123456 Action.xlsx, 123456 RC.xlsx, 123456 PF.xlsx would be the files. The folder would be 123456 Random Center.

是否可以使用批处理命令或其他通过命令提示符将文件移动到该文件夹​​的方法?

Is there a way to move those files to that folder using a batch command or something else through the command prompt?

这是我一直尝试使用/修改的代码.

Here is the the code I have been trying to use/modify.

@echo off
pushd "C:\New folder"
rem Process all files in this folder separating the names at " "
for /F "tokens=1* delims=-" %%a in ('dir /B .xlsx') do (
   rem At this point %%a have the name before the " " and %%b the rest after " "
   rem Create the folder, if not exists
   if not exist "%%a" md "%%a"
   rem Move the file there
   move "%%a-%%b" "%%a"
)
popd

这将创建一个名为%% a的文件夹,但不添加任何内容.我被困住了,需要一些帮助.

That creates a folder named %%a but puts nothing in it. I'm stuck and need some help.

推荐答案

首先,欢迎您使用Stack Overflow

First of all, welcome to Stack Overflow

在您提供的代码中,您尝试使用dir的输出循环遍历文件,并立即使用空格将其拆分.取而代之的是,您应该使用for循环来循环浏览所有以* .xlsx结尾的文件,然后在空格之前和之后对其进行制动.

In the code you provided you try to loop through files using the output of dir, and immediately split that up using spaces. Instead of this, you should use a for loop to loop through all files ending with *.xlsx first, and then brake that up in before and after the space.

尝试一下:

@echo off
pushd "C:\New folder"
FOR %%G IN (*.xlsx) DO (
  FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
    if not exist "%%a Random Center" md "%%a Random Center"
    move "%%G" "%%a Random Center"
  )
)
popd
pause

在此代码中,我首先循环遍历所有以xlsx结尾的文件,方法是循环遍历 xlsx(是通配符)而没有/开关.之后,我用/F开关将%% G(文件名作为字符串)循环.

In this code I first loop through all files ending with xlsx, by looping through xlsx ( is a wildcard) without a / switch. After that I loop through %%G (wchich are the filenames) as string using the /F switch.

请注意,您尝试使用-作为分隔符,而不是.您在移动命令中犯了同样的错误.如果文件使用-而不是,则也应在我的代码中更改定界符.

Note that you're trying to use the - as a delimiter, instead of . You make the same error in your move command. If the files use a - instead of a , you should change the delimiter in my code as well.

这将检查是否存在一个以与文件相同的词开头并将其移动到其中的文件夹:

This looks if there is a folder which starts with the same word as the files and moves them there:

@echo off
setlocal EnableDelayedExpansion
pushd "C:\New folder"
FOR %%G IN (*.xlsx) DO (
  FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
    set "outFolder=%%a Random Center"
    for /D %%i in (*.*) do (
      for /F "tokens=1 delims= " %%b IN ("%%i") do (
        if "%%a"=="%%b" set "outFolder=%%i"
      )
    )
    if not exist "!outfolder!" md "!outfolder!"
    move "%%G" "!outfolder!"
  )
)
popd
pause

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

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