Last active
April 24, 2024 04:23
-
-
Save jcefoli/b77e7f2e8d5bfd005c55 to your computer and use it in GitHub Desktop.
Powershell: Move Files & Folders In Directory Recursively to Another Directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Recursively move all files in C:\SourceDir into C:\Destination | |
Assumes C:\Destination exists already, or there could be problems | |
#> | |
Move-Item -Path "C:\SourceDir\*" -Destination "C:\Destination" |
@ChangBroot Yeah, there was some niche reason why I needed this a long time ago, but I cannot remember. It's not very practical for most use cases, but I think I needed to move files out of a directory quickly without needing to preserve paths or directories.
Most of my gists are a place I dump scripts to remember/reuse/modify them later, so any comments/feedback are welcome, but it doesn't mean that they're a copy/paste solution for everyone. Glad this helped you.
This was an awesome find! Simplicity at it's finest.
Get-ChildItem 'dot-slash *.(file extension)' -Recurse | Move-Item -Destination 'dot-slash'
I just... I mean I... Thanks you. It was so simple I completely missed it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jcefoli , don't worry about people's comments. Your script is exactly what I needed. These one-liner people live in an ideal world with ideal problems, but you and I don't live in an ideal world. The onliner command doesn't work if the file(s) is open by some remote user(s) which is why I believe you might have developed this script. The onliner copies the active-session-file from a folder to the root of the destination, instead of the its folder, plus it doesn't move the folder wher the file is in use. In away, it thinks that the folder is in use and moves the file but not the folder. I tested the following command and it didn't work:
Get-ChildItem "c:\temp1" -Recurse | Move-Item -Destination "c:\temp2"
Inside temp1 i had another folder called test with a bitmap file named a.bmp (i.e. c:\temp1\test\a.bmp). I opened the "a.bmp" file in Paint and run the above command, but it moved the file (which I thought it shouldn't) to c:\temp2 instead of c:\temp2\test\ and didn't move the parent folder of "a.bmp." However, your script did the job perfectly...almost.
Anyways, my situation is much more complicated as I'm dealing with 20 TB of financial data that need to be moved with tens of remote users actively using files and copying and then deleting the parent is also not option because I have only 14 TB of disk space.
Thanks for the script. Peace!