by xtree79 at 2013-03-16 05:17:56
Hi everyone,by DonJ at 2013-03-16 09:10:37
As you can see below I wrote a sricpt to search for files and put the output into a tex file.
My problem is this, I would like to format my output results like the following:
insert1 + { $.FullName } + insert 2 + { $.FullName } =
Could someone help me with this ?
Here is a copy of my current script:# PowerShell Foreach-Object
$findDir ="search directory"
$LogPath = "my ouputfolder"
$FilesExe = gci $findDir -recurse
$filetype = "test.txt"
$outFile = $LogPath + $filetype
$insert1 = "decode"
$insert 2= "\output"
# Create Log File Function
$Logs = $FilesExe | ? {$.extension -eq ".zpl"} | foreach { $.FullName } | Out-File $outFile -Append
Please use the CODE button to format code.by xtree79 at 2013-03-16 16:42:01
The format operator may be appropriate.
"{0} {1} {2} {3}" -f $insert1,$.FullName,$insert2,$.FullName
Each {0} placeholder will take the corresponding item from the comma-separated list {0} gets the first item, {1} gets the second, and so on. You can read more in about_operators, I believe.
hey Don,by xtree79 at 2013-03-17 08:13:10
thanks for the reply, well i tried what you told me above but now I getting a syntax error when i try to run it.
Is there something else i m missing from using the format operator ?
thanks in advance
# PowerShell Foreach-Object
$findDir ="C:\Users\rkirkland\Desktop\Clients"
$LogPath = "C:\Users\rkirkland\Desktop\Clients\powershell1"
$FilesExe = gci $findDir -recurse
$filetype = "test.txt"
$insert1 = "decode"
$insert2 = "\output"
$outFile = $LogPath + $filetype
$Logs1 = $FilesExe | ? {$.extension -eq ".zpl"}| foreach ($Log in $Logs1){
"{0} {1} {2} {3}" -f<br> $insert1,$_.FullName,$insert2,$_.FullName| Out-File $outFile -Append <br> }<br></code></blockquote>by DonJ at 2013-03-16 16:47:08<blockquote>Seeing the error would help.</blockquote>by xtree79 at 2013-03-16 19:32:01<blockquote><code>Unexpected token 'in' in expression or statement.<br>At C:\Users\rkirkland\Desktop\Clients\powershell\findzpl2.ps1:9 char:67<br>+ $Logs1 = $FilesExe | ? {$_.extension -eq ".zpl"}| foreach ($Log in <<<< $Logs1){<br> + CategoryInfo : ParserError: (in:String) [], ParseException<br> + FullyQualifiedErrorId : UnexpectedToken</code></blockquote>by DonJ at 2013-03-16 19:48:08<blockquote>Ah. Yeah. You're using ForEach-Object wrong. You're using the scripting construct, which is not legal in the pipeline. ForEach-Object, the cmdlet (what you're using) is different from ForEach the scripting construct. It's confusing because ForEach is an alias to the cmdlet and also the scripting construct. <br><br><code><br>letype<br>$FilesExe | ? {$_.extension -eq ".zpl"}| foreach {<br> "{0} {1} {2} {3}" -f
$insert1,$.FullName,$insert2,$.FullName| Out-File $outFile -Append
}
You were mixing two methods.
Don,
thanks again for your help, by the way i finally got it work for me:# PowerShell Foreach-Object
$findDir ="C:\Users\rkirkland\Desktop\Clients"
$LogPath = "C:\Users\rkirkland\Desktop\Clients\powershell1"
$FilesExe = gci $findDir -recurse
$filetype = "test.txt"
$insert1 = "decode"
$insert2 = "\output"
$outFile = $LogPath + $filetype
$Logs1 = $FilesExe | ? {$.extension -eq ".zpl"}
$logs1 = $FilesExe | ? {$.extension -eq ".zpl"}| foreach {
"{0} {1} {2} {3}" -f `
$insert1,$.FullName,$insert2,$_.FullName| Out-File $outFile -Append
}