Copy items with target directory structure


i trying create powershell script creates necessary target directory structure me when copying files.

since source directory structure can vary process process, want avoid pre-creating directory structure , instead create on target based on structure present in source directory.

i have semi-working solution right now, however, problem current script is creating directory structure , copying files destination structure, also copying files root of folder!

i copy files root of folder , avoid other extraneous file copies.

this code using right now:

$srcfiledirectory = "c:\myfolder1" $destdirectory = "c:\copytest"  #get files in directory structure $files = get-childitem $srcfiledirectory -recurse  foreach ($file in $files) { 	copy-item -path $file.fullname -destination $destdirectory -recurse -container  }#foreach

it worthwhile note not want copy directory structures verbatim.  want able "cherry pick" files reading them text file have code following:

$filecontent = get-content "c:\temp\filereport.txt"  foreach ($line in $filecontent) { 	 	$file = [system.io.fileinfo]($line) 	copy-item -path $file.fullname -destination $destdirectory -recurse -container 	 }#foreach
using powershell v. 4.0.

please advise.



this came leverages more extensive knowledge/experience .net powershell:

$srcfiledirectory = "c:\copysource" $destdirectory = "c:\copytest"  function get-directorystructure($filepath, $srcdirectory) { 	$fileinfo = [system.io.fileinfo]$filepath 	$directory = $fileinfo.directory.tostring() 	$startingindex = $directory.indexof($srcdirectory) 	 	$dirtree = $directory.remove($startingindex, $srcdirectory.length) 	return $dirtree }  function get-filename($filepath) { 	$fileinfo = [system.io.fileinfo]$filepath 	$filename = $fileinfo.name 	 	return $filename }#function get-filename  function combine-filepaths($srcdirpath, $dirpath, $filepath) { 	$combinedfilepath = "{0}{1}\{2}" -f $srcdirpath, $dirpath, $filepath 	return $combinedfilepath }  $robocopyargs = @" "$srcfiledirectory" "$destdirectory" * /e /xf * "@ write-host "these robocopy args:" write-host $robocopyargs start-process robocopy -verb runas $robocopyargs -verbose  #get files in directory structure $files = get-childitem $srcfiledirectory -recurse -file  foreach ($file in $files) { 	$dirtree = get-directorystructure -filepath $file -srcdirectory $srcfiledirectory 	$filename = get-filename -filepath $file 	$destpath = combine-filepaths -srcdirpath $destdirectory -dirpath $dirtree -filepath $filename 	 	copy-item -path $file.fullname -destination $destpath -recurse -container -verbose }#foreach
gets job done , though may not concise like, allows me substitute either reading file system or list of files (with paths) text file well. 



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

some help on Event 540

WMI Repository 4GB limit - Win 2003 Ent Question

Event ID 1302 (error 1307) DFS replication service encountered an error while writing to the debug log file