0

From time to time, when I force-shutdown a Windows Server 2003 machine (I have no choice), the folders contained in a certain directory all change their 'modified date' to the current date.

Luckily, inside each of these folders, the files are all with the correct 'modified date'.

One of these files I want to copy the "modified (or created) date" with is a .jdf file that each of the folders has inside.

I need help to create a script in PowerSell.

I found this PS1 script that almost does the job:

Get-ChildItem $root | Where-Object {$_.PSIsContainer} | Foreach-Object{

# get the oldest file for the current directory object
$oldest = Get-ChildItem $_.FullName | Sort-Object LastWriteTime | Select-Object LastWriteTime -Last 1

if($oldest)
{
# oldest object found, set current directory LastWriteTime
$_ | Set-ItemProperty -Name CreationTime -Value $oldest.LastWriteTime 
$_ | Set-ItemProperty -Name LastWriteTime -Value $oldest.LastWriteTime
}
else
{
# current directory is empty, directory LastWriteTime is left unchanged
Write-Warning "Directory '$($_.FullName)' is empty, skiping..."
}
}

The problem is that the last modified object inside the folders sometimes are subfolders that also get the current date as 'modified date'. How can I make it look the files inside the folder and not the folders?

  • Your aim is a bit unclear for me: what the desired script should effect exactly? (Please don't comment; edit your question rather.) However, NTFS date and time stamp for a particular folder changes whenever an object (file or folder) is either created or deleted inside it. – JosefZ Jun 10 '15 at 11:13
  • @JosefZ: I just edited the post adding a script. I hope it is clearer now. thanks – poupou Jun 10 '15 at 11:19
0

Whereas Get-ChildItem $root -directory gives the same result as

Get-ChildItem $root | Where-Object {$_.PSIsContainer}

you could try (I don't know PS verbose syntax)

Get-ChildItem $root -file

or even

gci $root -file

Not sure whether your script remainder needs improvement...

|improve this answer|||||
  • I tried adding "-directory", I get an error: Get-ChildItem : A parameter cannot be found that matches parameter name 'directory'. At line:1 char:31 + Get-ChildItem $root -directory <<<< | Where-Object {$_.PSIsContainer} | Foreach-Object{ – poupou Jun 11 '15 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.