Move Files with Different Extensions to Different Folders using PowerShell
In a typical computer system, you might encounter a situation where you have many files scattered across different folders, and you want to organize them based on their extensions. For instance, you may have image files with extensions like .jpg and .crw, and you want to move all .jpg files to one folder, and all .crw files to another folder.
Identifying File Extensions
File extensions are a way of identifying the type of a file. For example, .txt files are text files, .pdf files are Adobe Portable Document Format, and so on. To move files based on their extensions using PowerShell, you first need to identify the extensions of the files in the folder.
In the above code, we define the path to the folder, get a list of all files in the folder, then select only the extensions of the files using the ForEach-Object cmdlet, and finally, remove any duplicates using the Sort-Object cmdlet with the -Unique parameter.
Moving Files Based on Extensions
After identifying the file extensions, you can then use a loop to move the files to their respective folders. You can do this by using the If-Else cmdlet and checking the file extension property of each file.
In the above code, we start a loop for each extension, create a new destination path for the files with that extension, and check if the destination path exists. If it does not exist, we create a new folder. Then, we select all the files in the folder with the current extension and move them to the destination path using the Move-Item cmdlet.
Adding Metadata
When moving image files, it is often helpful to preserve their metadata. For example, when moving .crw files, you may want to preserve the camera settings and other metadata stored in the .xmp files. You can do this by using the Copy-Item cmdlet instead of Move-Item and preserving the metadata using the -PassThru parameter.
In the above code, we get a list of all .xmp files, then select the corresponding .jpg file by matching their names. We create a new destination path for the .jpg file, then copy the .xmp file to the destination path using the Copy-Item cmdlet. We then rename the copied file to have the same name as the .jpg file.
- In this article, we learned how to move files with different extensions to different folders using PowerShell. We first identified the file extensions and then used a loop to move the files to their respective folders. We then learned how to preserve metadata for image files like .crw files.
References
-
PowerShell Move-Item cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item
-
PowerShell Copy-Item cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item
-
PowerShell Test-Path cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path
-
PowerShell New-Item cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item
-
PowerShell Get-ChildItem cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem
-
PowerShell Select-Object cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object
-
PowerShell ForEach-Object cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object
-
PowerShell If-Else cmdlet documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/if