Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2657
  • Last Modified:

VBScript to set file Last Modified date as another file

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
alexkfl
Asked:
alexkfl
  • 5
  • 4
  • 2
  • +1
1 Solution
 
RobSampsonCommented:
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
 
alexkflAuthor Commented:
Hi RobSampson,

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

Any other way to download it?

Thanks
Alex
0
 
dax_badCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
prashanthdCommented:
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
 
RobSampsonCommented:
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
 
RobSampsonCommented:
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
 
RobSampsonCommented:
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
 
alexkflAuthor Commented:
dax_bad,
Your code works great.
Thanks for the log function.
It is now a complete solution.

Thanks
Alex
0
 
alexkflAuthor Commented:
@RobSampson,
@prashanthd,

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

 
0
 
RobSampsonCommented:
Cool. Thanks for trying the other suggestions.

Regards,

Rob.
0
 
dax_badCommented:
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
 
alexkflAuthor Commented:
dax_bad,

You deserve it.
Thanks

Alex
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now