Author Posts

June 11, 2014 at 12:25 pm

$SourceDir = "C:\somedir"

Get-ChildItem -Path $SourceDir |
foreach {
if ($_ -match "(\d{10})" ) {

$phone = "$($matches[0])$($_.Extension)"
$day = (date).AddDays(-1).ToShortDateString()
$newname = "$day" + '-' + "$oldname"
Rename-Item -Path $_.FullName -newName $newname -Force
}
}

June 11, 2014 at 12:38 pm

The Rename-Item most likely fails due to the date in the $day variable.

(date).AddDays(-1).ToShortDateString() returns 10/06/2014 on my PC and the slash is not a valid character for a file name. You need to use something like (date).AddDays(-1).ToString('yyyy-MM-dd') to get a valid date string for your file name.

Regards
Daniel

June 11, 2014 at 2:29 pm

thank you that worked