遞歸更改資料夾的 DateLastModified 屬性以符合內部文件的屬性

遞歸更改資料夾的 DateLastModified 屬性以符合內部文件的屬性

我有許多資料夾,每個資料夾內都有文件。

結構看起來像這樣:

資料夾.No.1
資料夾_No_2
資料夾編號 3

其中的文件類似:

Folder.No.1\My.Movie.1.avi
Folder.No.1\My.Movie.1.txt

Folder_No_2\My_Movie_2.avi
Folder_No_2\My_Movie_2.jpg
Folder_No_2\My_Movie_2.txt

Folder No 3\My Movie 3.avi

如您所看到的,有些資料夾.名稱中包含,有些資料夾包含_空格,有些資料夾名稱中包含空格。

一個一致的因素是每個資料夾都會總是包含 .avi 文件,無論其他內容如何。

因此,如何更改Date Modified資料夾的日期/時間以符合資料夾中包含的 .avi 檔案的日期/時間?有什麼方法可以透過DateLastModified使用 VBScript 將檔案(子層級)內的檔案複製到父資料夾來做到這一點嗎?

到目前為止我正在做這樣的事情:

Function Recursion(strDirectory)
On Error Resume Next 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirectory)
Set colFiles = objFolder.Files
Dim objFolderShellItem

For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile)) = "AVI" Then
    Set objShell = CreateObject("Shell.Application")
    Set objShellFolder = objShell.NameSpace(strDirectory)
    Set objFolderItem = objFolder.Self
    'folder
    objFolderItem.ModifyDate = objFile.DateLastModified
    'file
    'objShellFolder.Items.Item(objFile.Name).ModifyDate = objFile.DateLastModified
    Wscript.Echo "Date of folder" & objFolder.Name & "was updated" 
End If
Next

For Each folder In objFolder.SubFolders
    Recursion(folder)  '<- recurse here
Next

Set objFso = Nothing
Set objFolder = Nothing
set colFiles = Nothing

End Function




Call Recursion("C:\Temp")

但從命令列呼叫時會失敗:cscript CopyDateToParent.vbs

是什麼導致這個不起作用?

答案1

如果您使用的是較舊的系統,這就是您的處理方式。

vbs 不再允許使用它。您將需要編譯語言或可能的核心級權限才能執行此操作。

在這個網站上使用google powershell,它很容易完成

Function Recursion(strDirectory)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirectory)
Set colFiles = objFolder.Files
Dim objFolderShellItem
Dim blnFound



blnFound = false
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile)) = "AVI" Then
    blnFound = true
    dtDateTime = objFile.DateLastModified
    'if you care only about one file, then you can exit for here for performance, or you have to do more logic on keep thing max date for the avi file or min.  you never said.
    ''file
    ''objShellFolder.Items.Item(objFile.Name).ModifyDate = objFile.DateLastModified
End If
Next

For Each folder In objFolder.SubFolders
    if blnFound = true Then
        Set objShell = CreateObject("Shell.Application")
        Set objShellFolder = objShell.NameSpace(strDirectory)''should be parent folder not 'folder' its in
        Set objFolderItem = objShellFolder.Self
        objFolderItem.ModifyDate = dtDateTime   ''no longer possible since around 2010
    End IF
    Recursion(folder)  '<- recurse here
Next

Set objFso = Nothing
Set objFolder = Nothing
set colFiles = Nothing

End Function




Call Recursion("C:\Temp\")

抱歉,我沒有任何「微軟」系統可以幫助您

function GetFiles($path = $pwd) 
{ 
    foreach ($item in Get-ChildItem $path)
    {
        if (Test-Path $item.FullName -PathType Container) 
        {
            $item.LastWriteTime = $dateToChange 
            GetFiles $item.FullName
        } 
        else 
        {   
            if($item.extension.ToUpper() -eq 'AVI')
            {
                $dateToChange = $item.LastWritten
            }
        }
    } 
}

相關內容