Question

I would like to change a folder lastWriteTime without modify a lastWriteTime of files which are in this directory.

I have been trying this:

$a = Get-Date "02/09/2013 4:59 PM"
$d = "C:\Users\user\Documents\CV"
$d.LastWriteTime = $a

but it's didn't working because d has not a LastWriteTime property. There is any way to change this property? I have to modified lastTimeWrite property only "CV" directory - files in "CV" shouldn't be touched.

Was it helpful?

Solution

One way is to cast it to [system.io.directoryinfo]

$a = Get-Date "02/09/2013 4:59 PM"
$d = [system.io.directoryinfo]"C:\Users\user\Documents\CV"
$d.LastWriteTime = $a

or using get-item cmdlet

$a = Get-Date "02/09/2013 4:59 PM"
$d = get-item C:\Users\user\Documents\CV
$d.LastWriteTime = $a

OTHER TIPS

Function Touch-File
#Touches a file or folder if the file does not exist it creates a new empty    file
    {
    $file = $args[0]
    $date = $args[1]

    if($file -eq $null) {
        throw "No filename supplied"
    }

    if($date -eq $null) {
        throw "No date supplied"
    }

    if(Test-Path $file)
    {
    #Optionally select the date stamp you want to update

         (Get-Item $file).LastWriteTime = $date
         (Get-Item $file).CreationTime = $date
         (Get-Item $file).LastAccessTime =$date
     }
     else
    {
         echo $null > $file
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top