0

I built a script that converts .doc files to .docx.

I have a problem that when the .doc file is password-protected, I can't access it and then the script hangs.

I am looking for a way to check if the file has a password before I open it.

I using Documents.Open method to open the file.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Doron
  • 45
  • 2
  • 6

2 Answers2

4

If your script hangs on opening the document, the approach outlined in this question might help, only that in PowerShell you'd use a try..catch block instead of On Error Resume Next:

$filename = "C:\path\to\your.doc"

$wd = New-Object -COM "Word.Application"

try {
  $doc = $wd.Documents.Open($filename, $null, $null, $null, "")
} catch {
  Write-Host "$filename is password-protected!"
}

If you can open the file, but the content is protected, you can determine it like this:

if ( $doc.ProtectionType -ne -1 ) {
  Write-Host ($doc.Name + " is password-protected.")
  $doc.Close()
}

If none of these work you may have to resort to the method described in this answer. Rough translation to PowerShell (of those parts that detect encrypted documents):

$bytes  = [System.IO.File]::ReadAllBytes($filename)
$prefix = [System.Text.Encoding]::Default.GetString($bytes[1..2]);

if ($prefix -eq "ÐÏ") {
  # DOC 2005
  if ($bytes[0x20c] -eq 0x13) { $encrypted = $true }

  # DOC/XLS 2007+
  $start = [System.Text.Encoding]::Default.GetString($bytes[0..2000]).Replace("\0", " ")
  if ($start -like "*E n c r y p t e d P a c k a g e") { $encrypted = $true }
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Hello Ansgar Wiechers, Thanks for your commend but at both options I stuck when the script try to open the file. If i let him wrong password he ask for password again and again until i will click on the close button. ( There is no error when i write wrong password so i can't using the catch option). – Doron Jun 30 '13 at 12:23
  • Thanks, solved this for 2007 format in PHP as well, but don't really understand what is happening with the 2005 format: `if ($bytes[0x20c] == 0x13) { $encrypted = $true }`. What about `.doc` support? – Rvanlaak Mar 22 '16 at 14:52
  • 1
    @Rvanlaak Please post a new question. – Ansgar Wiechers Mar 22 '16 at 14:55
  • It would seem to me that this line. $prefix = [System.Text.Encoding]::Default.GetString($bytes[1..2]); should have [0..1] instead of [1..2] as the string we are looking for starts at position zero. – David P Aug 03 '20 at 07:12
0

There is a technique outlined here. Essentially, you supply a fake password which files without a password will ignore; then you error-trap the ones that do require a password, and can skip them.

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • Hello Andy G, I try to do that before but when i try that with a powershell what I got is second chance to write a password. until i click on close on the password wizard that continues ask me for password. – Doron Jun 30 '13 at 11:13
  • @user2536300 See Ansgar's post which contains PowerShell code. – Andy G Jun 30 '13 at 11:16