Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Move-FileWithTimestamp {
- [cmdletbinding()]
- param(
- [Parameter(Mandatory=$true,Position=0)][string]$Path,
- [Parameter(Mandatory=$true,Position=1)][string]$Destination
- )
- $origLastAccessTime = ( Get-Item $Path ).LastAccessTime
- $fileName = ( Get-Item $Path ).Name
- Move-Item -Path $Path -Destination $Destination
- $(Get-Item ($Destination+''+$fileName)).LastAccessTime =
- $origLastAccessTime
- }
- function Move-FileWithTimestamp {
- [cmdletbinding()]
- param(
- [Parameter(Mandatory=$true,Position=0)][string]$Path,
- [Parameter(Mandatory=$true,Position=1)][string]$Destination
- )
- $origCreationTime = ( Get-Item $Path ).CreationTime
- $origLastWriteTime = ( Get-Item $Path ).LastWriteTime
- $origLastAccessTime = ( Get-Item $Path ).CreationTime
- $fileName = ( Get-Item $Path ).Name
- Move-Item -Path $Path -Destination $Destination
- $(Get-Item ($Destination+''+$fileName)).CreationTime = $origCreationTime
- $(Get-Item ($Destination+''+$fileName)).LastWriteTime =
- $origLastWriteTime
- $(Get-Item ($Destination+''+$fileName)).LastAccessTime =
- $origLastAccessTime
- }
- function Move-FileWithTimestamp {
- [cmdletbinding()]
- param(
- [Parameter(Mandatory=$true,Position=0)][string]$Path,
- [Parameter(Mandatory=$true,Position=1)][string]$Destination
- )
- $origCreationTime = ( Get-Item $Path ).CreationTime
- $origLastWriteTime = ( Get-Item $Path ).LastWriteTime
- $origLastAccessTime = ( Get-Item $Path ).CreationTime
- $origChildCreationTime = ( Get-ChildItem $Path ).CreationTime
- $origChildLastWriteTime = ( Get-ChildItem $Path ).LastWriteTime
- $origChildLastAccessTime = ( Get-ChildItem $Path ).CreationTime
- $fileName = ( Get-Item $Path ).Name
- Move-Item -Path $Path -Destination $Destination
- $(Get-Item ($Destination+''+$fileName)).CreationTime = $origCreationTime
- $(Get-Item ($Destination+''+$fileName)).LastWriteTime = $origLastWriteTime
- $(Get-Item ($Destination+''+$fileName)).LastAccessTime =
- $origLastAccessTime
- $(Get-ChildItem ($Destination+''+$fileName)) | ForEach-Object {
- $_.CreationTime = $origChildCreationTime }
- $(Get-ChildItem ($Destination+''+$fileName)) | ForEach-Object {
- $_.LastWriteTime = $origChildLastWriteTime }
- $(Get-ChildItem ($Destination+''+$fileName)) | ForEach-Object {
- $_.LastAccessTime = $origChildLastAccessTime }
- }
- $src = "C:SrcFolder123"
- $dest = "X:DestFolder321"
- $src = $src.Replace("","\")
- $i = Get-ChildItem -Path $src -Recurse
- $i | % { ## -- All files and folders
- $apath = $_.FullName -Replace $src,""
- $cpath = $dest + $apath
- Copy-Item -Path $_.FullName -Destination $cpath -Force
- If (Test-Path $cpath)
- {
- Set-ItemProperty -Path $cpath -Name CreationTime -Value $_.CreationTime
- Set-ItemProperty -Path $cpath -Name LastWriteTime -Value $_.LastWriteTime
- Set-ItemProperty -Path $cpath -Name LastAccessTime -Value $_.LastAccessTime
- }
- }
- $d = Get-ChildItem -Path $src -Recurse -Directory
- $d | % { ## -- Folders only
- $apath = $_.FullName -Replace $src,""
- $cpath = $dest + $apath
- If (Test-Path $cpath)
- {
- Set-ItemProperty -Path $cpath -Name CreationTime -Value $_.CreationTime
- Set-ItemProperty -Path $cpath -Name LastWriteTime -Value $_.LastWriteTime
- Set-ItemProperty -Path $cpath -Name LastAccessTime -Value $_.LastAccessTime
- }
- }
- $f = Get-ChildItem -Path $src -Recurse -File
- $f | % { ## -- Delete files only
- $apath = $_.FullName -Replace $src,""
- $cpath = $dest + $apath
- If (Test-Path $cpath)
- {
- Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
- }
- }
- $d | % { ## -- Delete directories only
- $apath = $_ -Replace $src,""
- $cpath = $dest + $apath
- If (Test-Path $cpath)
- {
- Remove-Item $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
- }
- }
- $Source = "C:test"
- $Destination = "C:test1"
- [System.IO.Directory]::Move($Source, $Destination)
- $source = @"
- using System;
- using System.IO;
- public class DirectoryCopyExample
- {
- public static void DirectoryCopy(string sourceDirName, string destDirName)
- {
- // Get the subdirectories for the specified directory.
- DirectoryInfo dir = new DirectoryInfo(sourceDirName);
- if (!dir.Exists)
- {
- throw new DirectoryNotFoundException(
- "Source directory does not exist or could not be found: "
- + sourceDirName);
- }
- // Since we are deleting the files in the source directory, we need to save the dates before they are modified
- DateTime dirCreationTime = Directory.GetCreationTime(sourceDirName);
- DateTime dirLastAccessTime = Directory.GetLastAccessTime(sourceDirName);
- DateTime dirLastWriteTime = Directory.GetLastWriteTime(sourceDirName);
- DirectoryInfo[] dirs = dir.GetDirectories();
- // If the destination directory doesn't exist, create it.
- if (!Directory.Exists(destDirName))
- {
- Directory.CreateDirectory(destDirName);
- }
- // Get the files in the directory and copy them to the new location.
- FileInfo[] files = dir.GetFiles();
- foreach (FileInfo file in files)
- {
- string temppath = Path.Combine(destDirName, file.Name);
- file.CopyTo(temppath, false);
- File.SetCreationTime(temppath, File.GetCreationTime(file.FullName));
- File.SetLastAccessTime(temppath, File.GetLastAccessTime(file.FullName));
- File.SetLastWriteTime(temppath, File.GetLastWriteTime(file.FullName));
- File.Delete(file.FullName);
- }
- // Recursively copy all sub directories
- foreach (DirectoryInfo subdir in dirs)
- {
- string temppath = Path.Combine(destDirName, subdir.Name);
- DirectoryCopy(subdir.FullName, temppath);
- }
- Directory.SetCreationTime(destDirName, dirCreationTime);
- Directory.SetLastAccessTime(destDirName, dirLastAccessTime);
- Directory.SetLastWriteTime(destDirName, dirLastWriteTime);
- Directory.Delete(sourceDirName);
- }
- }
- "@
- Add-Type -TypeDefinition $source
- [DirectoryCopyExample]::DirectoryCopy("C:test", "D:test")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement