Jump to content

Recommended Posts

Posted

Hello! I'm converting documents to PDF using a script I found - my knowledge of Powershell is very limited. I want to know how to make the script target multiple folders and then copy the converted PDF files into a folder WITHIN the one with the original word documents in and name it PDF or CONVERTED or something similar. Here's the script:

 

$documents_path = 'c:\PDF-Convert\Science\'

 

$word_app = New-Object -ComObject Word.Application

 

# This filter will find .doc as well as .docx documents

Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {

 

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

 

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

 

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

 

$document.Close()

}

 

$word_app.Quit()

Posted (edited)
Hello! I'm converting documents to PDF using a script I found - my knowledge of Powershell is very limited. I want to know how to make the script target multiple folders and then copy the converted PDF files into a folder WITHIN the one with the original word documents in and name it PDF or CONVERTED or something similar. Here's the script:

 

$documents_path = 'c:\PDF-Convert\Science\'

 

$word_app = New-Object -ComObject Word.Application

 

# This filter will find .doc as well as .docx documents

Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {

 

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

 

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

 

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

 

$document.Close()

}

 

$word_app.Quit()

 

I am not a PowerShell guru but to save in a sub folder and change the name I would say you need to edit the line:

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

to be

 

$pdf_filename = "$($_.DirectoryName)\AFOLDER\CONVERTED-$($_.BaseName).pdf"

where AFOLDER is the directory you want it to be in. I presume this folder will be created if it doesn't exist but test before using in anger!

 

To check multiple folders you will need to carry out some recursion but not sure how you would go about that.

 

EDIT: Missed out the rename the file to show it was converted.

Edited by TechMonkey
  • Thanks 1
Posted
I am not a PowerShell guru but to save in a sub folder and change the name I would say you need to edit the line:

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

to be

 

$pdf_filename = "$($_.DirectoryName)\AFOLDER\CONVERTED-$($_.BaseName).pdf"

where AFOLDER is the directory you want it to be in. I presume this folder will be created if it doesn't exist but test before using in anger!

 

To check multiple folders you will need to carry out some recursion but not sure how you would go about that.

 

EDIT: Missed out the rename the file to show it was converted.

 

That doesn't seem to work but thanks for the help!

Posted
I am not a PowerShell guru but to save in a sub folder and change the name I would say you need to edit the line:

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

to be

 

$pdf_filename = "$($_.DirectoryName)\AFOLDER\CONVERTED-$($_.BaseName).pdf"

where AFOLDER is the directory you want it to be in. I presume this folder will be created if it doesn't exist but test before using in anger!

 

To check multiple folders you will need to carry out some recursion but not sure how you would go about that.

 

EDIT: Missed out the rename the file to show it was converted.

 

Haha I replied before I saw your edit - tried it with the extra bit (including creating the directory BEFORE running the script) and it works. It puts the files into a different directory at least which is what I was after so thanks for that! All I need to know now is possibly how to put the script into a folder and when I run it from there - it automatically does all that.

Posted

From the looks of it the first line sets where it will look so either you need to change that line to choose where the script is run from or to ask you for a path name, so you can run the script from anywhere and choose where to run it against. It does pick up any docx file from the folder it is pointed at

 

In fact just did a quick search and it looks like if you use $PSScriptRoot that gets the scripts location. So the first line should say:

$documents_path = $PSScriptRoot

 

Without having tested it I think that should work.

  • Thanks 1
Posted
From the looks of it the first line sets where it will look so either you need to change that line to choose where the script is run from or to ask you for a path name, so you can run the script from anywhere and choose where to run it against. It does pick up any docx file from the folder it is pointed at

 

In fact just did a quick search and it looks like if you use $PSScriptRoot that gets the scripts location. So the first line should say:

$documents_path = $PSScriptRoot

 

Without having tested it I think that should work.

 

You're a legend!

 

I found around the same time the code for creating a folder in the same location too. So the script now creates the PDF folder, converts the files and puts them in there!

 

$documents_path = $PSScriptRoot

New-Item PDF -type directory

 

$word_app = New-Object -ComObject Word.Application

 

# This filter will find .doc as well as .docx documents

Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {

 

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

 

$pdf_filename = "$($_.DirectoryName)\PDF\$($_.BaseName).pdf"

 

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

 

$document.Close()

}

 

$word_app.Quit()

Posted
Haha oh how I love the consistency of teaching staff... I've been send a bulk of Publisher files now. Any idea how I could convert those?? I've tried changing the Word.Application to Publisher.Application and swapped out anywhere it says 'word' for 'publisher' but it doesn't seem to be working.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...