Batch file 如何根据部分文件名将文件移动到新文件夹或子文件夹?

Batch file 如何根据部分文件名将文件移动到新文件夹或子文件夹?,batch-file,Batch File,我有一个文件夹,里面有很多.tif文件 它们都是以MW\uw或SW\uw开始的,之后也是NSSW。因此,我需要能够在以后从前两个扩展到NSSW 我已经有一个批处理文件,它首先根据文件名的前两个字符将文件移动到文件夹MW或SW。这是我当前的批处理文件,它工作得很好。但是我想我需要第二个批处理文件或者添加到这个文件中来完成下面的1&2个步骤。请参见以下代码后面的内容 REM Sort by First name. REM This script creates a folder for either

我有一个文件夹,里面有很多.tif文件

它们都是以
MW\uw
SW\uw
开始的,之后也是
NSSW
。因此,我需要能够在以后从前两个扩展到
NSSW

我已经有一个批处理文件,它首先根据文件名的前两个字符将文件移动到文件夹
MW
SW
。这是我当前的批处理文件,它工作得很好。但是我想我需要第二个批处理文件或者添加到这个文件中来完成下面的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).tif
xxx(2).tif
等重命名副本

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

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

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

    例如,我们从
    MW
    文件开始
    文件进入文件夹
    C:\temp\Test\u Moving\u 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
    

    看起来这项任务可以通过修改

       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
       )
    

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

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


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

    ,看起来任务可以通过修改

       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
       )
    

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

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

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