
Ich möchte ein Powershell-Skript erstellen, um PDF-Dateien anhand ihrer jeweiligen pdftotext-Zeilen und/oder pdfinfo-Titel umzubenennen. Zuerst möchte ich eine Auswahlliste mit PDF-Dateien in einem bestimmten Pfad erstellen und aus der Liste eine PDF-Datei auswählen, die ich umbenennen möchte. Kann mir jemand helfen, eine Auswahlliste aus Dateinamen zu erstellen, die über den GCI-Befehl weitergeleitet werden können?
Antwort1
Vielen Dank für alle Kommentare. Hier ist mein Code zum Umbenennen einer PDF-Datei anhand ihres jeweiligen Textes oder ihrer Metadaten. Der Code ist sehr primitiv, da ich kein IT-Fachmann bin; ich habe nur Teile von anderen geliehen und gesammelt.
$x = @() Funktion Read-OpenFileDialog([Zeichenfolge]$WindowTitle, [Zeichenfolge]$InitialDirectory, [Zeichenfolge]$Filter = "Alle Dateien (*.*)|*.*", [Schalter]$AllowMultiSelect) { Add-Type -AssemblyName System.Windows.Forms $openFileDialog = Neues Objekt System.Windows.Forms.OpenFileDialog $openFileDialog.Title = $WindowTitle if (![string]::IsNullOrWhiteSpace($InitialDirectory)) { $openFileDialog.InitialDirectory = $InitialDirectory } $openFileDialog.Filter = $Filter wenn ($AllowMultiSelect) { $openFileDialog.MultiSelect = $false } $openFileDialog.ShowHelp = $true # Ohne diese Zeile kann die Funktion ShowDialog() je nach Systemkonfiguration und Ausführung von der Konsole bzw. ISE hängen bleiben. $openFileDialog.ShowDialog() > $null wenn ($AllowMultiSelect) { return $openFileDialog.Filenames } sonst { return $openFileDialog.Filename } } $file = Read-OpenFileDialog -WindowTitle "PDF-Datei speichern" -InitialDirectory '$path' -Filter "PDF-Dateien (*.pdf)|*.pdf" if (![string]::IsNullOrEmpty($file)) { Write-Host "Sie haben die Datei ausgewählt: $file" } else { "Nachricht senden" } $Pfad= "$Datei"|Geteilter Pfad cd $Pfad [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $objForm = Neues Objekt System.Windows.Forms.Form $objForm.Text = "FORMULAR VERFÜGEN" $objForm.Größe = Neues Objekt System.Drawing.Größe(700,400) $objForm.StartPosition = "Bildschirm zentrieren" $objForm.KeyPreview = $True $objForm.Add_KeyDown({wenn ($_.KeyCode -eq "Eingabe") { $x=$objListbox.SelectedItem;$objForm.Close() } }) $objForm.Add_KeyDown({wenn ($_.KeyCode -eq "Escape") {$objForm.Close()}}) $OKButton = Neues Objekt System.Windows.Forms.Button $OKButton.Location = Neues Objekt System.Drawing.Size(75,220) $OKButton.Größe = Neues Objekt System.Drawing.Größe(75,23) $OKButton.Text = "EVET" $objForm.Controls.Add($OKButton) $OKButton.Add_Click({$x=$objListbox.SelectedItem;$objForm.Close()}) $CancelButton = Neues Objekt System.Windows.Forms.Button $CancelButton.Location = Neues Objekt System.Drawing.Size(200,220) $CancelButton.Size = Neues Objekt System.Drawing.Size(75,23) $CancelButton.Text = "Kontakt" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) $objLabel = Neues Objekt System.Windows.Forms.Label $objLabel.Location = Neues Objekt System.Drawing.Size(10,20) $objLabel.Größe = Neues Objekt System.Drawing.Größe(480,20) $objLabel.Text = "ZUGEHÖRIGER TEIL DES TEXTES:" $objForm.Controls.Add($objLabel) $objListbox = Neues Objekt System.Windows.Forms.Listbox $objListbox.Location = Neues Objekt System.Drawing.Size(10,40) $objListbox.Größe = Neues Objekt System.Drawing.Größe(600,840) $objListBox.Font = "Arial Schwarz" pdftotext -eol dos -l 2 $Datei tmp.txt (gc tmp.txt) | ? {$_.trim() -ne "" } | Inhalt von tmp.txt festlegen $i=pdfinfo "$file" |grep -i Titel|sed -e ‚s/Titel\:[ ]*//g‘ Wenn (![string]::IsNullOrEmpty($i)) { $line6=$i } $line1= (Get-Inhalt tmp.txt)[0] $line2= (Get-Content tmp.txt)[1] $line3= (Get-Content tmp.txt)[2] $line4= (Get-Content tmp.txt)[3] $line5= (Get-Content tmp.txt)[4] [void] $objListbox.Items.Add("$line1") [void] $objListbox.Items.Add("$line2") [void] $objListbox.Items.Add("$line3") [void] $objListbox.Items.Add("$line4") [void] $objListbox.Items.Add("$line5") [void] $objListbox.Items.Add("$line6") $objListbox.Höhe =170 $objForm.Controls.Add($objListbox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() $z= $objListbox.AusgewählteElemente wenn ($z -ne ""){ $z= $z -ersetze '[^a-zA-Z0-9_ ]',"_" $z= $z -ersetze '[ ]',"_" $z= $z -ersetze '_{2,}',"_" $z= $z.Trim() Write-Host "Datei $z.pdf öffnen" Umbenennen des Elements „$file -newname $z“.pdf“ } Sonst {Write-Host "isim değişmedi"} rm tmp.txt Pause $x