[[UPDATE]] I noticed that the files with long names had special characters in them, I experimented with long file names without them and it worked. My new question is: how can I improve my code such that even if file names contain "[]" they are sorted according to their extensions.
I am learning powershell scripting and wrote a simple script to get started. The script basically takes a location as a parameter and then uses a foreach loop to evaluate which folder the file should go into based on the file extension.
The script runs smoothly and does what is expected except with files with long names, they are left as it is. If I shorten the name of the file and rerun the script it works fine. I used file.extension separately on files with long names (using the same foreach loop that is in the script) and it works fine if I write out the extension.
Below is a short excerpt from the script
$Docs = '.txt', '.xlsx', '.pdf', '.doc', '.docx', '.odt', '.html', '.pptx'
foreach ($i in Get-ChildItem -Path .)
{
if ($Docs -contains $i.extension)
{
Move-item $i -Destination "Documents"
}
}
I can't understand where I am going wrong, can someone please point me in the right direction
EDIT:
The issue (as stated in the update) is with file names with special characters in them. Here is an example:
A file named "Picture [hello].jpg" will not be moved but a file named "Picture hello.jpg" would be.
But if I run the same foreach loop just writing out the extensions, it writes out the correct file extension.