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

时间:2016-06-01 04:30:17

标签: batch-file

我有一个文件夹,里面有很多.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等移动时重命名副本。< / p>

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

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

  3. 我需要的是根据文件名“日期部分”的最后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
    

1 个答案:

答案 0 :(得分:0)

看起来任务可以通过修改

来实现
   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