Changing date modified via scripting


  1. Posts : 2
    Win 7 Home Premium 64 bit
       #1

    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?
    Last edited by bludgin; 05 Apr 2015 at 14:32.
      My Computer


  2. Posts : 6,285
    Windows 10 Pro X64
       #2
      My Computer


  3. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #3

    bludgin said:
    [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


  4. Posts : 2
    Win 7 Home Premium 64 bit
    Thread Starter
       #4

    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


 

  Related Discussions
Our Sites
Site Links
About 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 09:35.
Find Us