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.
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 %.
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?
Use the <pre and /pre tags to wrap your code.
Glad that worked for you.
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.
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)
}
}
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
}
}
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
The other thing you can do is use the -WhatIf switch for the Move-Item cmdlet.