使用文本文件作为Powershell脚本的输入 [英] Using a text file as input to a Powershell script

查看:397
本文介绍了使用文本文件作为Powershell脚本的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小组正在转移到一个新的网络,在该网络中,我们无法直接从网络A中的计算机复制到网络B中的新计算机.在网络A中的这台计算机上使用多年之后,我的项目文件遍布磁盘.我需要构建一个脚本来将文件夹和文件复制到备份磁盘.那里没问题,但是网络技术人员要求在复制前 知道总字节数.

My group is moving to a new network where we can't directly copy from our computer in Network A to the new machine in Network B. After years on this machine in Network A, I've got project files interspersed all over the disk. I need to build a script to copy the folders and files to a backup disk. No problem there, but the network tech guy requires the total byte count to be known before copying.

在CMD中,我使用了C:\中的dir /AD /S /B > C:\Users\r6540\Desktop\UserFiles.txt来生成大量目录,包括很多我手动编辑掉的垃圾.

In CMD I've used dir /AD /S /B > C:\Users\r6540\Desktop\UserFiles.txt from C:\ to generate a huge list of directories, including a lot of junk that I've manually edited out.

例如

C:\Dev\SSIS
C:\Dev\SSIS\DatabaseCleanup
C:\Dev\SSIS\DatabaseMaintTests
C:\Dev\SSIS\EclipseKeys
C:\Dev\SSIS\TemplateProject

我从没使用过PowerShell,但是肯定看起来该任务将在其能力范围之内.我发现了:

I've never used PowerShell, but it certainly looks like this task would be within its ability. I found this:

$startFolder = "C:\Scripts"

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }

Microsoft technet 上,以及与此相同的页面:

at Microsoft technet and also this, same page:

$objFSO = New-Object -com  Scripting.FileSystemObject
"{0:N2}" -f (($objFSO.GetFolder("C:\Scripts").Size) / 1MB) + " MB"

我要查找的输出是目录名称,选项卡和文件夹大小(虽然没有上面显示的"MB"),但是将CRLF作为EOL写入文本文件.

The output I'm looking for is the directory name, a tab, and the folder size (without the "MB" as shown above though) and CRLF as the EOL written to a text file.

例如

C:\Dev\SSIS 70.23
C:\Dev\SSIS\DatabaseCleanup 17.80
C:\Dev\SSIS\DatabaseMaintTests  22.91
C:\Dev\SSIS\EclipseKeys 1.22
C:\Dev\SSIS\TemplateProject 13.29

任何人都对PowerShell足够了解,可以遍历UserFiles.txt并获取结果文本文件输出吗?

Anyone know PowerShell well enough to troop through UserFiles.txt and get the resulting text file output?

形式与功能无关紧要-因此,如果您能提出一种替代方法,我将很高兴看到它.

Form doesn't matter as much as function--so if you can come up with an alternate approach, I'd be glad to see it.

谢谢.

推荐答案

在PowerShell中这非常简单:

This is pretty straightforward in PowerShell:

$objFSO = New-Object -com  Scripting.FileSystemObject
Get-Content c:\input.txt |
    Foreach { "{0}`t{1:N2}" -f $_, (($objFSO.GetFolder($_).Size) / 1MB) } |
    Out-File c:\output.txt -enc ascii

这假设您找到的FileSystemObject脚本有效. :-)

This is assuming the FileSystemObject script you found works. :-)

这篇关于使用文本文件作为Powershell脚本的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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