SteroidKing666
SteroidKing666

Reputation: 573

Script changes timestamp successfully for all files in a directory. But subdirectories files are left untouched. What change is required?

So I am using this script:

$a = Get-Date "2/25/2021 6:26AM"

$b = Get-ChildItem "C:\dir\dir2"
foreach ($i in $b)
{
    $i.LastWriteTime = $a
    $a = $a.AddMinutes(1)    
}
$b

The above script successfully modifies the time timestamp of all the files inside C:\dir\dir2 to the $a value.

However, there are more directories inside dir2. These directories also have several files. The name of the directories under dir2 get their timestamp changes. But the files inside these subdirectories dont get their timestamps modified.

For example:

dir
  dir2
     dirA
       fileABC
       fileDCS
     dirB
     file.txt
     file2.txt

Here every file and folder under dir2 get their timestamp changed.

But fileABC and fileDCS under dirA retain their old timestamp.

How can I modify my script to also accommodate the timestamp change for fileABC and fileDCS under the subdirectory dirA of dir2?

Upvotes: 0

Views: 392

Answers (1)

general-gouda
general-gouda

Reputation: 318

Add -recurse to Get-ChildItem to go through subdirectories. And use -force to make sure you enumerate hidden items too.

Upvotes: 1

Related Questions