Creating Folders

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of Bob McCoy Bob McCoy 1 year, 10 months ago.

  • Author
    Posts
  • #29122
    Profile photo of Ernesto Lombardi
    Ernesto Lombardi
    Participant

    I can run this command
    Get-ChildItem C:\Users\Admin\Downloads | Select-Object Extension | % {New-Item -Type Directory -path $_.Extension}

    This drops my new folders into C:\
    I would like to have them drop into another folder
    like to C:\Users\Admin\Documents

    Any tips?

    The end result I am attempting to make is a quick Script to sort my documents per file type.
    Then again per date created.

  • #29124
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    By default, it will put it wherever the current location is set. You can either use a push-location or set-location for the scope of the script or use its name specifically as below.

    $root = "C:\Users\Admin\Documents"
    Get-ChildItem C:\Users\Admin\Downloads | 
        Select-Object Extension | 
        foreach {New-Item -Type Directory -Path $(Join-Path $root $_.Extension)}
    

    Also, in the future, when posting code you'd like others to review, please avoid single-character aliases like ? or %.

  • #29128
    Profile photo of Ernesto Lombardi
    Ernesto Lombardi
    Participant

    Thanks again for the help Bob McCoy.
    The data conversion script you helped me with did exactly what I needed!

    How do you get that neat little script box?

  • #29129
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Use the

    
    		
    	
  • #29130
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Guess I screwed that up. At the bottom of the text input box you will see allowed tags. Use the pre and /pre tags to wrap your code.

  • #29131
    Profile photo of Ernesto Lombardi
    Ernesto Lombardi
    Participant
    Function Create-ExtensionFolder {
    Param(
    $EndPath = "C:\Users\Admin\Documents",
    $StartPath = "C:\Users\Admin\Downloads"
    )
    $Filter = Get-ChildItem -Path $StartPath | Select-Object Extension
    
    Get-ChildItem $StartPath | 
        Select-Object Extension | 
            foreach {
                New-Item -Type Directory -Path $(Join-Path $EndPath $_.Extension)
                    }
    }
    
  • #29132
    Profile photo of Ernesto Lombardi
    Ernesto Lombardi
    Participant
    function Copy-ToExtension {
    param(
    $EndPath = "C:\Users\Admin\Documents",
    $StartPath = "C:\Users\Admin\Downloads"
    )
    $ExtList = Get-ChildItem -Path $StartPath | Select-Object Extension
    $FilterFiles = Get-ChildItem $StartPath -Filter *$ExtList -Recurse
    
    foreach ($file in $FilterFiles)
            {
            $Ext = $file.Extension
            $EndPathExt = "$EndPath\$Ext"
            Copy-Item -Path $file.FullName -Destination $EndPathExt
            }
    }
    
  • #29133
    Profile photo of Ernesto Lombardi
    Ernesto Lombardi
    Participant

    Course I just noticed after asking that question below Reply to Creating Folders
    it saids
    Enclose code blocks in

     and 

    to format it
    I made 2 functions
    One to create my extension folders
    and
    the other to sort my documents for me.
    I use the Copy-Item because I am testing and do not want to move files and loose files
    I think the final version will be Move-Item to keep from massive archives of copys

  • #29139
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    The other thing you can do is use the -WhatIf switch for the Move-Item cmdlet.

You must be logged in to reply to this topic.