Hi there, I try to clean a folder with thousands of files. The goal is to move the files to diffrent folders sortet by name. All files that start with the lettre A should move to a-folder - and so on.
My script looks like that:
also you have the subfolders inside d:\test. if you do get-childitem the directory will be picked up as well. $_.name will most likely cause error when you try to move.
try something like
#add -file to specify you only want the files and not the directories inside
Get-ChildItem -Path 'C:\Users\potato\Documents\test' -File |
ForEach-object {
if($_.name -like 'a*') {
# include path and desitination
Move-Item -path $_.FullName -destination C:\Users\potato\Documents\test\a-folder
}
elseif($_.name -like 'b*') {
Move-Item -path $_.FullName -destination C:\Users\potato\Documents\test\b-folder
}
elseif($_.name -like 'c*') {
Move-Item -path $_.FullName -destination C:\Users\potato\Documents\test\c-folder
}
}
test it out with a few files
Hopefully the pros will jump in to give more guidance if I’ve missed something.
I think the main problem with the example in your question, and also the solution, by @mawazo, is the the repetitive code. I would do something like this: