Forum Discussion
ChrisC365
Sep 24, 2020Brass Contributor
Script to copy MS Excel files from one location to another and keep a log
Hi Not sure if PowerShell is design for this, I was thinking back to the days when you would create a BAT file. I am wanting to automate the process of copying 2 MS Excel files; each has a se...
- 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])" }
ChrisC365
Oct 08, 2020Brass Contributor
Hi Many thanks
The source folder has many files inside this location, I was just needing to extract two particular files, for example "First filename1.xls" and "Second Filename.xlsx". The script does copy the folder contents but its just two files I need.
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])"
}