我有一个名为 Videos 的目录.在这个目录里面,是一堆各种相机的子目录.我有一个脚本可以检查各种摄像机中的每一个,并删除早于特定日期的记录.
I have a directory called Videos. Inside this directory, are a bunch of sub directories of various cameras. I have a script that will check each of the various cameras, and delete recordings older than a certain date.
我在获取相机的完整目录信息时遇到了一些麻烦.我正在使用以下内容来获取它:
I am having a bit of trouble getting the full directory information for the cameras. I am using the following to get it:
#Get all of the paths for each camera $paths = Get-ChildItem -Path "C:\Videos\" | Select-Object FullName然后我遍历 $paths 中的每个路径并删除我需要的任何内容:
And then I loop through each path in $paths and delete whatever I need to:
foreach ($pa in $paths) { # Delete files older than the $limit. $file = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } $file | Remove-Item -Recurse -Force $file | Select -Expand FullName | Out-File $logFile -append }当我运行脚本时,出现如下错误:
When I run the script, I am getting errors such as:
@{FullName=C:\Videos\PC1-CAM1} Get-ChildItem : Cannot find drive. A drive with the name '@{FullName=C' does not exist. At C:\scripts\BodyCamDelete.ps1:34 char:13 + $file = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsCont ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (@{FullName=C:String) [Get-ChildItem], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand有没有办法把@{FullName= 从路径中去掉?我认为这可能是问题所在.
Is there a way to strip that @{FullName= off of the Path? I think that may be what the issue is.
推荐答案在您的情况下 $pa 是一个具有 FullName 属性的对象.您访问的方式是这样的.
In your case $pa is an object with a FullName property. The way you would access that would be this.
$file = Get-ChildItem -Path $pa.FullName -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit }然而,只更改这一行并离开会更简单
However it would just be simpler to change only this line and leave
$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object -ExpandProperty FullName-ExpandProperty 将只返回字符串而不是 Select-Object 返回的对象.
-ExpandProperty will just return the string instead of the object that Select-Object was returning.
更多推荐
Powershell 获取完整路径信息
发布评论