Solved

VBScript to set file Last Modified date as another file

Posted on 2011-03-01
12
2,223 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

10 Experts available now in Live!

Get 1:1 Help Now