【问题标题】:How to move files to a new folder or subfolders based on parts of the file name?如何根据部分文件名将文件移动到新文件夹或子文件夹?
【发布时间】:2016-06-02 13:50:45
【问题描述】:

我有一个存放大量 .tif 文件的文件夹。

它们都以MW_SW_ 开头,后来还有NSSW。所以我需要能够从后面的前两个扩展到NSSW

我已经有一个批处理文件,它首先根据文件名的前 2 个字符将文件移动到文件夹 MWSW。这是我当前的批处理文件,它工作得很好。但我认为我需要第二个批处理文件或添加此文件来执行以下 1 和 2 步骤。请看下面,在这段代码之后。

REM Sort by First name.
REM This script creates a folder for either the full file name,
REM or if it contains an underscore, the part before the underscore.

REM TODO - Don't copy over existing files.
REM TODO - Move files into Sub folders based on Date in file name "last 8 characters .tif

@echo off
REM Needed because you are working with variables that are immediately called
setlocal enabledelayedexpansion

REM Start of the loop to get all files with a psd or jpg Extension
for %%A in (*.tif *.jpg *.pdf) do (
   echo file found  %%A

   REM Grabs only the file name
   for /f "delims=" %%B in ("%%A") do set fname=%%~nB
   REM Grabs only the extension
   for /f "delims=" %%C in ("%%A") do set fextn=%%~xC

   REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
   for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
   echo folder name !folname!

   REM Checks for the existence of the folder, if the folder does not exist it creates the folder
   if not exist "!folname!" (
      echo Folder !folname! does not exist, creating
      md "!folname!"
   ) else (
      echo Folder !folname! exists
   )

   REM Moves the file to the folder
   echo Moving file %%A to folder !folname!
   REM   if not exist "%%D\%%A" move "%%A" "!folname!"
   if not exist "%%D\%%A" copy "%%A" "!folname!"

   REM  add the date DDMMYYYY to the end of each file. Name can be 80 characters long.
   rem ren "!folname!\%%A" "????????????????????????????????????????????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.tif"
)

echo Finished
pause

所以我猜这就是我需要的。执行以下操作的第二个或第三个批处理文件。我希望,有人可以提供帮助。

注意:请记住,如果文件存在,它会在移动时重命名副本,文件名末尾带有xxxx(1).tifxxx(2).tif 等。

  1. 根据文件名从文件名的第 4 个到第 10 个字符或从第 4 个字符到第二个或下一个“_”,将现在列在 MW 文件夹中的文件移动到新的或现有的子文件夹中.

  2. 根据文件名的最后8个字符将命名文件夹子文件夹中的文件移动到子日期命名文件夹中。

然后我需要根据文件名“日期部分”的最后 7 个字符将文件从 MW 文件夹移动到新的或现有的子文件夹中。

例如,我们以 MW 文件开头
进入文件夹C:\temp\Test_Moving_Files\的文件

MW_VRL5VF10000_6542234_01052016.TIF 
MW_Flybuys_677888_01052016.TIF 
MW_VRL5VF10000_333443_02052016.TIF
MW_Flybuys_555555_02052016.TIF
MW_goodguys_534535_02052016.TIF
MW_goodguys_222222_02052016.TIF
MW_Flybuys_123443_03052016.TIF
MW_Flybuys_3545555_03052016.TIF
MW_goodguys_444444_03052016.TIF
MW_goodguys_888888_03052016.TIF

子文件夹的输出应按如下子文件夹排序:

MW\VRL5VF10000\01052016\MW_VRL5VF10000_6542234_01052016.TIF
MW\VRL5VF10000\02052016\MW_VRL5VF10000_333443_02052016.TIF

MW\Flybuys\01052016\MW_Flybuys_677888_01052016.TIF
MW\Flybuys\02052016\MW_Flybuys_555555_02052016.TIF
MW\Flybuys\03052016\MW_Flybuys_123443_03052016.TIF
MW\Flybuys\03052016\MW_Flybuys_3545555_03052016.TIF

MW\goodguys\01052016\MW_goodguys_222222_02052016.TIF
MW\goodguys\02052016\MW_goodguys_534535_02052016.TIF
MW\goodguys\03052016\MW_goodguys_444444_03052016.TIF
MW\goodguys\03052016\MW_goodguys_888888_03052016.TIF

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    看来任务可以通过修改来完成

       REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
       for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
       echo folder name !folname!
    
       REM Checks for the existence of the folder, if the folder does not exist it creates the folder
       if not exist "!folname!" (
          echo Folder !folname! does not exist, creating
          md "!folname!"
       ) else (
          echo Folder !folname! exists
       )
    

       REM Using the file name it separates it into 4 parts using "_" as a delimiter.
       for /f "tokens=1-4 delims=_" %%D in ("!fname!") do set "folname=%%D\%%E\%%G"
       echo Folder name !folname!
    
       REM Checks for the existence of the folder path. If the folder
       REM path does not exist, it creates the complete folder structure.
       if not exist "!folname!\*" (
          echo Folder !folname! does not exist, creating ...
          md "!folname!"
       ) else (
          echo Folder !folname! exists.
       )
    

    上面的代码现在不再使用下划线分隔文件名的第一个子字符串,而是使用第一个、第二个和第四个子字符串。我没有测试!

    命令MD 也可以一次创建多个文件夹,因此变量folname 也可以是文件夹路径字符串。好吧,我建议将批处理代码中的变量folname 重命名为FolderPath

    当然,您必须在批处理脚本的其余部分使用folname 或更好的FolderPath,而不是%%D

    【讨论】: