
Eu tenho várias pastas, cada uma com arquivos dentro da pasta.
A estrutura é mais ou menos assim:
Pasta.No.1 Pasta_No_2 Pasta nº 3
e os arquivos dentro são algo como:
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
Como você pode ver, algumas pastas contêm .
no nome, algumas contêm _
e outras contêm espaços.
O único fator consistente é que cada pastasemprecontém um arquivo .avi, independentemente de qualquer outra coisa.
Portanto, como posso alterar a Date Modified
data/hora da pasta para corresponder ao arquivo .avi contido na pasta? Existe alguma maneira de fazer isso copiando o DateLastModified
arquivo interno (o filho) para a pasta pai usando VBScript?
Até agora estou trabalhando em algo assim:
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")
mas falha ao chamar da linha de comando com:cscript CopyDateToParent.vbs
O que está fazendo com que isso não funcione?
Responder1
Se você estiver em um sistema mais antigo, é assim que você lidaria com isso.
Não é mais permitido no vbs. Você precisará de uma linguagem compilada ou de possíveis privilégios no nível do kernel para fazer isso.
google powershell neste site, é feito com muita facilidade
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\")
Desculpe, não tenho nenhum sistema "Microsoft" para ajudá-lo
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
}
}
}
}