Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Last active April 24, 2024 04:23
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcefoli/b77e7f2e8d5bfd005c55 to your computer and use it in GitHub Desktop.
Save jcefoli/b77e7f2e8d5bfd005c55 to your computer and use it in GitHub Desktop.
Powershell: Move Files & Folders In Directory Recursively to Another Directory
<#
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"
@sergeyklay
Copy link

@juanpis013
Copy link

Here is a "better" (different I would say) implementation to maintain the folders' structure:

$sourcePath = "C:\FolderLocation"
$destPath = "C:\NewFolderLocation"
Write-Host "Moving all files in '$($sourcePath)' to '$($destPath)'"
$fileList = @(Get-ChildItem -Path "$($sourcePath)" -File -Recurse)
$directoryList = @(Get-ChildItem -Path "$($sourcePath)" -Directory -Recurse)
ForEach($directory in $directoryList){
$directories = New-Item ($directory.FullName).Replace("$($sourcePath)",$destPath) -ItemType Directory -ea SilentlyContinue | Out-Null
}
Write-Host "Creating Directories"
ForEach($file in $fileList){
try {
Move-Item -Path $file.FullName -Destination ((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)) -Force -ErrorAction Stop
}
catch{
Write-Warning "Unable to move '$($file.FullName)' to '$(((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)))': $($_)"
return
}
}
Write-Host "Deleting folder '$($sourcePath)'"
Remove-Item -Path "$($sourcePath)" -Recurse -Force -ErrorAction Stop

https://stackoverflow.com/a/67476381/3735825

@Nicknaem
Copy link

Nicknaem commented Sep 5, 2021

why the hell do you need get-childitems
move-item -path C:\SourceDir* -destination C:\Destination
moves all the contents including subfolders and files

@jcefoli
Copy link
Author

jcefoli commented Sep 7, 2021

why the hell do you need get-childitems
move-item -path C:\SourceDir* -destination C:\Destination
moves all the contents including subfolders and files

You seem pleasant. Good point though. There must have been some reason I did this in a convoluted way back in the day (perhaps there was a limitation in the Move-Item commandlet in older versions of Powershell)

Regardless, I updated this with a cleaner implementation based off your judgmental suggestion

@Nicknaem
Copy link

Nicknaem commented Sep 7, 2021

why the hell do you need get-childitems
move-item -path C:\SourceDir* -destination C:\Destination
moves all the contents including subfolders and files

You seem pleasant. Good point though. There must have been some reason I did this in a convoluted way back in the day (perhaps there was a limitation in the Move-Item commandlet in older versions of Powershell)

Regardless, I updated this with a cleaner implementation based off your judgmental suggestion

sorry man I just got a little angry at that moment, I'm trying to make some batch script and at every little problem I stumble on complicated codes when most of them can be simply done, and about this problem to move all files and subfolders, people are doing foreach scripts using cmd mv function while it is possible to run powershell commands straight from batch file and do that operation with one line using move-item, and very few talk about it, plus there is on documentation no microsoft docs about calling powershell cmdlets straight from cmd.

@ChangBroot
Copy link

why the hell do you need get-childitems
move-item -path C:\SourceDir* -destination C:\Destination
moves all the contents including subfolders and files

You seem pleasant. Good point though. There must have been some reason I did this in a convoluted way back in the day (perhaps there was a limitation in the Move-Item commandlet in older versions of Powershell)

Regardless, I updated this with a cleaner implementation based off your judgmental suggestion

@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!

@jcefoli
Copy link
Author

jcefoli commented Oct 15, 2021

@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.

@fourthborngoose
Copy link

fourthborngoose commented Apr 6, 2022

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