0

Hi all I require assistance with my Powershell script(I'm new to Powershell). What I'm trying to accomplish is currently I have a script setup to Convert RTF documents to PDF's and save it to a directory. What I want my script to do now is, when my script searches Recursively through my source directory, if a RTF is found, I wish to create the matching directory that it was found in, in my destination directory and save the file in the destinations new directory.

So as follows: If a RTF is found in the following directory C:\users\testuser\folder1\newuser , my current script will convert the documents and save it in C:\users\folder2. So if a file was found in the newuser directory, I want to create a newuser directory in C:\users\folder2\newuser and save the converted document in the directory, Please assist, I'm new to 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 Answer 1

0

Then you will need to use a combination of Split-Path and String.Replace to build a new path from your source file's path and create the new folder and file. A caveat of using String.Replace is that it is case sensitive so your paths need to be converted to a common case, like lower case. An example of using Split-Path and String.Replace is below:

$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

Your new code would look like this:

$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()
2
  • Thank You, for you help, I just have 1 question, in your 2nd post you have $ExampleFile stated, but I don't see it reference anywhere is that suppose to be used in their after split-path. I'm a little confused.
    – Josh Adams
    Commented Dec 20, 2018 at 12:35
  • Ignore my last comment, my brain hasn't caught up with me yet, sorry about that.
    – Josh Adams
    Commented Dec 20, 2018 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .