Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. function Move-FileWithTimestamp {
  2. [cmdletbinding()]
  3. param(
  4. [Parameter(Mandatory=$true,Position=0)][string]$Path,
  5. [Parameter(Mandatory=$true,Position=1)][string]$Destination
  6. )
  7.  
  8. $origLastAccessTime = ( Get-Item $Path ).LastAccessTime
  9. $fileName = ( Get-Item $Path ).Name
  10. Move-Item -Path $Path -Destination $Destination
  11. $(Get-Item ($Destination+''+$fileName)).LastAccessTime =
  12. $origLastAccessTime
  13. }
  14.  
  15. function Move-FileWithTimestamp {
  16. [cmdletbinding()]
  17. param(
  18. [Parameter(Mandatory=$true,Position=0)][string]$Path,
  19. [Parameter(Mandatory=$true,Position=1)][string]$Destination
  20. )
  21. $origCreationTime = ( Get-Item $Path ).CreationTime
  22. $origLastWriteTime = ( Get-Item $Path ).LastWriteTime
  23. $origLastAccessTime = ( Get-Item $Path ).CreationTime
  24.  
  25. $fileName = ( Get-Item $Path ).Name
  26. Move-Item -Path $Path -Destination $Destination
  27. $(Get-Item ($Destination+''+$fileName)).CreationTime = $origCreationTime
  28. $(Get-Item ($Destination+''+$fileName)).LastWriteTime =
  29. $origLastWriteTime
  30. $(Get-Item ($Destination+''+$fileName)).LastAccessTime =
  31. $origLastAccessTime
  32. }
  33.  
  34. function Move-FileWithTimestamp {
  35. [cmdletbinding()]
  36. param(
  37. [Parameter(Mandatory=$true,Position=0)][string]$Path,
  38. [Parameter(Mandatory=$true,Position=1)][string]$Destination
  39. )
  40. $origCreationTime = ( Get-Item $Path ).CreationTime
  41. $origLastWriteTime = ( Get-Item $Path ).LastWriteTime
  42. $origLastAccessTime = ( Get-Item $Path ).CreationTime
  43. $origChildCreationTime = ( Get-ChildItem $Path ).CreationTime
  44. $origChildLastWriteTime = ( Get-ChildItem $Path ).LastWriteTime
  45. $origChildLastAccessTime = ( Get-ChildItem $Path ).CreationTime
  46.  
  47.  
  48. $fileName = ( Get-Item $Path ).Name
  49. Move-Item -Path $Path -Destination $Destination
  50. $(Get-Item ($Destination+''+$fileName)).CreationTime = $origCreationTime
  51. $(Get-Item ($Destination+''+$fileName)).LastWriteTime = $origLastWriteTime
  52. $(Get-Item ($Destination+''+$fileName)).LastAccessTime =
  53. $origLastAccessTime
  54. $(Get-ChildItem ($Destination+''+$fileName)) | ForEach-Object {
  55. $_.CreationTime = $origChildCreationTime }
  56. $(Get-ChildItem ($Destination+''+$fileName)) | ForEach-Object {
  57. $_.LastWriteTime = $origChildLastWriteTime }
  58. $(Get-ChildItem ($Destination+''+$fileName)) | ForEach-Object {
  59. $_.LastAccessTime = $origChildLastAccessTime }
  60. }
  61.  
  62. $src = "C:SrcFolder123"
  63. $dest = "X:DestFolder321"
  64. $src = $src.Replace("","\")
  65.  
  66. $i = Get-ChildItem -Path $src -Recurse
  67. $i | % { ## -- All files and folders
  68.  
  69. $apath = $_.FullName -Replace $src,""
  70. $cpath = $dest + $apath
  71.  
  72. Copy-Item -Path $_.FullName -Destination $cpath -Force
  73.  
  74. If (Test-Path $cpath)
  75. {
  76. Set-ItemProperty -Path $cpath -Name CreationTime -Value $_.CreationTime
  77. Set-ItemProperty -Path $cpath -Name LastWriteTime -Value $_.LastWriteTime
  78. Set-ItemProperty -Path $cpath -Name LastAccessTime -Value $_.LastAccessTime
  79. }
  80. }
  81.  
  82. $d = Get-ChildItem -Path $src -Recurse -Directory
  83. $d | % { ## -- Folders only
  84.  
  85. $apath = $_.FullName -Replace $src,""
  86. $cpath = $dest + $apath
  87.  
  88. If (Test-Path $cpath)
  89. {
  90. Set-ItemProperty -Path $cpath -Name CreationTime -Value $_.CreationTime
  91. Set-ItemProperty -Path $cpath -Name LastWriteTime -Value $_.LastWriteTime
  92. Set-ItemProperty -Path $cpath -Name LastAccessTime -Value $_.LastAccessTime
  93. }
  94. }
  95.  
  96.  
  97. $f = Get-ChildItem -Path $src -Recurse -File
  98. $f | % { ## -- Delete files only
  99.  
  100. $apath = $_.FullName -Replace $src,""
  101. $cpath = $dest + $apath
  102.  
  103. If (Test-Path $cpath)
  104. {
  105. Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
  106. }
  107. }
  108.  
  109.  
  110. $d | % { ## -- Delete directories only
  111.  
  112. $apath = $_ -Replace $src,""
  113. $cpath = $dest + $apath
  114.  
  115. If (Test-Path $cpath)
  116. {
  117. Remove-Item $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
  118. }
  119. }
  120.  
  121. $Source = "C:test"
  122. $Destination = "C:test1"
  123. [System.IO.Directory]::Move($Source, $Destination)
  124.  
  125. $source = @"
  126. using System;
  127. using System.IO;
  128.  
  129. public class DirectoryCopyExample
  130. {
  131. public static void DirectoryCopy(string sourceDirName, string destDirName)
  132. {
  133. // Get the subdirectories for the specified directory.
  134. DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  135.  
  136. if (!dir.Exists)
  137. {
  138. throw new DirectoryNotFoundException(
  139. "Source directory does not exist or could not be found: "
  140. + sourceDirName);
  141. }
  142.  
  143. // Since we are deleting the files in the source directory, we need to save the dates before they are modified
  144. DateTime dirCreationTime = Directory.GetCreationTime(sourceDirName);
  145. DateTime dirLastAccessTime = Directory.GetLastAccessTime(sourceDirName);
  146. DateTime dirLastWriteTime = Directory.GetLastWriteTime(sourceDirName);
  147.  
  148. DirectoryInfo[] dirs = dir.GetDirectories();
  149. // If the destination directory doesn't exist, create it.
  150. if (!Directory.Exists(destDirName))
  151. {
  152. Directory.CreateDirectory(destDirName);
  153. }
  154.  
  155. // Get the files in the directory and copy them to the new location.
  156. FileInfo[] files = dir.GetFiles();
  157. foreach (FileInfo file in files)
  158. {
  159. string temppath = Path.Combine(destDirName, file.Name);
  160.  
  161. file.CopyTo(temppath, false);
  162.  
  163. File.SetCreationTime(temppath, File.GetCreationTime(file.FullName));
  164. File.SetLastAccessTime(temppath, File.GetLastAccessTime(file.FullName));
  165. File.SetLastWriteTime(temppath, File.GetLastWriteTime(file.FullName));
  166.  
  167. File.Delete(file.FullName);
  168. }
  169.  
  170. // Recursively copy all sub directories
  171. foreach (DirectoryInfo subdir in dirs)
  172. {
  173. string temppath = Path.Combine(destDirName, subdir.Name);
  174. DirectoryCopy(subdir.FullName, temppath);
  175. }
  176.  
  177. Directory.SetCreationTime(destDirName, dirCreationTime);
  178. Directory.SetLastAccessTime(destDirName, dirLastAccessTime);
  179. Directory.SetLastWriteTime(destDirName, dirLastWriteTime);
  180.  
  181. Directory.Delete(sourceDirName);
  182.  
  183. }
  184. }
  185. "@
  186.  
  187. Add-Type -TypeDefinition $source
  188.  
  189. [DirectoryCopyExample]::DirectoryCopy("C:test", "D:test")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement