Rename a Word document and save its file name with its first 10 letters

I recovered some Word documents from a damaged hard drive using a piece of software called photorec. The problem is that document names cannot be restored; they are all renamed to a sequence of numbers. There are over 2,000 documents to sort, and I was wondering if I could rename them using some automated process.

Is there a script that I could use to find the first 10 letters in a document and rename them with this? He should be able to cope with several documents that have the same first 10 letters, and therefore do not record documents with the same name. In addition, this would be to avoid renaming the document with illegal characters (for example, "?", "*", "/", Etc.).

I have only a little experience with Python, C and even less with bash programming on Linux, so bear with me if I don’t know exactly what I am doing if I need to write a new script.

+3
source share
3 answers

What about VBScript? Here is a sketch:

FolderName = "C: \ Docs \"

Set fs = CreateObject("Scripting.FileSystemObject")

Set fldr = fs.GetFolder(Foldername)

Set ws = CreateObject("Word.Application")

For Each f In fldr.Files
    If Left(f.name,2)<>"~$" Then
        If InStr(f.Type, "Microsoft Word") Then

        MsgBox f.Name

        Set doc = ws.Documents.Open(Foldername & f.Name)
        s = vbNullString
        i = 1
        Do While Trim(s) = vbNullString And i <= doc.Paragraphs.Count
            s = doc.Paragraphs(i)
            s = CleanString(Left(s, 10))
            i = i + 1
        Loop

        doc.Close False

        If s = "" Then s = "NoParas"
        s1 = s
        i = 1
        Do While fs.FileExists(s1)
            s1 = s & i
            i = i + 1
        Loop

        MsgBox "Name " & Foldername & f.Name & " As " & Foldername & s1 _
            & Right(f.Name, InStrRev(f.Name, "."))
        '' This uses copy, because it seems safer

            f.Copy Foldername & s1 & Right(f.Name, InStrRev(f.Name, ".")), False

            '' MoveFile will copy the file:
        '' fs.MoveFile Foldername & f.Name, Foldername & s1 _
        ''  & Right(f.Name, InStrRev(f.Name, "."))

        End If
    End If
Next

msgbox "Done"
ws.Quit
Set ws = Nothing
Set fs = Nothing

Function CleanString(StringToClean)
''http://msdn.microsoft.com/en-us/library/ms974570.aspx
Dim objRegEx 
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True

''Find anything not a-z, 0-9
objRegEx.Pattern = "[^a-z0-9]"

CleanString = objRegEx.Replace(StringToClean, "")
End Function
+3
source

Word , .

- Python, , ASCII. :

#!/usr/bin/python

import glob
import os

for file in glob.glob("*.doc"):
    f = open(file, "rb")
    new_name = ""
    chars = 0

    char = f.read(1)
    while char != "":
        if 0 < ord(char) < 128:
            if ord("a") <= ord(char) <= ord("z") or ord("A") <= ord(char) <= ord("Z") or ord("0") <= ord(char) <= ord("9"):
                new_name += char
            else:
                new_name += "_"
            chars += 1
            if chars == 100:
                new_name = new_name[:20] + ".doc"
                print "renaming " + file + " to " + new_name
                f.close()
                break;
        else:
            new_name = ""
            chars = 0
        char = f.read(1)

    if new_name != "":
        os.rename(file, new_name)

. , . , , , , , , , .

100 ASCII ( , doc ..), 20 , , , az AZ 0-9 , .

+2

can someone let me know how to execute this as a program and do the job

0
source

Source: https://habr.com/ru/post/1756924/


All Articles