Powershell 获取完整路径信息

编程入门 行业动态 更新时间:2024-10-25 08:28:15
本文介绍了Powershell 获取完整路径信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 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 获取完整路径信息

本文发布于:2023-11-10 02:03:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   完整   信息   Powershell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!