批处理文件以根据文件名的一部分移动文件,根据文件夹名称的一部分移动到文件夹 [英] Batch file to Move files based on part of filename, to folder based on part of folder name

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

问题描述

此处找到了pyhton解决方案,但我需要基于批处理文件的解决方案.

Found a pyhton solution here, but I need a batch file-based solution.

有很多文件:

  • SSP4325_blah-blah-blah.xml
  • JKP7645_blah.xml
  • YTG6457-blah-blah.xml
  • SSP4325_blah-blah-blah.xml
  • JKP7645_blah.xml
  • YTG6457-blah-blah.xml

以及包含一部分文件名的文件夹名称:

And folder names that contain a piece of the file name:

  • RefID-SSP4325,JKP7645,GHT1278,YRR0023
  • RefID-YTG6457
  • RefID - SSP4325, JKP7645, GHT1278, YRR0023
  • RefID - YTG6457

我正在寻找一个批处理解决方案,该解决方案将读取文件名的一部分在开头(在第一个破折号或下划线之前),然后将该文件移动到文件名开头在其中作为文件名一部分的文件夹中.文件夹名称.

I'm looking for a batch solution which would read a portion of the file name at the front (before either the first dash or underscore) and then move that file into the folder where the front of the filename exists as part of the folder name.

因此在上述示例中,前两个文件(SSP4325和JKP7645)被移入第一个文件夹,因为其中包含该文本作为文件夹名称的一部分.

So in the above examples, the first two files (SSP4325 and JKP7645) were moved into the first folder because it contained it contained that text as part of the folder name.

第三个文件将被移至第二个文件夹.

The third file would be moved into the second folder.

我有数百个文件和63个文件夹.因此,我希望能够实现自动化.

I have hundreds of files and 63 folders. So I'm hoping to be able to automate.

由于环境限制,无法使用Powershell或Python.因此希望有一种批处理文件方法.

Can't use Powershell or Python due to limitations of the environment. So hoping for a batch file approach.

谢谢.肖恩.

推荐答案

此解决方案仅复查一次文件夹并将它们存储在 array 中,因此此方法应运行得更快.

This solution review the folders just one time and store they in an array, so this method should run faster.

@echo off
setlocal EnableDelayedExpansion

rem Process the folders
set i=0
for /D %%a in (*) do (

   rem Store this folder in the next array element
   set /A i+=1
   set "folder[!i!]=%%a"

   rem Separate folder in parts and store the number of the array element in each one
   for %%b in (%%a) do set "part[%%b]=!i!"

)

rem Process the files
for %%a in (*.xml) do (

   rem Get the first part of name
   for /F "delims=-_" %%b in ("%%a") do (

      rem If such a folder exists...
      if defined part[%%b] (

         rem Get the number of the corresponding array element and move the file
         for %%n in (!part[%%b]!) do ECHO move "%%a" "!folder[%%n]!"

      ) else (

         echo No folder exists for this file: "%%a"

      )

   )
)

此方法还有几个优点:您可以检查某个文件夹是否不存在,或者获取移动到每个文件夹的文件数,等等.如果您对这些问题不感兴趣,只需删除if命令并使代码更简单...

This method have also several advantages: you may check if a certain folder does not exists, or get the number of files moved to each folder, etc. If you are not interested in these points, just remove the if command and make the code simpler...

有关批处理文件中阵列管理的说明,请参见

An explanation of array management in Batch files is given at this answer.

这篇关于批处理文件以根据文件名的一部分移动文件,根据文件夹名称的一部分移动到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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