none
How to set the date of a file using PowerShell? RRS feed

  • Question

  • After using ImageMagick to process a file, I want to restore the original date/time of the file, before processing.  Can someone explain to me how to set the date of "$jpgfile"?

    -Thanks

    $date = Get-Item "$jpgfile" | select LastAccessTime

    convert $jpgfile.Name -scale 500 $newjpgfileName

    How do I set the date of "$jpgfile" here????

    Thursday, November 10, 2016 2:31 AM

Answers

  • File objects have a LastWriteTime property, so you can write, for example:


    PS C:\> $time = (Get-Item "D:\Path\Filename.txt").LastWriteTime
    PS C:\> <process your file here>
    PS C:\> (Get-Item "D:\Path\Filename.txt").LastWriteTime = $time
    

    Of course, you can simplify by putting the file's name in a variable, but that's the basic idea.


    -- Bill Stewart [Bill_Stewart]

    Thursday, November 10, 2016 4:15 PM
    Moderator

All replies

  • $file =Get-Item $jpgfile  # notice we do not use or want quotes in PowerShell
    $lwt = $File.LastWriteTIme
    #convert
    $file =Get-Item $jpgfile  # notice we do not use or want quotes in PowerShell
    $file.LastWriteTime = $lwt


    \_(ツ)_/

    Thursday, November 10, 2016 3:19 AM
  • Did you try the ImageMagick option 'preserve-timestamp' ? 

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, November 10, 2016 9:25 AM
  • "touch.exe" allows you to selectively set any/all of the 3 dates (created, modified, accessed) via command line.

    See SysInternals


    Tom T

    Thursday, November 10, 2016 3:52 PM
  • File objects have a LastWriteTime property, so you can write, for example:


    PS C:\> $time = (Get-Item "D:\Path\Filename.txt").LastWriteTime
    PS C:\> <process your file here>
    PS C:\> (Get-Item "D:\Path\Filename.txt").LastWriteTime = $time
    

    Of course, you can simplify by putting the file's name in a variable, but that's the basic idea.


    -- Bill Stewart [Bill_Stewart]

    Thursday, November 10, 2016 4:15 PM
    Moderator
  • "touch.exe" allows you to selectively set any/all of the 3 dates (created, modified, accessed) via command line.

    See SysInternals


    Tom T

    Does not do what was asked.


    \_(ツ)_/

    Thursday, November 10, 2016 4:16 PM
  • I see no "preserve-timestamp" option in ImageMagick.  At least for the OSX version.
    Friday, November 11, 2016 3:27 AM
  • Thanks, worked like a charm!
    Friday, November 11, 2016 3:28 AM