Thursday, January 11, 2024
0 comments

Data Movement Automation via PowerShell Script: Part-2

10:54 AM

After writing 1st Article  which was related to data movement on FTP server from Drive , Today  I will write about Data Movement from Drive to Drive with different conditions using PowerShell Script :



1. Copy Files by checking duplicity on certain time:
 
To copy files from a specific path that were modified 2 days ago, while checking for duplicate files in the destination folder, you can use PowerShell. The script below takes care of copying files and avoids overwriting existing files in the destination folder with the same name. It uses a timestamp to make the copied file names unique in the destination:


# Source and destination paths
$sourcePath = "C:\SourceFolder"
$destinationPath = "C:\DestinationFolder"
# Get files modified 2 days ago
$filesToCopy = Get-ChildItem -Path $sourcePath -File | Where-Object { $_.LastWriteTime.Date -eq (Get-Date).AddDays(-2).Date }

# Copy each selected file to the destination, checking for duplicates
foreach ($file in $filesToCopy) {
$destinationFile = Join-Path $destinationPath "$($file.BaseName)_$((Get-Date).ToString('yyyyMMddHHmmss'))$($file.Extension)"

# Check if the destination file already exists
if (-not (Test-Path -Path $destinationFile)) {
Copy-Item -Path $file.FullName -Destination $destinationFile -Force
Write-Host "Copied $($file.FullName) to $($destinationFile)"
} else {
Write-Host "Skipped $($file.FullName) - Duplicate file already exists in destination."
}
}

Explanation of the script:

  • $sourcePath: Specifies the source folder path.
  • $destinationPath: Specifies the destination folder path.
  • Get-ChildItem: Retrieves the files in the source folder.
  • Where-Object: Filters files based on their LastWriteTime.Date property, ensuring they were modified 2 days ago.
  • foreach: Iterates through each selected file.
  • $destinationFile: Creates a unique name for the copied file in the destination by appending a timestamp to the base file name.
  • Test-Path: Checks if the destination file already exists.
  • Copy-Item: Copies each file to the destination folder only if it doesn't already exist, and appends a timestamp to the file name to make it unique.
2. Delete Files of Certain Time
To delete files from a specific path that were modified a certain number of days ago, you can use PowerShell along with the Get-ChildItem and Remove-Item cmdlets. Here's an example script that deletes files modified a specified number of days ago:

# Path to the folder
$folderPath = "C:\YourFolderPath"

# Number of days ago to consider
$daysAgo = 7
# Get files modified $daysAgo days ago
$filesToDelete = Get-ChildItem -Path $folderPath -File | Where-Object { $_.LastWriteTime.Date -lt (Get-Date).AddDays(-$daysAgo).Date }

# Delete each selected file
foreach ($file in $filesToDelete) {
Remove-Item -Path $file.FullName -Force
Write-Host "Deleted $($file.FullName)"
}

Explanation of the script:

  • $folderPath: Specifies the folder path from which files will be deleted.
  • $daysAgo: Specifies the number of days ago to consider. Files modified before this threshold will be deleted.
  • Get-ChildItem: Retrieves the files in the specified folder.
  • Where-Object: Filters files based on their LastWriteTime.Date, ensuring they were modified more than $daysAgo days ago.
  • foreach: Iterates through each selected file.
  • Remove-Item: Deletes each selected file from the folder.

Adjust the $folderPath and $daysAgo variables according to your requirements. This script deletes files modified more than a certain number of days ago, helping you manage your file system by removing older files.

3. Copying File/Folders After Creating Zip File on Runtime:

If you want to copy folders along with their contents after creating a zip file on runtime using PowerShell, you can use Compress-Archive to create the zip file and then Copy-Item to copy the zip file and additional folders. Here's an example script:




# Source folders
$sourceFolders = @("C:\SourceFolder1", "C:\SourceFolder2")

# Destination folder for the copied folders
$copyDestination = "C:\CopyDestination"

foreach ($folder in $sourceFolders) {
# Get the folder name without the path
$folderName = (Get-Item $folder).Name

# Destination folder for the current folder
$destinationFolder = Join-Path -Path $copyDestination -ChildPath $folderName

# Destination zip file path
$zipFilePath = Join-Path -Path $destinationFolder -ChildPath "$folderName.zip"

# Create a zip file using Compress-Archive
Compress-Archive -Path $folder -DestinationPath $zipFilePath

# Copy the entire folder structure to the destination folder
Copy-Item -Path $folder -Destination $copyDestination -Recurse -Force
}

Write-Host "Folders and zip files copied successfully."

Explanation of the script:

  • $sourceFolders: Specifies an array of source folders to be zipped and copied.
  • $copyDestination: Specifies the destination folder for the copied folders.
  • $folderName: Retrieves the name of each source folder.
  • $destinationFolder: Specifies the destination folder for the current folder being processed.
  • $zipFilePath: Specifies the destination path for the zip file within the current folder.
  • Compress-Archive: Creates a zip file from the specified source folder.
  • Copy-Item: Copies the entire folder structure (including the zip file) to the destination folder.


To be continued Insha'Allah.

0 comments:

 
Toggle Footer