新しく追加されたファイルのフォルダーを調べ、それらのファイルを別のフォルダーに移動し、ghostscriptを使用して.tifに変換するPowerShellスクリプトを一緒に作成しました。その後、すべての.pdfがPDFフォルダーに移動され、すべてのtiffがTIFフォルダーに移動されます。完了すると、txtファイルへのログエントリが作成されます。新しいファイルがドロップされると、フォルダーのインポートにより、このスクリプトが再度実行されます。
私が抱えている問題:
誰かが私が間違っていることを教えてくれることを願っています!
コード:
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Folder\Import"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
Add-content "C:\Folder\log.txt" -value $logline
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.26\bin\gswin64c.exe'
#Directory containing the PDF files that will be converted
$inputDir = 'C:\Folder\'
#.pdf Files
$pdfDir = 'C:\Folder\*.pdf'
#.tif Files
$tifDir = 'C:\Folder\*.tif'
#Directory catchall for all incoming files.
$dumpDir = 'C:\Folder\Import\*.*'
#Output path where converted PDF files will be stored
$pdfOutputDir = 'C:\Folder\PDF'
#Output path where the TIF files will be saved
$tifOutputDir = 'C:\Folder\TIF'
Get-ChildItem -Path $dumpDir -Recurse -File | Move-Item -Destination $inputDir
$pdfs = get-childitem $inputDir -recurse | where {$_.Extension -match "pdf"}
foreach($pdf in $pdfs)
{
$tif = $tifOutputDir + $pdf.BaseName + ".tif"
if(test-path $tif)
{
"tif file already exists " + $tif
}
else
{
'Processing ' + $pdf.Name
$param = "-sOutputFile=$tif"
& $tool -q -dNOPAUSE -sDEVICE=tiffg4 $param -r300 $pdf.FullName -c quit
}
Get-ChildItem -Path $pdfDir -Recurse -File | Move-Item -Destination $pdfOutputDir
Get-ChildItem -Path $tifDir -Recurse -File | Move-Item -Destination $tifOutputDir
}
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}
質問/回答の編集についてお詫び申し上げます。
これが作業スクリプトです。かなりの数の変更がありますが、必要な方法で実行されます。開発の途中で、.pdfから.tiff、.txt、.jpgに変更するように求められました。このプロセスは依然として非常にうまく機能し、Ghostスクリプト部分を変更して目的の出力タイプを取得する必要があります。
他のスレッドからの多くの助けがあるので、このスクリプトのかなりの部分が、多数の異なる人々から一緒にフランケンシュタイン化されています。
必要なコンポーネント:
このスクリプトの機能を実行します:
これを.exeに変換し、NSSMを使用して、常に実行するサービスとして設定しました。
$freshStart = 0
$PrintPageHandler ={
param([object]$sender, [System.Drawing.Printing.PrintPageEventArgs]$ev)
$linesPerPage = 0
$yPos = 0
$count = 0
$leftMargin = $ev.MarginBounds.Left
$topMargin = $ev.MarginBounds.Top
$line = $null
$printFont = New-Object System.Drawing.Font "Arial", 10
# Calculate the number of lines per page.
$linesPerPage = $ev.MarginBounds.Height / $printFont.GetHeight($ev.Graphics)
# Print each line of the file.
while ($count -lt $linesPerPage -and (($line = $streamToPrint.ReadLine()) -ne $null))
{
$yPos = $topMargin + ($count * $printFont.GetHeight($ev.Graphics))
$ev.Graphics.DrawString($line, $printFont, [System.Drawing.Brushes]::Black, $leftMargin, $yPos, (New-Object System.Drawing.StringFormat))
$count++
}
# If more lines exist, print another page.
if ($line -ne $null)
{
$ev.HasMorePages = $true
}
else
{
$ev.HasMorePages = $false
}
}
While ($freshStart -eq 0)
{
$prossDir = 'C:\FINAL\PROCESSING\'
$files = get-childitem -Path $prossDir | where {$_.Extension -match "txt"}
foreach($file in $files)
{
$path = "C:\FINAL\PROCESSING\$file"
$logline = "$(Get-Date), BackLog, FINAL, $path"
Add-content "C:\LOG\log.txt" -value $logline
}
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.26\bin\gswin64c.exe'
#Directory containing the PDF files that will be converted
$inputDir = 'C:\FINAL\'
#.pdf Files
$pdfDir = 'C:\FINAL\*.pdf'
#.jpg Files
$jpgDir = 'C:\FINAL\*.jpg'
#.txt Files
$txtDir = 'C:\FINAL\*.txt'
#Directory that deletes all old pdfs
$deleteME = 'C:\FINAL\DELETE\*.pdf'
#Directory catchall for all incoming files.
$dumpDir = 'C:\FINAL\PROCESSING\*.*'
#Output path where converted PDF files will be stored
$pdfOutputDir = 'C:\FINAL\DELETE'
#Output path where the JPG files will be saved
$jpgOutputDir = 'C:\FINAL\Folder2'
#Output path where the TXT files will be saved
$txtOutputDir = 'C:\FINAL\Folder1'
Get-ChildItem -Path $dumpDir -File | Move-Item -Destination $inputDir
function Out-Pdf
{
param($InputDocument, $OutputFolder)
Add-Type -AssemblyName System.Drawing
$doc = New-Object System.Drawing.Printing.PrintDocument
$doc.DocumentName = $InputDocument.FullName
$doc.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
$doc.PrinterSettings.PrinterName = 'CutePDF Writer'
$doc.PrinterSettings.PrintToFile = $true
$streamToPrint = New-Object System.IO.StreamReader $InputDocument.FullName
$doc.add_PrintPage($PrintPageHandler)
$doc.PrinterSettings.PrintFileName = "$($InputDocument.DirectoryName)\$($InputDocument.BaseName).pdf"
$doc.Print()
$streamToPrint.Close()
}
Get-Childitem -Path "C:\FINAL" -File -Filter "*.txt" |
ForEach-Object { Out-Pdf $_ $_.Directory }
$pdfs = get-childitem $inputDir | where {$_.Extension -match "pdf"}
foreach($pdf in $pdfs)
{
$jpg = $inputDir + $pdf.BaseName + ".jpg"
$cJpg = $inputDir + $pdf.BaseName + "_" + "%03d" + ".jpg"
if(test-path $jpg)
{
"jpg file already exists " + $jpg
}
else
{
'Processing ' + $pdf.Name
$param = "-sOutputFile=$cJpg"
& $tool -q -dNOPAUSE -sDEVICE=jpeg $param -r300 $pdf.FullName -c quit
}
}
Get-ChildItem -Path $pdfDir -Recurse -File | Move-Item -Destination $pdfOutputDir
Get-ChildItem -Path $jpgDir -Recurse -File | Move-Item -Destination $jpgOutputDir
Get-ChildItem -Path $txtDir -Recurse -File | Move-Item -Destination $txtOutputDir
Remove-Item -Path $deleteME
$freshStart = 1
}
### SET Folder TO WATCH + FILES TO WATCH + SubFolder YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\FINAL\PROCESSING"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action =
{
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, FINAL, $path"
Add-content "C:\LOG\log.txt" -value $logline
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.26\bin\gswin64c.exe'
#Directory containing the PDF files that will be converted
$inputDir = 'C:\FINAL\'
#.pdf Files
$pdfDir = 'C:\FINAL\*.pdf'
#.jpg Files
$jpgDir = 'C:\FINAL\*.jpg'
#.txt Files
$txtDir = 'C:\FINAL\*.txt'
#Directory that deletes all old pdfs
$deleteME = 'C:\FINAL\DELETE\*.pdf'
#Directory catchall for all incoming files.
$dumpDir = 'C:\FINAL\PROCESSING\*.*'
#Output path where converted PDF files will be stored
$pdfOutputDir = 'C:\FINAL\DELETE'
#Output path where the JPG files will be saved
$jpgOutputDir = 'C:\FINAL\Folder2'
#Output path where the TXT files will be saved
$txtOutputDir = 'C:\FINAL\Folder1'
Get-ChildItem -Path $dumpDir -File | Move-Item -Destination $inputDir
$PrintPageHandler ={
param([object]$sender, [System.Drawing.Printing.PrintPageEventArgs]$ev)
$linesPerPage = 0
$yPos = 0
$count = 0
$leftMargin = $ev.MarginBounds.Left
$topMargin = $ev.MarginBounds.Top
$line = $null
$printFont = New-Object System.Drawing.Font "Arial", 10
# Calculate the number of lines per page.
$linesPerPage = $ev.MarginBounds.Height / $printFont.GetHeight($ev.Graphics)
# Print each line of the file.
while ($count -lt $linesPerPage -and (($line = $streamToPrint.ReadLine()) -ne $null))
{
$yPos = $topMargin + ($count * $printFont.GetHeight($ev.Graphics))
$ev.Graphics.DrawString($line, $printFont, [System.Drawing.Brushes]::Black, $leftMargin, $yPos, (New-Object System.Drawing.StringFormat))
$count++
}
# If more lines exist, print another page.
if ($line -ne $null)
{
$ev.HasMorePages = $true
}
else
{
$ev.HasMorePages = $false
}
}
function Out-Pdf
{
param($InputDocument, $OutputFolder)
Add-Type -AssemblyName System.Drawing
$doc = New-Object System.Drawing.Printing.PrintDocument
$doc.DocumentName = $InputDocument.FullName
$doc.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
$doc.PrinterSettings.PrinterName = 'CutePDF Writer'
$doc.PrinterSettings.PrintToFile = $true
$streamToPrint = New-Object System.IO.StreamReader $InputDocument.FullName
$doc.add_PrintPage($PrintPageHandler)
$doc.PrinterSettings.PrintFileName = "$($InputDocument.DirectoryName)\$($InputDocument.BaseName).pdf"
$doc.Print()
$streamToPrint.Close()
}
Get-Childitem -Path "C:\FINAL" -File -Filter "*.txt" |
ForEach-Object { Out-Pdf $_ $_.Directory }
$pdfs = get-childitem $inputDir | where {$_.Extension -match "pdf"}
foreach($pdf in $pdfs)
{
$jpg = $inputDir + $pdf.BaseName + ".jpg"
$cJpg = $inputDir + $pdf.BaseName + "_" + "%03d" + ".jpg"
if(test-path $jpg)
{
"jpg file already exists " + $jpg
}
else
{
'Processing ' + $pdf.Name
$param = "-sOutputFile=$cJpg"
& $tool -q -dNOPAUSE -sDEVICE=jpeg $param -r300 $pdf.FullName -c quit
}
}
Get-ChildItem -Path $pdfDir -Recurse -File | Move-Item -Destination $pdfOutputDir
Get-ChildItem -Path $jpgDir -Recurse -File | Move-Item -Destination $jpgOutputDir
Get-ChildItem -Path $txtDir -Recurse -File | Move-Item -Destination $txtOutputDir
Remove-Item -Path $deleteME
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}