Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

@juanpis013

This comment has been minimized.

Copy link

@juanpis013 juanpis013 commented May 10, 2021

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

@Nika-m

This comment has been minimized.

Copy link

@Nika-m Nika-m 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

This comment has been minimized.

Copy link
Owner Author

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

@Nika-m

This comment has been minimized.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment