Foreach output-file

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5713

    by xtree79 at 2013-03-22 04:11:08

    Hi everyone,

    Here is my problem, when i'm try to use out-file cmd for $destFilename in my loop i m not getting the results pushed to the text file.
    Now if replace out-put with write-host cmd, I get all my results inside the console window.
    Thing i have tried are :
    input-object = this only will give me 1 sting line result

    thanks in advance


    # PowerShell Foreach-Object
    $findDir ='C:\Users\folder\Desktop\Clients\folder'
    $LogPath = "C:\Users\folder\Desktop\Clients\powershell1\"
    $FilesExe = gci $findDir -recurse
    $LogPath = "C:\Users\folder\Desktop\Clients\powershell1\"
    $filetype = "test.txt"
    $outFile = $LogPath + $filetype
    $insert3 = $FilesExe | ? {$_.extension -eq ".zpl"}
    $insert4 = "base64 -d"
    $insert5 = "\output"
    $insert6 = "\"

    foreach ($file in $insert3)
    {
    $ParentPath = Split-Path $file.fullname -parent
    $folderspace = ' '
    $destFilename = $insert4 + $folderspace + $ParentPath + $insert6+ $file.name + $folderspace + $ParentPath + $insert5 + $insert6 + $file.name
    out-file -filepath $outFile -enc Default -append
    }

    by poshoholic at 2013-03-22 05:48:53

    In your current script, Out-File does not have any input. You need to use -InputObject or pipe input into Out-File in order to write data to a file.

    Also, your script can be simplified quite a bit. Take a look at this version as an example (note that it requires PowerShell 3, but with one modification it could be made to work with PowerShell 2):
    # Identify the directory to search
    $findDir = 'C:\Users\folder\Desktop\Clients\folder'
    # Identify the path to output to
    $logFile = 'C:\Users\folder\Desktop\Clients\powershell1\test.txt'
    # Get all zpl files inside $findDir, even if they are hidden (-Force finds hidden files)
    foreach ($zplfile in Get-ChildItem -LiteralPath $findDir -Recurse -Force -File -Filter '*.zpl') {
    # Identify the location where you want the decoded file to go
    $decodedFileFolder = Join-Path -Path $zplfile.DirectoryName -ChildPath output
    $decodedFilePath = Join-Path -Path $decodedFileFolder -ChildPath $zplfile.Name
    # Identify the base64 command to decode the file
    $base64Cmd = "base64 -d $($zplFile.FullName) $decodedFilePath"
    # Now output the base64 decode command to a file
    $base64Cmd | Out-File -FilePath $logFile -Append
    }

    by xtree79 at 2013-03-24 08:42:19

    Kirk,
    thanks for your input on how to make my script more simplified , but after doing some more research i figure out how to get the results that i m looking for.


    # PowerShell Foreach-Object
    $findDir ='C:\Users\folder\Desktop\Clients\heartland'
    $LogPath = "C:\Users\folder\Desktop\Clients\powershell1\"
    $FilesExe = gci $findDir -recurse
    $LogPath = "C:\Users\folder\Desktop\Clients\powershell1\"
    $filetype = "test.txt"
    $outFile = $LogPath + $filetype
    $insert3 = $FilesExe | ? {$_.extension -eq ".zpl"}
    $insert4 = "base64 -d"
    $insert5 = "\output"
    $insert6 = "\"

    $Results = @()

    foreach ($file in $insert3)
    {
    $ParentPath = Split-Path $file.fullname -parent
    $folderspace = ' '
    $Results += $insert4 + $folderspace + $ParentPath + $insert6+ $file.name + $folderspace + $ParentPath + $insert5 + $insert6 + $file.name

    }

    $Results | out-file -filepath $outFile -append

You must be logged in to reply to this topic.