我有多个文件夹,每个文件夹内都有文件。
该结构看起来是这样的:
文件夹编号1 文件夹编号 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 执行此操作。您需要一种编译语言或可能的内核级权限才能执行此操作。
在这个网站上谷歌 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\")
抱歉,我没有任何“Microsoft”系统可以帮助您
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
}
}
}
}