Hi ,
i have working script..
i want to put additional checks in the script to check for files and proceed further.
it should check for files after it checks for source folder.
if all three files available then proceed further to run remaining lines of checking backupfolder.
if even one file is missing or all missing it should write log the missing files and exit
Files names to check-
cat.xml
rat.xml
bat.xml
$logdate=Get-Date -Format ddMMMyyyy_HHmmss
$Logfile = "E:\Logs\FileProcessing_$logdate.txt"
function LogWrite ([string]$logstring)
{
Add-content $Logfile -value $logstring
}
#Zip Files
$sourcefolder="E:\INT\PUT"
$backupFolder="E:\INT\Archive"
#check if source exists
if(!(Test-Path $sourcefolder)){
$msj="The source folder ""$($sourcefolder)"" doesn't exists. Exiting..."
LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ERROR:`t$msj"
LogWrite -ForegroundColor Red $msj
exit -1
}
#check the target folder or create it
if(!(Test-Path $backupFolder)){
$msj="The target folder ""$($backupFolder)"" was created"
LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) EXECUTE:`t$msj"
LogWrite -ForegroundColor Magenta $msj
New-Item -ItemType Directory -Path $backupFolder | Out-Null
}
foreach($file in (Get-ChildItem -File $sourcefolder -Filter "*.xml" -Recurse)){
$date = Get-date -Format "ddMMyyyy_HHmmss"
$destinationFile = "$($FILE.Directory.FullName)\$($file.Name).zip"
$MoveFile = "$backupFolder\$($file.BaseName)_$date.xml"
try{
$msj="Compressing File ""$($file.Name)"" into ""$destinationFile"" succeeded"
Compress-Archive -Path $($file.FullName) -CompressionLevel Optimal -DestinationPath $destinationFile -Force
LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) COMPRESS:`t$msj"
#successful compress
$msj="Moving File ""$MoveFile"""
Move-Item -Path $($file.FullName) -Destination $MoveFile -Force
LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ARCHIVE:`t$msj"
}
catch{
$msj="There was an Error: $($_.exception.Message)"
LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ERROR:`t$msj"
LogWrite -ForegroundColor Red $msj
exit -1
}
}
if ((Test-Path $firstfile) -and (Test-Path $secondfile) -and (Test-Path $Thirdfile)) {
Yourcodehere
} else { break }