小编Tha*_*ama的帖子

如何使用 Windows Powershell 自动打印为 PDF

我有一个文件夹,其中有n个word、excel和powerpoint文件,扩展名为“.Doc、.Docx、.xls、.xlsx、.ppt等”。应使用 Microsoft 打印到 PDF 选项将这些文件的内容转换为 PDF,而不改变其格式,并且输出文件应自动保存到扩展名为 .pdf 的文件夹中。

我已经尝试过使用下面的脚本,它只打印 PDF 中的空白页。

请帮忙。

function ConvertTo-PDF {
    param(
        $TextDocumentPath
    )
    
    Add-Type -AssemblyName System.Drawing
    $doc = New-Object System.Drawing.Printing.PrintDocument
    $doc.DocumentName = $TextDocumentPath
    $doc.PrinterSettings = new-Object System.Drawing.Printing.PrinterSettings
    $doc.PrinterSettings.PrinterName = 'Microsoft Print to PDF'
    $doc.PrinterSettings.PrintToFile = $true
    $file=[io.fileinfo]$TextDocumentPath
    $pdf= [io.path]::Combine($file.DirectoryName, $file.BaseName) + '.pdf'
    $doc.PrinterSettings.PrintFileName = $pdf
    $doc.Print()
    $doc.Dispose()
}
Run Code Online (Sandbox Code Playgroud)

谢谢。

powershell

6
推荐指数
1
解决办法
1万
查看次数

将批处理文件中的波浪号(〜)替换为其他字符时,如何转义呢?

该脚本可以将某些字符替换为文件名中的指定字符,但不适用于tilde ~

以下两个部分工作正常。

@echo off
setlocal enabledelayedexpansion
for %%a in (*_*) do (
    set file=%%a
    ren "!file!" "!file:_=A!"
)
for %%a in (*-*) do (
    set file=%%a
    ren "!file!" "!file:-=B!"
)
Run Code Online (Sandbox Code Playgroud)

但是这部分不起作用。

for %%a in (*~*) do (
    set file=%%a
    ren "!file!" "!file:~=C!"
)
Run Code Online (Sandbox Code Playgroud)

为什么更换~不起作用以及如何解决?

cmd batch-file

2
推荐指数
1
解决办法
79
查看次数

标签 统计

batch-file ×1

cmd ×1

powershell ×1