Windows批处理文件 - 按修改和/或创建日期将文件排序到文件夹中

时间:2016-06-02 01:33:07

标签: windows file date batch-file directory

我尝试调整StackOverflow上发布的现有批处理文件以完成我的任务,但需要有关批处理文件语法和命令的特定区域的指导。

任务是通过创建和/或修改日期(以较早者为准)将装满图像文件的文件夹分类到子文件夹中。

以下脚本适用于文件创建日期,但是,如果已从另一个文件夹中复制了一组文件,则它们都具有相同的创建日期(但保留较早的'已修改的日期,因此,为什么我希望使用最早的日期)

感激地收到任何建议。

我一直在寻找适应的代码:

    @echo off
    setlocal enabledelayedexpansion
    set RawData="C:\Users\Jernau\source_image\*.jpg"
    set ArchiveFolder="C:\Users\Jernau\source_image\organised\"
    for %%a in ("%RawData%") do (
        echo Processing %%~nxa ...
        set File=%%~fa
        for /f "tokens=1* delims=," %%a in ('wmic datafile where "name=
            '!File:\=\\!'" get 'CreationDate' /format:csv ^| find /i  
            "%ComputerName%"') do (set CreationDate=%%b)
        echo %%~nxa: !CreationDate!
        set cYear=!CreationDate:~0,4!
        set cMonth=!CreationDate:~4,2!
        set cDay=!CreationDate:~6,2!
        set TimeStamp=!cYear!-!cMonth!-!cDay!
        if not exist "%ArchiveFolder%\!TimeStamp!" (
            md "%ArchiveFolder%\!TimeStamp!"
        )
        move "!File!" "%ArchiveFolder%\!TimeStamp!"
    )

1 个答案:

答案 0 :(得分:0)

这是我最终得到的(.VBS)脚本。根据在其他地方找到的代码进行修改,这会根据照片的拍摄日期为目标文件夹中的所有照片创建子文件夹。它并不完美,但是为了处理我的相机输出,它可以很好地完成工作。

folderName = "C:\photos to sort into folder\"

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(folderName)

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderName)
Set fc = f.Files

For Each fs In fc
   If lcase(fso.GetExtensionName(fs)) = "jpg" Then

      Set objFolderItem =  objFolder.ParseName(fs.Name)
      strValue = objFolder.GetDetailsOf(objFolderItem, 12)

      If strValue <> "" Then

'Local time settings cause problems with DatePart, so manually split and set strValue to a Date object
strValue = CDate(right(left(strValue,13),4) & "-" & right(left(strValue,7),2) & "-" & right(left(strValue,3),2))

          newYY = DatePart("yyyy", strValue)
          newMM = DatePart("m", strValue)
          If newMM < 10 Then newMM = "0" & newMM
          newDD = DatePart("d", strValue)
            If newDD < 10 Then newDD = "0" & newDD
            newFolder = folderName  & newYY & "-" & newMM & "-" & newDD    'check for valid drive and path

                If Not fso.FolderExists(newFolder) Then 
                fso.CreateFolder(newFolder)
                End If

'               fso.CopyFile fs.Path, newFolder & "\"
                fso.MoveFile fs.Path, newFolder & "\"
        End If 
    End If
Next