如果path是文件夹或文件,从全名字符串中知道吗?

时间:2011-04-21 08:27:39

标签: string powershell

对不起标题。我会试着更好地解释一下。

让我们假设我运行此命令以获取目录的所有路径,并且我想将它们重定向到文本文件。

gci e:\mytree -r | % {$_.fullname} | out-file e:\folderstructure.txt

现在我需要使用new-item cmdlet重新创建这个嵌套结构。

现在让我们假设我运行这个命令:

gc e:\folderstructure.txt | % {
[system.io.fileinfo]$info = $_
write-host $info $info.extension
}
产生此输出的

E:\mytree\folder1 
E:\mytree\folder2 
E:\mytree\folder3 
E:\mytree\file1.txt .txt
E:\mytree\file12.txt .txt
E:\mytree\folder1\folder.with.dots .dots
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt .txt
E:\mytree\folder3\file4.doc .doc

正如你所看到的,folder.with.dots是一个文件夹但是“它被看作”作为一个文件(它给我.dots扩展名),因为它包含名称中的点。如果我不知道我的文件的所有可能的扩展是否有任何属性可以告诉我一个对象是否包含容器,以便我可以使用带有正确的开关文件或目录的新项目来创建它?

我希望你尽管我的英语能理解我的问题。提前致谢。

修改。 JPBlanc回答后更新

非常感谢你。 :)

我是这样尝试的:

gc e:\folderstructure.txt | % {
[system.io.directoryinfo]$info = $_
if ($info.psiscontainer) {
write-host "$info is a folder" }
else {
write-host "$info is a file" }
}

,输出结果为:

E:\mytree\folder1 is a file
E:\mytree\folder2 is a file
E:\mytree\folder3 is a file
E:\mytree\file1.txt is a file
E:\mytree\file12.txt is a file
E:\mytree\folder1\folder.with.dots is a file
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt is a file
E:\mytree\folder3\file4.doc is a file

遵循你的建议:

gc e:\folderstructure.txt | % {
[system.io.directoryinfo]$info = $_
if ((get-item $info).psiscontainer) {
write-host "$info is a folder" }
else {
write-host "$info is a file" }
}

E:\mytree\folder1 is a folder
E:\mytree\folder2 is a folder
E:\mytree\folder3 is a folder
E:\mytree\file1.txt is a file
E:\mytree\file12.txt is a file
E:\mytree\folder1\folder.with.dots is a folder
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt is a file
E:\mytree\folder3\file4.doc is a file
一切正常。现在我可以实现我的目标。再次感谢。

最后编辑。 我有另一个想法。我决定在创建txt文件之前检查对象是文件还是文件夹。遇到一些困难之后(例如我很快发现我无法重定向格式表,我用来隐藏表头来导出-csv http://blogs.msdn.com/b/powershell/archive/2007/03/07/why-can-t-i-pipe-format-table-to-export-csv-and-get-something-useful.aspx) 我提出了这个解决方案:

gci e:\mytree -r | 
select fullname,@{n='folder';e={ switch ($_.psiscontainer) { 
                                                                    true {1} 
                                                                    false {0}  
                                                                 }
                                     }
                     } | % {($_.fullname,$_.folder) -join ","} | out-file e:\structure.txt

给我这个输出:

E:\mytree\folder1,1
E:\mytree\folder2,1
E:\mytree\folder3,1
E:\mytree\file1.txt,0
E:\mytree\file12.txt,0
E:\mytree\folder1\folder.with.dots,1
E:\mytree\folder1\folder.with.dots\file inside folder with dots.txt,0
E:\mytree\folder3\file4.doc,0

因此,我可以轻松地拆分两个参数,并根据对象类型使用new-item cmdlet。

2 个答案:

答案 0 :(得分:3)

FileInfo类型和DirectoryInfo类型都有一个属性PSIsContainer,可以让您查看对象是否是目录。

PS C:\temp> (Get-Item 'Hello world.exe').PSIsContainer
False
PS C:\temp> (Get-Item 'c:\temp').PSIsContainer
True

答案 1 :(得分:2)

我稍微修改了你的代码:

gc c:\scripts\files.txt | % {
$item = Get-item $_
Write-Host $item.fullName $item.PSIscontainer
}

现在,我的输出如下:

C:\Scripts\mytree\folder1 True
C:\Scripts\mytree\folder2 True
C:\Scripts\mytree\file1.txt False
C:\Scripts\mytree\file2.txt False
C:\Scripts\mytree\folder.with.dots True
C:\Scripts\mytree\folder.with.dots\file inside folder with dots.txt False
相关问题