header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Mimicking looping through hard drives and adding files to them
Last Post 21 Jun 2012 11:29 AM by RiffyRiot. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
HofferUser is Offline
New Member
New Member
Posts:8
Avatar

--
19 Jun 2012 03:48 PM
    metatag <!-- /* Font Definitions */ @font-face {font-family:Cambria; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Cambria; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Cambria; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} --> metatagHopefully I can explain this well enough so someone can provide me with some insight.

    I have a comma delimited text file that reads like this:
    Email,Location of file location1,Location of file 2. ….. , Size of all files.
    Email,Location of file location1,Location of file 2. ….. , Size of all files.
    Email,Location of file location1,Location of file 2. ….. , Size of all files.
    Email,Location of file location1,Location of file 2. ….. , Size of all files.

    I use a foreach loop to go through the file row by row grabbing the size of all the files related to that email address.  The part I cannot figure out is from this point on. I need to set 8 variables, each acting as the size taken up on hard drive if the files were actually on it. I want to loop through these variables then add the size of the files (a number) to which ever one is the smallest, and keep looping through for each row adding the “size of the files” to which ever one of the hard drive variables is the smallest, thus when the foreach loop is done going through all the rows the 8 variables representing the hard drives will be close in value.

    I have tried using a hash table as well as an array to keep track of the size of the variables representing the hard drives, but have been unable to develop the proper logic to add the “size of the files” field to which ever of the eight variables, is the smallest during that iteration of the loop.

    Any help on how you would do this would be greatly appreciated.
    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    19 Jun 2012 04:59 PM
    A simple example of the logic/techniques required...
    Using a Hash table.

    
    ## Set initial data
    $harddrives=@{'HD1'=1.8;'HD2'=2;'HD3'=3;'HD4'=10;'HD5'=5;'HD6'=6;'HD7'=7;'HD8'=8}
    
    ## Create a loop to to simulate rows
    foreach($newvalue in '1','2','3'){
        ## Create really high initial lowest value
        $lowestvalue=1e100
        ## Enumerate hashtable to find lowest value, and assign the values key to $nextdrive
        $harddrives.Keys | %{
            if($harddrives.Item($_) -lt $lowestvalue){$lowestvalue=$harddrives.Item($_);$nextdrive=$_}
            write-host $lowestvalue $nextdrive
            }
    ## Add $newvalue to the calculated Key
        $harddrives[$nextdrive]+=$newvalue
        }
    $harddrives
    


    There will be more elegant ways, but this serves to demonstrate the basics.
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    19 Jun 2012 06:39 PM
    Same principle, but using an array instead...

    
    
    ## Set initial data
    
    $harddrives=@()
    $harddrives+=New-Object PSObject -Property @{'Index'=0;'HardDrive'='HD1';'UsedSpace'=1}
    $harddrives+=New-Object PSObject -Property @{'Index'=1;'HardDrive'='HD2';'UsedSpace'=2}
    $harddrives+=New-Object PSObject -Property @{'Index'=2;'HardDrive'='HD3';'UsedSpace'=3}
    $harddrives+=New-Object PSObject -Property @{'Index'=3;'HardDrive'='HD4';'UsedSpace'=4}
    $harddrives+=New-Object PSObject -Property @{'Index'=4;'HardDrive'='HD5';'UsedSpace'=5}
    $harddrives+=New-Object PSObject -Property @{'Index'=5;'HardDrive'='HD6';'UsedSpace'=6}
    $harddrives+=New-Object PSObject -Property @{'Index'=6;'HardDrive'='HD7';'UsedSpace'=7}
    $harddrives+=New-Object PSObject -Property @{'Index'=7;'HardDrive'='HD8';'UsedSpace'=8}
    
    ## Create a loop to simulate rows
    foreach($newvalue in '1','2','3'){
    ## Use Powershell cmdlets to find lowest value, calculate Index number, add $newvalue
        $harddrives[($harddrives | Sort UsedSpace | Select -First 1 | Select -expand Index)].UsedSpace+=$newvalue
    }
    
    ## Display result
    $harddrives | ft -a
    
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    HofferUser is Offline
    New Member
    New Member
    Posts:8
    Avatar

    --
    21 Jun 2012 11:03 AM
    Riffy thanks for the help! I was able to use your code and get it to work properly, here is what I did:
    
    foreach($row in $content)
    {
        ## Create really high initial lowest value
        [single]$lowestvalue =100
        [array]$userinfosplit = $row.Split(",")
        $usize = $userinfosplit[-1]
        ## Enumerate hashtable to find lowest value, and assign the values key to $nextdrive
        $harddrives.Keys | %{
            if($harddrives.Item($_) -lt $lowestvalue){$lowestvalue=$harddrives.Item($_);$nextdrive=$_}
            write-host $lowestvalue $nextdrive
        }
        ## Add $newvalue to the calculated Key
        $harddrives[$nextdrive]+=$usize
        add-content C:\pstpowershell\userlists\userlist$nextdrive.txt "$row"
    }
    

    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    21 Jun 2012 11:29 AM
    Awesome! I'm glad it worked for you.

    Perhaps you could mark the thread as resolved to help other people searching?
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer