2011-04-21 71 views
1

对不起,标题。我会尽力解释更好。如果路径是文件夹或文件,从全名字符串中知道?

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

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 

一切工作正常。现在我可以实现我的目标。再次感谢。

LAST EDIT。 我有另一个想法。我决定在创建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

停止尝试存储丰富的信息为纯文本,你不会有这个问题。 ;) – JasonMArcher 2011-04-22 00:13:26

回答

3

两个FileInfo键入一个DirectoryInfo类型有一个属性PSIsContainer,它允许您查看该对象是否是一个目录。

PS C:\temp> (Get-Item 'Hello world.exe').PSIsContainer 
False 
PS C:\temp> (Get-Item 'c:\temp').PSIsContainer 
True 
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 
+0

+1。嗨ravikanth。非常感谢你。你的帖子总是很有趣。 :) – 2011-04-21 09:19:53

+0

NP。很高兴你找到我的答案有帮助。 – ravikanth 2011-04-21 09:25:44

相关问题