
나는 많은 양의 데이터를 복구한 친구를 도우려고 했으나 메타데이터가 손실되었습니다. 대부분이 기사나 레시피이기 때문에 그녀는 제목이나 텍스트의 첫 번째 줄 정도가 파일 이름으로 충분할 것이라고 믿습니다.
나는 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 #>