Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Syntax questions -- advice on correct approach

$
0
0

Normal 0 false false false EN-US X-NONE X-NONE

 Preamble to my question..

 I am an old fellow who was skilled in more procedural languages and development methods. PowerShell is causing me a variety of questions as I get my feet wet.  The metaphor is different and that is causing me to ask these questions...

 Question #1 – Efficiency versus ‘getting it done’

Consider the following code snippet (used for all three questions) …

 Determine total number of files

Get-ChildItem-Path$strCurPath-Force |

ForEach-Object {$lngFileTotal++ }

 

# Deterine number of files that will be deleted

$lngFileRemovedCount= 0

Get-ChildItem-Path$strCurPath-Force | Where-Object { !$_.PSIsContainer -and$_.LastWriteTime -lt$limit } |

ForEach-Object { $lngFileRemovedCount++ }

 

#  Delete the files that are old

Get-ChildItem-Path$strCurPath-Force | Where-Object { !$_.PSIsContainer -and$_.LastWriteTime -lt$limit }  |

   tee$strCurPath\Removed\$FileName   |  Remove-Item-force-whatif

 

Add-Content$strCurPath\Removed\$FileName-Value ("'nFiles Removed Count = "+$lngFileRemovedCount)

Add-Content$strCurPath\Removed\$FileName-Value ("'nTotal Files = "+$lngFileTotal)

$lngFileLeftCount=$lngFileTotal-$lngFileRemovedCount

Add-Content$strCurPath\Removed\$FileName-Value ("'nFiles Left = "+$lngFileLeftCount)

 

Write-Host"DONE"

 To my simple view, this requires three iterations through the directory – one to count the number of files, one to determine how many will be deleted and the third pass to actually delete the files. 

 Use of the Measure-Object value would seem simpler but I have not figured out a way to capture the value – see Question #2. 

 Is there a more efficient method or approach to use in this example? 

 

Question #2 – How to capture the Measure-Object value

Consider the following code snippet…

 Get-ChildItem -Path $strCurPath -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit } |

Get-ChildItem -Recurse -force | Measure-Object

Count    : 1249

Average  :

Sum      :

Maximum  :

Minimum  :

Property :

 I can get the count of items displayed but I would like to set a variable equal to that value for later use.  My attempts to do that are met with a PowerScript error message.

 What is the proper syntax for accomplishing this?

 

Question #3 – How to append information to the end of the log file

 

The actual values are accurate but the use of the Add-Content cmdlet seems to not be correct  – some information is appended to the end of the log file but it looks like gibberish – possibly a foreign character set.  See the cut and paste below from the log file.

 a---        6/21/2014   8:17 PM     111176 MDAlarm_20140621-201638.jpg                                                                       

-a---        6/21/2014   8:17 PM      89959 MDAlarm_20140621-201635.jpg                                                                       

 

 

渧楆敬敒潭潃湵渧潔慴楆敬〲㠶渧楆敬敌瑦㴠㠠㤱

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles