我想要一个选中的文件夹,但是在此之前,我需要检查该文件夹中是否存在某个文件。如果可以,我需要执行一些操作,如果不需要,请执行其他操作。
我需要每次检查该文件是否存在。因此,这是一个正在运行的过程。我不能执行一次。
预先感谢
到目前为止,我有这个:
$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")}
}