$from = \"\\\\something\\1 XLS\\2010_04_22\\*\"
$to = \"c:\\out\\1 XLS\\2010_04_22\\\"
copy-item $from $to -Recurse
This works if c:\\out\\
I modified @FrinkTheBrave 's answer to also create sub-directories, and resolve relative paths:
$src_root = Resolve-Path("./source/path/")
$target_root = Resolve-Path("./target/path/")
$glob_filter = "*.*"
Get-ChildItem -path $src_root -filter $glob_filter -recurse |
ForEach-Object {
$target_filename=($_.FullName -replace [regex]::escape($src_root),$target_root)
$target_path=Split-Path $target_filename
if (!(Test-Path -Path $target_path)) {
New-Item $target_path -ItemType Directory
}
Copy-Item $_.FullName -destination $target_filename
}
Here's an example that worked for me. I had a list of about 500 specific files in a text file, contained in about 100 different folders, that I was supposed to copy over to a backup location in case those files were needed later. The text file contained full path and file name, one per line. In my case, I wanted to strip off the Drive letter and first sub-folder name from each file name. I wanted to copy all these files to a similar folder structure under another root destination folder I specified. I hope other users find this helpful.
# Copy list of files (full path + file name) in a txt file to a new destination, creating folder structure for each file before copy
$rootDestFolder = "F:\DestinationFolderName"
$sourceFiles = Get-Content C:\temp\filelist.txt
foreach($sourceFile in $sourceFiles){
$filesplit = $sourceFile.split("\")
$splitcount = $filesplit.count
# This example strips the drive letter & first folder ( ex: E:\Subfolder\ ) but appends the rest of the path to the rootDestFolder
$destFile = $rootDestFolder + "\" + $($($sourceFile.split("\")[2..$splitcount]) -join "\")
# Output List of source and dest
Write-Host ""
Write-Host "===$sourceFile===" -ForegroundColor Green
Write-Host "+++$destFile+++"
# Create path and file, if they do not already exist
$destPath = Split-Path $destFile
If(!(Test-Path $destPath)) { New-Item $destPath -Type Directory }
If(!(Test-Path $destFile)) { Copy-Item $sourceFile $destFile }
}
Yes, add the -Force
parameter.
copy-item $from $to -Recurse -Force
In PowerShell 3 and above I use the Copy-Item with New-Item.
copy-item -Path $file -Destination (new-item -type directory -force ("C:\Folder\sub\sub\" + $newSub)) -force -ea 0
I haven't tried it in ver 2.
$filelist | % {
$file = $_
mkdir -force (Split-Path $dest) | Out-Null
cp $file $dest
}
I have stumbled here twice, and this last time was a unique situation and even though I ditch using copy-item
I wanted to post the solution I used.
Had a list of nothing but files with the full path and in majority of the case the files have no extensions. the -Recurse -Force
option would not work for me so I ditched copy-item
function and fell back to something like below using xcopy as I still wanted to keep it a one liner. Initially I tied with Robocopy but it is apparently looking for a file extension and since many of mine had no extension it considered it a directory.
$filelist = @("C:\Somepath\test\location\here\file","C:\Somepath\test\location\here2\file2")
$filelist | % { echo f | xcopy $_ $($_.Replace("somepath", "somepath_NEW")) }
Hope it helps someone.