Я хочу иметь отмеченную папку, но перед этим мне нужно проверить, существует ли определенный файл в этой папке.Если это произойдет, мне нужно выполнить некоторые действия, если не сделать другие.
И мне нужно проверить, существует ли этот файл каждый раз.Так что это запущенный процесс.Я не могу просто выполнить один раз.
Заранее спасибо
Пока у меня есть это:
$root= [Environment]::GetFolderPath("Desktop")
$foldertmp = "$root\UNB\TMP\"
$filter = 'FACT_TEMPORAL.TXT' # <-- set this according to your requirements
$PDFDestination = "$root\UNB\NO_PROCESADO\"
#C:\Users\JuanMa\Desktop\UNB\TMP
Write-Host "EL ROOT ES $root" -fore white
$fsw = New-Object IO.FileSystemWatcher $foldertmp, $filter -Property @{
IncludeSubdirectories = $true # <-- set this according to your requirements
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' at '$path'was $changeType at $timeStamp" -fore white
comprobar
limpia
printpdf
renombra
borrar
}
function comprobar()
{
if(!(Test-Path 'C:\Users\JuanMa\Desktop\UNB\TMP\FACT_TEMPORAL.TXT')){
Write-Host "No hay archivo temporal. Proceso correcto" -fore white
Get-Printer -Name "UNBILLING" | select Name, PrinterStatus, JobCount, DriverName
}
if((Test-Path 'C:\Users\JuanMa\Desktop\UNB\TMP\FACT_TEMPORAL.TXT')){
Write-Host "Archivo existe. Error" -fore white
Get-Printer -Name "UNBILLING" | select Name, PrinterStatus, JobCount, DriverName
limpia
renombra_noprocesado
borrar
}
}
function limpia()
{
$regex = [System.Text.RegularExpressions.Regex]
write-host "FUNCION LIMPIA ARCHIVO TXT" -fore white
$a = get-content $root\UNB\TMP\FACT_TEMPORAL.txt |
Foreach-Object { $regex::Replace($_, '^[.! pÿ>i@]{1,}', '') } |
Foreach-Object { $regex::Replace($_, '^0 {2,}', '') } |
Foreach-Object { $regex::Replace($_, '>', '') } |
Foreach-Object { ($_.TrimEnd() )} | Where-Object {$_ -ne ""} |
set-content $root\UNB\FINAL_TEXTO\FACT_FINAL.txt
}
function printpdf()
{
write-host "FUNCION IMPRIMIR A PDF" -fore white
start-process -filepath "$root\UNB\FINAL_TEXTO\FACT_FINAL.txt" -verb print
}
function borrar()
{
write-host "FUNCION BORRAR" -fore white
#Start-Sleep -s 2
Remove-Item $root\UNB\TMP\FACT_TEMPORAL.txt
}
function renombra()
{
write-host "FUNCION RENOMBRAR ARCHIVO" -fore white
Get-Item $root\UNB\FINAL_TEXTO\FACT_FINAL.txt | Rename-Item -NewName {("print-"+'{0:yyyyMMddhhmmss}{1}' -f (Get-Date),".txt")}
}
function renombra_noprocesado()
{
write-host "FUNCION RENOMBRAR ARCHIVO NO PROCESADO" -fore white
Move-Item $root\UNB\FINAL_TEXTO\FACT_FINAL.txt $PDFDestination
Get-Item $root\UNB\NO_PROCESADO\FACT_FINAL.txt | Rename-Item -NewName {("print-"+'{0:yyyyMMddhhmmss}{1}' -f (Get-Date),".txt")}
}