[英]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.