Using a text file as input to a Powershell script

25,583

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

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

Share:
25,583
Jim the Frayed
Author by

Jim the Frayed

Surpassingly Accurate Coder of Vanquishing - UI/DB developer since 1978. - Worked in dBase in CP/M with 8" floppies. - Working in SQL Server since 6.5, i.e., Ancient. "There is no system foolproof enough to thwart a sufficiently great fool." -- J.R. Alves

Updated on July 09, 2022

Comments

  • Jim the Frayed
    Jim the Frayed almost 2 years

    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.

    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.

    e.g.

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

    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"
        }
    

    at Microsoft technet and also this, same page:

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

    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.

    e.g.

    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
    

    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.

    Thanks.

  • Keith Hill
    Keith Hill almost 9 years
    To get the true size using Get-ChildItem, use -Force to get it to list hidden files.
  • Matt
    Matt almost 9 years
    @KeithHill This is true.
  • Jim the Frayed
    Jim the Frayed almost 9 years
    This works fine, except the separator is a space instead of a tab. Apparently the tabs washed out when I pasted the input file text. The output file becomes an input file for the database upload process, and the space causes an error. Know of a way we can stuff a tab character there?
  • Jim the Frayed
    Jim the Frayed almost 9 years
    The first script gets close, but the separator is " -- ", and I need a tab there, because the output file here becomes the input file for another process. Thanks for the total/CSV version too--very illustrative.
  • Matt
    Matt almost 9 years
    @JimtheFrayed replaced with tab
  • Keith Hill
    Keith Hill almost 9 years
    You can stuff in a tab char using <backtick>t inside a double quoted string. See the updated answer.
  • Jim the Frayed
    Jim the Frayed almost 9 years
    Perfecto! This generates the exact file format needed. Thanks, Kevin!