如果文件存在,则 Powershell 脚本创建文件夹

如果文件存在,则 Powershell 脚本创建文件夹

大家好,我需要有关 Powershell 脚本的帮助(我是 Powershell 新手)。我目前尝试完成的是,我有一个脚本设置,用于将 RTF 文档转换为 PDF 并将其保存到目录中。我现在希望我的脚本执行的操作是,当我的脚本递归搜索我的源目录时,如果找到 RTF,我希望在我的目标目录中创建找到它的匹配目录,并将文件保存在目标新目录中。

因此如下:如果在以下目录中发现 RTF C:\users\testuser\folder1\newuser,则我当前的脚本将转换文档并将其保存在中C:\users\folder2。因此,如果在 newuser 目录中发现文件,我想在中创建一个 newuser 目录C:\users\folder2\newuser并将转换后的文档保存在目录中,请帮忙,我是 powershell 新手。

$source = "C:\users\testuser\folder1\"
$destination = "C:\users\testuser\folder2\"


$word_app = New-Object -ComObject word.application



#Convert RTF to PDF

Get-ChildItem -Path $source -Filter *.rtf? -Recurse | ForEach-Object {

    $document = $word_app.Documents.Open($_.FullName)

    $pdf_filename = "$destination\$($_.BaseName).pdf"

    $document.SaveAs([ref] $pdf_filename, [ref] 17)

    $document.Close()

}

$word_app.Quit()

答案1

然后,您需要结合使用 Split-Path 和 String.Replace 从源文件路径构建新路径并创建新文件夹和文件。使用 String.Replace 的一个注意事项是它区分大小写,因此您的路径需要转换为常见大小写,例如小写。使用 Split-Path 和 String.Replace 的示例如下:

$Source = "C:\Users\testuser\Folder1"
$Destination = "C:\Users\testuser\Folder2"

$ExampleFile = $Source + "\newuser\thisfile.rtf"

#Use a combination of Split-Path and String.Replace
#Split-Path will take the full path to your file, and return just the parent folder path.
#String.Replace will then remove the $Source path from the remaining path.
#Finally we concatenate the new path with the $Destination path to get our new folder path.

$NewFolder = Split-Path $ExampleFile
Write-Host $NewFolder
# Output: C:\Users\testuser\Folder1\newuser

$NewFolder = $NewFolder.ToLower().Replace($Source.ToLower(), "")
Write-Host $NewFolder
# Output: \newuser

$NewFolder = $Destination + $NewFolder
Write-Host $NewFolder
# Output: C:\Users\testuser\Folder2\newuser

#Or, more simply:
$NewFolder = $($Destination + $(Split-Path $ExampleFile).ToLower().Replace($Source.ToLower(), ""))
Write-Host $NewFolder
# Output: C:\Users\testuser\Folder2\newuser

#Create the folder with ErrorAction = SilentlyContinue so that it doesn't throw an error if the folder already exists:
New-Item -Path $NewFolder -Type directory -ErrorAction SilentlyContinue

#Or, combine everything together and you have:
New-Item -Path $($Destination + $(Split-Path $ExampleFile).ToLower().Replace($Source.ToLower(), "")) -Type directory -ErrorAction SilentlyContinue

您的新代码将如下所示:

$source = "C:\users\testuser\folder1\"
$destination = "C:\users\testuser\folder2\"

$word_app = New-Object -ComObject word.application

#Convert RTF to PDF

Get-ChildItem -Path $source -Filter *.rtf? -Recurse | ForEach-Object {

    $document = $word_app.Documents.Open($_.FullName)

    #Create new path and filename from source path and filename
    $pdf_filename = $($Destination + $(Split-Path $ExampleFile).ToLower().Replace($Source.ToLower(), "")) + "\" + $_.BaseName + ".pdf"

    #Create new folder tree
    New-Item -path $(Split-Path $pdf_filename) -Type directory -ErrorAction SilentlyContinue

    $document.SaveAs([ref] $pdf_filename, [ref] 17)

    $document.Close()

}

$word_app.Quit()

相关内容