Solved

VBScript to set file Last Modified date as another file

Posted on 2011-03-01
12
2,248 Views
Last Modified: 2012-05-11
This is a further request from my original question.

In my work I take many video files as records using digital camera. Then I convert them to MP4 to save space, then I will delete the original MOV files. However, all video converters I've tried lack the ability to keep the output files' Last Modified Date as the original input files'.

I've tried many different programs (SetFileDate, FileDate etc.) but they all set file date to a particular or relative date/time, not using the date from another file. (The Linux TOUCH tool seems able to do this, but the Windows versions lack this function)

In my scenario, after conversion, the original and output video files are typically of same file name and in same folder, but have different extensions:
CIMG0001.MOV (original)
CIMG0001.MP4 (output)
CIMG0002.MOV (original)
CIMG0002.MP4 (output)
etc.

Is there a way to scan all the MP4 files in a folder and copy the Last Modified Date from the corresponding (i.e. same filename, different extension) MOV?

I hope the script can be case insensitive to filename extension, and can be optionally set to process the files in the folder in which the script is in, so that I can just copy the script to the video folder and run from there.
Also, I hope it is easy to change the extensions (MOV, MP4) in the script.

I use WinXP and Win7.

Thanks
0
Comment
Question by:alexkfl
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35014474
Hi there, try this utility called fstamp.exe

http://www.nfbnet.org/download/file_utilities.htm

and run
fstamp.exe /m:CIMG0001.mov CIMG0001.MP4

If that works, we'll script the entire folder.

Regards,

Rob.
0
 

Author Comment

by:alexkfl
ID: 35014642
Hi RobSampson,

The link to fstamp.zip is broken ("404 Not Found").

Any other way to download it?

Thanks
Alex
0
 
LVL 6

Accepted Solution

by:
dax_bad earned 250 total points
ID: 35015017
Hi alexkfl

Here's the modified code as promised

It now uses the actual working directory and is fully case non-sensitive. I also added logging so you can see what it does (a log called master.log will be created in the working directory) and added comments to the code, so it's clear whats being done.

save as .vbs and run it from any folder, no need to edit the path

Hope this is what you wanted.

Cheers
Daniel
'Please keep this
'title: Modify_LastModifiedDate.vbs
'Author: Daniel Badstue through Experts Exchange (dax_bad)
'Created on: 28-Feb-2011
'Last modified: 01-Mar-2011
'Description: Modifies the Last Modified time attribute on .mp4 files according to corresponding file name of .mov type. Please the script file in the working folder, with 
'Version: 1.1

'Initialize masterpath from working directory
masterPath = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))	'get working directory

'Set objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(masterPath)
Set objRE = new RegExp
Set objREmov = new RegExp
Set objREmp4 = new RegExp
Set objApp = CreateObject("Shell.Application") 
Set objAppFolder = objApp.NameSpace(masterPath) 

'Define constants
ForAppending = 8

'Initialize master log file
If objFSO.FileExists(masterPath & "Master.log") then
	Set logFile = objFSO.OpenTextFile(masterPath & "Master.log", ForAppending, False)
	logFile.WriteLine timeStamp() & "Script Started"
Else
	Set logFile = objFSO.CreateTextFile(masterPath & "Master.log")
	logFile.WriteLine timeStamp() & "New log file created!"
	logFile.WriteLine timestamp() & "Script started"
End If

'Initialize regular expressions
objRE.IgnoreCase = True
objREmov.IgnoreCase = True
objREmp4.IgnoreCase = True

'Initialize regular expression file patterns
objREmov.Pattern = ".mov"
objREmp4.Pattern = ".mp4" 

'Main engine start
For Each objMovFile In objFolder.Files 
	'If actual file is a .mov file, move to next part of the script, else move to next file.
	If objREmov.Test(objMovFile.Name) Then
		'Get orginal date of .mov file
		OldVAlue = objMovFile.DateLastModified
		' set regular expression for file name to compare
		objRE.Pattern = Left(objMovFile.Name,Len(objMovFile.Name)-4)
		For Each objMp4File In objFolder.Files
			If objRE.Test(objMp4File.Name) Then
				'If actual file is a .mp4 file, move to next part of the script, else move to next file.
				If objREmp4.Test(objMp4File.Name) Then
					Set Appfile = objAppFolder.ParseName(objMp4File.Name)

		      			logFile.WriteLine timestamp() & objMovFile.Name & " - Last modified date: " & objMovFile.DateLastModified
		      			logFile.WriteLine timestamp() & objMp4File.Name & " - Old value: " & objMp4File.DateLastModified
		      			
		      			Appfile.ModifyDate = OldVAlue	'Change the actual last modified date

		      			logFile.WriteLine timestamp() & objMp4File.Name & " - New value: " & objMp4File.DateLastModified
		      			logFile.WriteLine timestamp() & "Move to next file -->"
		      			
					Set AppFile = Nothing
					'file found and modified, exit for loop and move to next file
					Exit for
				End if
			End If
		Next
	End if
Next
'Main engine end

'reset runtime function
Function timeStamp()
	timestamp = Date & " " & Time & " : "
End Function

'Script complete, update log
logFile.WriteLine timestamp() & "Script complete"
logfile.Close

Set objFSO = Nothing
Set objFolder = Nothing
Set objFiles = Nothing
Set objRE = Nothing
Set objREmov = Nothing
Set objREmp4 = Nothing
Set logFile = Nothing

WScript.Quit

Open in new window

0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35015068
Hi,

You can try the following code
source_dir="c:\music\" ' source folderpath

On Error Resume Next

Set objShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objShell.NameSpace(source_dir)
Set colItems = objFolder.Items

For Each objItem In colItems
	If InStr(LCase(objitem.Name),".mp4") > 0 Then
    	Set objFile = objFSO.GetFile(objItem.Path)
    	Set objFile1 = objFSO.GetFile(Replace(LCase(objfile.Path),".mp4",".mov"))
    	dtmNewDate = objFile1.DateLastModified
    	objItem.ModifyDate  = dtmNewDate
    End If
Next

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35015070
OK, I've uploaded my copy of the file to here:
https://filedb.experts-exchange.com/incoming/ee-stuff/8065-FS.renamed-from-exe-for-your-safety

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35015149
With the utility I posted, this will set the date of each MP4 in the folder that has a matching MOV.

Regards,

Rob.
strFStamp = "C:\Tools\FStamp.exe"
strFolder = "C:\Conversions"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strFStamp = objFSO.GetFile(strFStamp).ShortPath
For Each objFile In objFSO.GetFolder(strFolder).Files
	If Right(LCase(objFile.Name), 4) = ".mp4" Then
		strMOV = Left(objFile.ShortPath, InStrRev(objFile.ShortPath, ".") - 1) & ".mov"
		strCommand = "cmd /c " & strFStamp & " /m:" & strMOV & " " & objFile.ShortPath
		objShell.Run strCommand, 0, False
	End If
Next

MsgBox "Done."

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 65

Expert Comment

by:RobSampson
ID: 35015219
Wow, all this time I've never known about the ModifyDate property:
http://msdn.microsoft.com/en-us/library/bb787825(v=vs.85).aspx

You learn something new every day.

Thanks dax_bad and prashanthd.

Rob.
0
 

Author Closing Comment

by:alexkfl
ID: 35015235
dax_bad,
Your code works great.
Thanks for the log function.
It is now a complete solution.

Thanks
Alex
0
 

Author Comment

by:alexkfl
ID: 35015269
@RobSampson,
@prashanthd,

Your codes also work, although I have to edit the path.
Thanks a lot

 
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35015321
Cool. Thanks for trying the other suggestions.

Regards,

Rob.
0
 
LVL 6

Expert Comment

by:dax_bad
ID: 35016321
Thanks for the points Alex,

for future reference, if you change the source and target file  extentions on your video files just modify line 40(Source extention) & 41 (Target extention)

Cheers
Daniel
0
 

Author Comment

by:alexkfl
ID: 35023752
dax_bad,

You deserve it.
Thanks

Alex
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
cPanel is a Unix based web hosting control panel that provides a graphical interface and automation tools designed to simplify the process of hosting a web site. cPanel utilizes a 3 tier structure that provides functionality for administrators, rese…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now