Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Changing date modified via scripting

05 Apr 2015   #1
bludgin

Win 7 Home Premium 64 bit
 
 
Changing date modified via scripting

Hi,

I would like to create some kind of batching script that replaces the date modified of many folders with their date created, any advice on what I should use to do this? I'm thinking JScript might do it, but I've not used that before, and not sure how to go about setting up to use it, or if it really is suitable.

The reason for this request, we have a media server shared on a local network, specifically an "incoming" folder that needs to be write enabled. When some users browse to folders inside incoming they create a thumbs.db file, which touches the containing folder's modified date. We could (and have) gotten those users to turn off that setting, but it only takes one person to mess it up, and has become a recurring problem over the years. Unfortunately, some other devices on the network (eg iPads) can not sort folders by date created, only date modified, so I would like to reset the folder dates automatically.

Any advice would be appreciated, thanks.

edit, after a bit more googling, it looks like powershell is the right tool for something like this, yes?


My System SpecsSystem Spec
.
05 Apr 2015   #2
Ztruker

Windows 10 Pro X64
 
 

My System SpecsSystem Spec
06 Apr 2015   #3
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by bludgin View Post
[A]fter a bit more googling, it looks like powershell is the right tool for something like this, yes?
Yes, PowerShell is one way to go; assuming you know how to run a PowerShell script, Bludgin.


Replace_lastwritetime_with_creationtime.ps1
Code:
# Replace_lastwritetime_with_creationtime.ps1 [directory_path]

# For each item in $source_location with the file 
# attributes of $filter_attributes, set the item's
# date modified to the item's creation time

##
[string] $source_location = "C:\Users\$env:Username\Desktop\New Folder" # this path will be used if no directory is passed to the script
[bool]   $should_recurse = $false
[array]  $attribute_filter = @('Directory')
	<# Valid values for the above array are
	ReadOnly, Hidden, System, Directory, 
	Archive, Device, Normal, Temporary, 
	SparseFile, ReparsePoint, Compressed, 
	Offline, NotContentIndexed, Encrypted #>
##

filter Filter-Attributes($attribs = $attribute_filter) {
	if ( ($_.Attributes -band $attribs) -eq ($attribs) ) {return $_}
}

function Replace-LastWriteTime-With-CreationTime($item) {
	$item.LastWriteTime = $item.CreationTime
}

if ($args[0]) {$source_location = $args[0]}
if ((Test-Path $source_location) -eq $false) {throw "$source_location" + ' is not a valid path'}

if ($attribute_filter.length -eq 0) {$attribute_filter = @('Normal')}
[io.fileattributes]$attribute_filter = ([io.fileattributes]$attribute_filter).value__

Get-ChildItem $source_location -Recurse:$should_recurse | Filter-Attributes |
ForEach-Object { Replace-LastWriteTime-With-CreationTime($_) }
My System SpecsSystem Spec
.

06 Apr 2015   #4
bludgin

Win 7 Home Premium 64 bit
 
 

Whoah, thank you Pyprohly!

I actually got it working in powershell already, though my script was simply this:

Get-ChildItem 'I:\' | ForEach-Object { $_.LastWriteTime = $_.CreationTime }

But your script is a fantastic introduction to using powershell properly, so thank you very much for taking the time.

The trickiest part of powershell for me is getting it to execute a script via it's icon, I still have not got it working like an old fashioned bat file or exe where you could drag files or folders onto the icon and they become command line parameters. So far I've only managed to right click: Run with powershell on the ps1 file, which doesn't support passing a folder to the script, but since it is something I will run a couple of times a week and always on the same folder, it's convenient enough and does the job.

Thanks again, your example really opened my eyes as to what powershell can do.
My System SpecsSystem Spec
Reply

 Changing date modified via scripting




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Date modified property
My wife is using a laptop running windows 7 32 bit. She is having problems getting all her pictures in order as in date and or month. When she clicks on the arrange folder and clicks on either day or anything nothing happens. She keeps them in an order by the day. Last week it was working just...
General Discussion
Changing multiple file modified date
Hi i am working on a bunch of php files and when i get done i would like to change all the modified dates to the same date. Is there a way in windows 7 64 pro to do this as a batch. I have seen third party tools but i would like to stay away from using them if i could as i dont trust them...
General Discussion
Windows 7 keeps changing my pictures!! (filesize & modified date)
This one has been driving me crazy. It seems that Windows likes to modify my jpg files!! Modified date and filesize seems to change on its own, perhaps something to do with the thumbnail view in Explorer. Any ideas why this happens and how I can stop it happening? Cheers zambz
Music, Pictures & Video
file date-created VS date-modified where modified < created
All -- Please help with this question about file date-created VS date-modified in Windows 7. Suppose a file is created at time1. How come Windows7 reports "date created" as "time1" and "date modified" as "time2" where "time2" is a time earlier than time1? See the attached file,...
General Discussion
Last Modified Date bug?
Hi there. How about that case in attached image? Thanks.
General Discussion
Date modified
Only those who have slipstreamed answer this please.. After integration of SP1 to a particular edition, did it the change the "date modified" column during the installation process??
Windows Updates & Activation


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 15:31.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App