Forum Discussion
Script to copy MS Excel files from one location to another and keep a log
- Oct 09, 2020
Hi ChrisC365,
Could you try this?
# Input Parameters $Date = Get-Date -Format "dd-MM-yyyy-dddd HH-mm" $SrcFile1 = "filename1.xlsx" $SrcFile2 = "filename2.xlsx" $Srcfolder = "C:\Source\" $Dstfolder = "C:\Destination\" $Logs = "C:\Logs\" # Copy Files From Source to Destination Try { Write-Host "Copying the $SrcFile1" -ForegroundColor Yellow Copy-Item -Path "$SrcFolder\$SrcFile1" -Recurse -Destination "$DstFolder\$Date - $SrcFile1" -ErrorAction SilentlyContinue Write-Host "Completed" -ForegroundColor Green } Catch { Write-Host "$SrcFile1 is not copied. Log File is in $Logs" -ForegroundColor Red Add-Content "$Logs\Log - $Date.txt" -Value "$SrcFile1 not copied because $($Error[0])" } Try { Write-Host "Copying the $SrcFile2" -ForegroundColor Yellow Copy-Item -Path "$SrcFolder\$SrcFile2" -Recurse -Destination "$DstFolder\$Date - $SrcFile2" -ErrorAction SilentlyContinue Write-Host "Completed" -ForegroundColor Green } Catch { Write-Host "$SrcFile2 is not copied. Log File is in $Logs" -ForegroundColor Red Add-Content "$Logs\Log - $Date.txt" -Value "$SrcFile2 not copied because $($Error[0])" }
The files are not automatically generated, apology for any confusion, what I mean is I want to automate the process rather than me manually copying these files across, so for example :
existing file name is "newProjects.xlxs", I want to copy this to a new location and rename the copy as - "24-09-2020 - 100015 - newProjects.xlsx"
existing file name is "oldProjects.xlsx", I want to copy this to a new location and rename the copy as - "24-09-2020 - 100015 - oldProjects.xlsx"
Then next day or week, I repeat the process again as above except the name changes -
"01-10-2020 - 130014 - newProjects.xlsx"
"01-10-2020 - 130014 - oldProjects.xlsx"
Chris
$sourceFile="C:\MySource\lsasetup.log"
$DestFile="C:\MySource\"+(get-date).ToString("ddMMyyyy")+".bak"
$FailLogLocation="C:\MySource\Logcopy.txt"
try{
Write-Host "Copying the file, Please wait..."
Copy-Item $sourceFile -Destination $DestFile
}
Catch{
Write-Host "Ops, Failed"
Add-Content -Path $FailLogLocation -Value $error[0]
}
I try to make it as simple as possible