
複数のフォルダーがあり、各フォルダー内にはファイルが入っています。
構造は次のようになります。
フォルダNo.1 フォルダ番号2 フォルダNo.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 ファイルが含まれます。
したがって、フォルダーの日付/時刻を、フォルダー内に含まれる .avi ファイルの日付/時刻と一致するように変更するにはどうすればよいでしょうか。VBScriptを使用して、フォルダー内のファイル (子) から親フォルダーにDate Modified
コピーすることで、これを実行する方法はありますか。DateLastModified
これまでのところ、私は次のようなことに取り組んでいます:
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\")
申し訳ありませんが、私はあなたを助けるための「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
}
}
}
}