Solved Changing date modified via scripting

bludgin

New member
Local time
2:07 AM
Messages
2
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?
 
Last edited:

My Computer

Computer type
PC/Desktop
OS
Win 7 Home Premium 64 bit

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Lenovo IdeaCenter 450
OS
Windows 10 Pro X64
CPU
Intel Quad Core i7-4770 @ 3.4Ghz
Memory
16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
Graphics Card(s)
Intel Integrated HD Graphics
Sound Card
Realtek HD Audio
Monitor(s) Displays
HP 22" LCD
Screen Resolution
1680 x 1050
Hard Drives
250GB Samsung EVO SATA-3 SSD
2TB Seagate ST2000DM001 SATA-2
1.5TB Seagate ST3150041AS SATA
Keyboard
Dell USB
Mouse
Lenovo USB
Internet Speed
Cable via Road Runner 3MB Upload, 30MB Download
Antivirus
Windows Defender, MBAM Pro, MBAE
Browser
Seamonkey
Other Info
UEFI/GPT
PLDS DVD-RW DH16AERSH
[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 Computer

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
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 Computer

Computer type
PC/Desktop
OS
Win 7 Home Premium 64 bit
Back
Top