简体   繁体   English

Get-Date无法将null转换为类型“ System.Datetime”

[英]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. 我得到了一个构建工具的任务,该工具可以将文件(jpeg,mov和heic)的创建时间设置为上次更改日期。 I'm really new to Powershell, having started just a couple of days ago. 我真的是Powershell的新手,就在几天前就开始了。 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

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. 如果我对问题的理解正确,那么任务是在根文件夹中搜索具有某些扩展名的文件,并为找到的每个文件将CreationTime属性更改为与LastWriteTime属性相同。

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. 如果rootfolder $folder内有子$folder ,其中可能还有一些文件要更改CreationTime,则需要在Get-ChildItem cmdlet上添加-Recurse开关。 If that is the case, you can also use the -Include parameter to simplify the command to: 如果是这种情况,您还可以使用-Include参数将命令简化为:

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

Hope that helps 希望能有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将参数“开始”绑定到目标。 异常设置“开始”:“无法将 null 转换为键入“System.DateTime”。” - Cannot bind parameter 'Start' to the target. Exception setting "Start": "Cannot convert null to type "System.DateTime"." 如何在我的 PowerShell 脚本中将日期转换为 system.datetime 类型? - How to convert date to type system.datetime in my PowerShells cript? 无法将值“Mon, 02 Nov 2020 06:39:24 PST”转换为类型“System.DateTime” - Cannot convert value "Mon, 02 Nov 2020 06:39:24 PST" to type "System.DateTime" 如何使用 powershell 将 system.string 转换为 system.DateTime 类型 - How to convert system.string to the type system.DateTime using powershell 从Get-Date / DateTime对象中减去小时 - Subtracting hours from Get-Date/DateTime Object Powershell:get-date & [datetime]::FromFileTime 返回不同的值 - Powershell: get-date & [datetime]::FromFileTime returns different values 由于格式错误,无法将$ startTime识别为system.datetime - Cannot recognise $startTime as a system.datetime due to a format error PowerShell的Get-Date和“无法将参数'Date'绑定到目标” - PowerShell's Get-Date and “Cannot bind parameter 'Date' to the target” 用管道将变量获取日期以转换字符串/格式? - Pipe a variable to get-date to convert string/format? (获取日期).ToUniversalTime()转换为字符串但不更改格式 - (Get-Date).ToUniversalTime() convert to string but not change format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM