Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy folder with subfolders? [duplicate]

This script works perfectly in PowerShell. It copies all files with specific type. But I want copy files with it folders & subfolders.

$dest  = "C:\example" $files = Get-ChildItem -Path "C:\example" -Filter "*.msg" -Recurse  foreach ($file in $files) {     $file_path = Join-Path -Path $dest -ChildPath $file.Name      $i = 1      while (Test-Path -Path $file_path) {         $i++         $file_path = Join-Path -Path $dest -ChildPath         "$($file.BaseName)_$($i)$($file.Extension)"     }      Copy-Item -Path $file.FullName -Destination $file_path } 
like image 899
gree line Avatar asked May 09 '17 10:05

gree line


People also ask

How do I copy a folder and subfolders?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

How do I copy a file from multiple sub folders to one folder?

If you want to copy the set of files to multiple folders, hold down the CONTROL key while dragging and dropping the file(s). This ensures that the files in the drop stack are not removed once the copy process has completed. This makes it easy to copy the same set of files (from different folders) to multiple locations.

How do I duplicate folders?

Alternatively, right-click the folder, select Show more options and then Copy. In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy. Navigate to the location where you want to place the folder and all its contents.

Can you duplicate drive folders?

If you have the Google Drive app installed on your computer, you can copy a folder in Google Drive the same way you'd copy any other folder of files. Right-click on the folder, click Copy, then go wherever you want to copy the folder, right-click again, and click Paste.


1 Answers

PowerTip: Use PowerShell to Copy Items and Retain Folder Structure

Source: https://blogs.technet.microsoft.com/heyscriptingguy/2013/07/04/powertip-use-powershell-to-copy-items-and-retain-folder-structure/

Question: How can I use Windows PowerShell 3.0 to copy a folder structure from a drive to a network share, and retain the original structure?

Answer: Use the Copy-Item cmdlet and specify the –Container switched parameter:

$sourceRoot = "C:\temp" $destinationRoot = "C:\n"  Copy-Item -Path $sourceRoot -Filter "*.txt" -Recurse -Destination $destinationRoot -Container 
like image 115
gvee Avatar answered Oct 06 '22 01:10

gvee