我有一个由我编写的 powershell 脚本,它查看文件夹中新添加的文件,然后将这些文件移动到另一个文件夹,然后使用 ghostscript 将其转换为 .tif。之后,所有 .pdf 都会移动到 PDF 文件夹,所有 tiff 都会移动到 TIF 文件夹。完成后,它会将日志条目写入 txt 文件。放入 Import 文件夹的任何新文件都会触发此脚本再次运行。
我遇到的问题:
- 转换后的 Tiff 在文件名前面有“TIF”,我不想要这个,只想和创建它的 pdf 同名。
- 当转换第二个文件时,它会创建最后一个转换文件的副本并将其粘贴在根文件夹中。不知道为什么会这样。但我只想要一份 tif 副本,并将其放在 TIF 文件夹中。
希望有人能告诉我我做错了什么!
代码:
### 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}
答案1
对于问题/答案的编辑,我深表歉意。
这是工作脚本。它有相当多的更改,但执行效果符合要求。在开发中期,我被要求将 .pdf 更改为 .tiff 再更改为 .txt 再更改为 .jpg。这个过程仍然运行良好,您只需修改 Ghost 脚本部分即可获得所需的输出类型。
由于得到了其他线程的大量帮助,因此该脚本的相当一部分内容是由许多不同的人拼凑起来的。
所需组件:
- 电源外壳
- CutePDF 作家
- GhostScript
此脚本的作用如下:
- 扫描文件夹并记录文件
- 将 .txt 文件移动到根文件夹进行处理
- 使用 CutePDF Writer 将 .txt 文件打印为 .pdf
- 使用 Ghostscript 将 .pdf 文件转换为 .jpg
- 将 .pdf、.jpg 和 .txt 文件移动到单独的文件夹
- 删除所有 .pdf
- 如果发现更改,则开始对“处理”文件夹进行更改扫描并再次运行脚本。
我将其转换为 .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}