我试图帮助一位朋友,她恢复了一大堆数据,但元数据丢失了。由于大部分数据都是文章或食谱,她认为标题或文本的第一行左右足以作为文件名。
我想尝试使用 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 #>