18 June,2012 by Tom Collins
Question: I have a nightly ETL , which requires some file manipulation. The requirement is to a) compare the file lengths b) if the file is bigger – backup the original file c) Replace the new original file with the new file .
How can I achieve this in Powershell?
Answer: This powershell script uses Get-item to compare two file sizes , backup file 2 and force a replace if bigger
$file1 = Get-Item 'C:\projects\testing\file1.txt' $file2 = Get-Item 'C:\projects\testing\file2.txt' If ($file1.Length -gt $file2.Length) { Write-Host "The file is bigger" #backup copy of file on another folder Copy-Item $file2 -Destination 'C:\projects\testing\removeItem' Copy-Item $file1 -Destination $file2 -Force }
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.
Posted by: |