Author Posts

January 1, 2012 at 12:00 am

by xtree79 at 2013-03-16 05:17:56

Hi everyone,

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

by DonJ at 2013-03-16 09:10:37

Please use the CODE button to format code.

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.

by xtree79 at 2013-03-16 16:42:01

hey Don,
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 `
$insert1,$_.FullName,$insert2,$_.FullName| Out-File $outFile -Append
}

by DonJ at 2013-03-16 16:47:08

Seeing the error would help.

by xtree79 at 2013-03-16 19:32:01

Unexpected token 'in' in expression or statement.
At C:\Users\rkirkland\Desktop\Clients\powershell\findzpl2.ps1:9 char:67
+ $Logs1 = $FilesExe | ? {$_.extension -eq ".zpl"}| foreach ($Log in < <<< $Logs1){
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

by DonJ at 2013-03-16 19:48:08

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.


letype
$FilesExe | ? {$_.extension -eq ".zpl"}| foreach {
"{0} {1} {2} {3}" -f `
$insert1,$_.FullName,$insert2,$_.FullName| Out-File $outFile -Append
}

You were mixing two methods.

by xtree79 at 2013-03-17 08:13:10

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
}