kardon
kardon

Reputation: 35

Get-Date cannot convert null to type "System.Datetime"

I got a task to build a tool that sets the creation time of a file (jpeg, mov, and heic) to the last change date. I'm really new to Powershell, having started just a couple of days ago. Some of the code was written by my instructor.

$SingleOutput = $true 
$objShell = New-Object -ComObject Shell.Application
$Results = @() 
$folder = "C:\Users\keketz.SCANLOCAL\Desktop\Neuer Ordner"
$files = Get-ChildItem $folder
foreach ($file in $files) {
    $FullName = $folder + "\" + $file.name
    $Result = @()
    $FSItem = Get-Item $FullName

    if ($FSItem -is [System.IO.FileInfo]) {
        $FolderPath = [System.IO.Path]::GetDirectoryName($FSItem.FullName)
        $FileName = $FSItem.Name
        $objFolder = $objShell.Namespace($FolderPath)
        $obJFolderItem = $objFolder.ParseName($FileName)

        for ($attr = 0; $attr -le 2000; $attr++) {
            $attrName = $objFolder.GetDetailsOf($null, $attr)
            $attrValue = $objFolder.GetDetailsOf($obJFolderItem, $attr)
            if ($attrName -eq "Erstelldatum") {
                $Results += New-Object PSObject -Property([ordered]@{
                    ID    = $attr;
                    Name  = $attrName;
                    Value = $attrValue
                })
            }
        }
    }
    $datum = Get-Date $Results.Value
    return
    $Results.Value
    [System.IO.File]::SetLastWriteTime($datum)
}

When I run this code I get the following error messages:

.creationtime : The term '.creationtime' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At C:\Users\MyUser\Desktop\getFile-MetaData.ps1:25 char:14
+     $datum = .creationtime $Results.Value
+              ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.creationtime:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

and

Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date":
"Cannot convert null to type "System.DateTime"."
At C:\Users\keketz.SCANLOCAL\Desktop\getFile-MetaData.ps1:25 char:23
+     $datum = Get-Date $Results.Value
+                       ~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

Upvotes: 0

Views: 5507

Answers (1)

Theo
Theo

Reputation: 61188

I think you have over-complicated this. If I understand the question properly, then the task is to search for files with certain extensions inside a root folder and for every file found, change the CreationTime attribute to become the same as the LastWriteTime attribute.

This should do it:

# set this $justTesting variable to $false to actually make the changes.
# when $true, like below, the script simply shows what would happen in the console window
$justTesting = $true

$folder = "C:\Users\keketz.SCANLOCAL\Desktop\Neuer Ordner"

# get a list of FileInfo objects for files with any of these extensions: '.jpeg', '.jpg', '.mov', '.heic'
$files = Get-ChildItem -Path $folder -File | Where-Object { '.jpeg', '.jpg', '.mov', '.heic' -contains $_.Extension }
# check your PowerShell version. If it is below 3.0 use this instead:
# $files = Get-ChildItem -Path $folder | Where-Object { !$_.PsIsContainer -and '.jpeg', '.jpg', '.mov', '.heic' -contains $_.Extension }

# loop through the files list and set the CreationTime to the same date as the LastWriteTime
foreach ($file in $files) {
    # create a message for output on screen
    $message = "Setting CreationTime for file '{0}' from {1} to {2}" -f $file.FullName, $file.CreationTime, $file.LastWriteTime
    if ($justTesting) {
        Write-Host ("TEST: $message") -ForegroundColor Cyan
    }
    else {
        Write-Host $message
        $file.CreationTime = $file.LastWriteTime
    }
}

If there are subfolders inside the rootfolder $folder where there may also be files to change the CreationTime for, you need to add the -Recurse switch on the Get-ChildItem cmdlet. If that is the case, you can also use the -Include parameter to simplify the command to:

$files = Get-ChildItem -Path $folder -File -Recurse -Include '*.jpeg', '*.jpg', '*.mov', '*.heic'

Hope that helps

Upvotes: 0

Related Questions