获取内容:找不到路径 [英] Get-Content : Cannot find path

查看:144
本文介绍了获取内容:找不到路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PowerShell中编写一个脚本,该脚本在"foreach"循环中读取名称中包含"example"的特定文件夹中的所有唯一文件. 问题是我试图将每个文件的内容保存在变量中,但没有成功.尝试使用 Get-Content $ file ,即使路径设置为的开头,它也会引发以下错误" Get-Content:找不到路径" >文件夹 var和文件实际上包含我需要的文件.我无法将其分配给 $ FileContent

I'm trying to write a script in PowerShell which reads in a "foreach" loop all the only files in a specific folder which contains "example" in it's name. The problem is that I'm trying to save the content of each file in a variable without any success. Tried to use Get-Content $file and it throws the following error "Get-Content : Cannot find path" even though the path was set at the beginning to the Folder var and file actually contains the file that I need. I can't assign it to $FileContent

$Folder = Get-ChildItem U:\...\Source

foreach($file in $Folder)
{
    if($file.Name -Match "example")
    {
        $FileContent = Get-Content $file                
    }
}

推荐答案

发生这种情况是因为FileInfo对象的默认行为仅返回文件名.也就是说,没有路径信息,因此Get-Content尝试从当前目录访问文件.

This happens as the FileInfo object's default behavior returns just the file's name. That is, there is no path information, so Get-Content tries to access the file from current directory.

使用FileInfo的FullName属性来使用绝对路径.像这样

Use FileInfo's FullName property to use absolute path. Like so,

foreach($file in $Folder)
{
...
    $FileContent = Get-Content $file.FullName

这篇关于获取内容:找不到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆