嘗試根據第一行左右的文字重命名大量 .doc 文件

嘗試根據第一行左右的文字重命名大量 .doc 文件

我試圖幫助一位朋友,他恢復了一堆數據,但元數據丟失了。由於其中大部分是文章或食譜,她認為標題或第一行左右的文字對於文件名來說就足夠了。

我想嘗試使用 powershell 腳本來...存取/讀取文件,抓取第一行(如果可能的話定義字元長度),然後重命名。就像...讀取前 10 個字元並重命名該檔案。

我找到了這個腳本,它似乎是針對 .txt 檔案的。是否有可能將其重新加工為 .doc,然後刪除有關 O 的部分,然後讓它讀取第一行的內容並使用讀取的前 10 個字元進行重命名?

任何幫助將不勝感激。 (如果我搞砸了腳本的發布,我深表歉意)

$myFolderFullOfTextFiles = 'C:\recoveredDocs'
$linesToReadInEachTextFile = 5

$myTextFiles = Get-ChildItem -Path $myFolderFullOfTextFiles

foreach( $textFile in $myTextFiles )
{
$newName = ''

foreach( $line in $(Get-Content -Path $textFile.FullName -Head $linesToReadInEachTextFile) )
{
    if( $line -like 'O*' )
    {
       $newName = $textFile.DirectoryName + '\' + $line.Substring(0,6) + '.txt'
    }
}

try
{
    Write-Host $newName
    Rename-Item -Path $textFile.FullName -NewName $newName
}
catch
{
    Write-Host "Failed to rename $textFile."
}

}

我也找到了這個腳本。哪個比較注重 .doc 。我所需要的只是......讀取文字的第一行是什麼,將其重命名(對字元進行一些合理的限制,例如前 10 個字元)。

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

strText = objDoc.Paragraphs(1).Range.Text
arrText = Split(strText, vbTab)
intIndex = Ubound(arrText)
strUserName = arrText(intIndex)

arrUserName = Split(strUserName, " ")
intLength = Len(arrUserName(1))
strName = Left(arrUserName(1), intlength - 1)

strUserName = strName & ", " & arrUserName(0)

strText = objDoc.Paragraphs(2).Range.Text
arrText = Split(strText, vbTab)
intIndex = Ubound(arrText)

strDate = arrText(intIndex)
strDate = Replace(strDate, "/", "")

intLength = Len(strDate)
strDate = Left(strDate, intlength - 1)

strFileName = "C:\Scripts\" &  strUserName & " " & strDate & ".doc"

objWord.Quit

Wscript.Sleep 5000

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\Scripts\Test.doc", strFileName

答案1

複製以下程式碼並透過命名擴展名為.ps1 的檔案來建立為powershell 腳本(在Windows 7 上使用powershell 4 測試正常- 使用「get-host|Select-Object version」或「$PSVersionTable.PSVersion」檢查您的powershell 版本)

 $word_app = New-Object -ComObject Word.Application     <# New word application #>
    $source = 'C:\recoveredDocs'    <# create the source variable #>
    $destination = 'C:\renamedDocs' <# create the destination variable #>

    if (!(Test-Path -path $destination)) {  <# check to see if destination folder exists #>
    New-Item -path $destination\ -type directory -Force  } <# create destination folder if it doesn't already exist #>
    echo 'checking files to convert...'

    <# filter for word .doc files only #>
    Get-ChildItem -Path $source -Filter *.doc? | ForEach-Object {
    if (!(Test-Path "$destination\$($_.BaseName).doc")) {   <# check to see if file is already in destination folder (Note. "!" is a PS Alias for "-Not") #>

    $document = $word_app.Documents.Open($_.FullName)   <# open word document #>

    $pattern = '[^a-zA-Z1234567890 ]'   <# create regex pattern of allowed characters #>

    $textstring = $document.range().text <# get the text string from the document #>

    $titlestring = $textstring -replace $pattern, ''    <# apply the regex pattern to eliminate the reserved characters #>

    $title = $titlestring.substring(0, [System.Math]::Min(10, $titlestring.Length)) <# limit the string to 10 characters #>

    $doc_strNewName = "$destination\$($title).doc"  <# create the new name and path for the doc #>

    echo "$($_.FullName) converted to  $doc_strNewName"

$document.SaveAs([ref] $doc_strNewName, [ref] 0)    <# save the document with new name and path #>

$document.Close()   <# close documnet #>

        }
    }

    echo "No More Files to Convert"

$word_app.Quit()    <# close the word application #>

相關內容