Jump to content

Anyhow to simplify my script?


Go to solution Solved by Marc,

Recommended Posts

this is the script, without using loop or etc, so long
very simple made, but running well

#include <File.au3>
#include <MsgBoxConstants.au3>

Local $sFileName = "test.au3"

 
Local $sFind = "µ"
Local $sReplace = "m"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "∫"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "˙"
Local $sReplace = "h"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "√"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "†"
Local $sReplace = "t"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "‰"
Local $sReplace = "r"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "∂"
Local $sReplace = "d"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ˇ"
Local $sReplace = "t"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "î"
Local $sReplace = "i"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "®"
Local $sReplace = "r"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ß"
Local $sReplace = "s"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ã"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ā"
Local $sReplace = "a"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "å"
Local $sReplace = "a"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ḍ"
Local $sReplace = "d"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ḥ"
Local $sReplace = "h"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ī"
Local $sReplace = "i"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ï"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ù"
Local $sReplace = "h"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "à"
Local $sReplace = "m"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ḷ"
Local $sReplace = "l"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ḹ"
Local $sReplace = "l"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ṃ"
Local $sReplace = "m"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ṇ"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ñ"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ì"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ṅ"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ṛ"
Local $sReplace = "r"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ṝ"
Local $sReplace = "r"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ṣ"
Local $sReplace = "s"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ś"
Local $sReplace = "s"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ṭ"
Local $sReplace = "t"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ā"
Local $sReplace = "a"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ḍ"
Local $sReplace = "d"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ḥ"
Local $sReplace = "h"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ī"
Local $sReplace = "i"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ḷ"
Local $sReplace = "i"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ḹ"
Local $sReplace = "i"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṃ"
Local $sReplace = "m"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṁ"
Local $sReplace = "m"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṇ"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṅ"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṛ"
Local $sReplace = "r"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṝ"
Local $sReplace = "r"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṣ"
Local $sReplace = "s"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ś"
Local $sReplace = "s"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ṭ"
Local $sReplace = "t"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ū"
Local $sReplace = "u"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ä"
Local $sReplace = "a"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "Ç"
Local $sReplace = "s"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "é"
Local $sReplace = "i"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ü"
Local $sReplace = "u"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ö"
Local $sReplace = "t"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ë"
Local $sReplace = "n"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

Local $sFind = "ò"
Local $sReplace = "d"
Local $iRetval = _ReplaceStringInFile($sFileName, $sFind, $sReplace)

This is meant for transliteration, from unreadable font to notepad, or we can say transliteration from rich text to plain text

but i feel the difficulty, as i found more and more words to be translated, so if there is a coding to make this simple, i.e only 2 lines, one from find and another for replace, it would be nice

Thanks

Edited by subuddhi
Link to comment
Share on other sites

You asked for "shorter", not "faster", so... 😀

#include <File.au3>
#include <MsgBoxConstants.au3>

Local $sFileName = "test.au3"
Local $srch = "µ∫˙√†‰∂ˇî®ßÃĀåḌḤĪïùàḶḸṂṆñìṄṚṜṢŚṬāḍḥīḷḹṃṁṇṅṛṝṣśṭūäÇéüöëò"
Local $repl = "mnhntrdtirsnaadhinhmllmnnnnrrsstadhiiimmnnrrsstuasiutnd"

for $i = 1 to StringLen($srch)
    _ReplaceStringInFile($sFileName, StringMid($srch,$i,1), StringMid($repl,$i,1))
Next

To speed it up, I'd read the file once into a string at the start, make all the replaces and write it back.

 

best regards,
Marc

Edited by Marc
type/clearer

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

@Marcyes it is works, thank you so much

well i dont know what the best way to present it, if there is a way to speed it up,would you like to tell how to do it..
yes it is true, sometimes when converting 1000 pages files, it tooks a while..but i no wonder how to make it faster

and also i just try to make, we can choose the file, use this FileOpenDialog,
so i declare variable like this Global $sFileName = "$sFileOpenDialog"

but i dont know, its not working

maybe you can tell me where the wrong is..

 

#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Hold down Ctrl or Shift to choose multiple files."

    ; Display an open dialog to select a list of file(s).
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @ScriptDir & "\", "Text (*.txt)|Au3 (*.au3)", BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT))
    
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.")

        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    Else
        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)

        ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
    Global $sFileName = "$sFileOpenDialog"
        $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)

        ; Display the list of selected files.
         MsgBox($MB_SYSTEMMODAL, "", "You chose the following files:" & @CRLF & $sFileOpenDialog)
    EndIf

EndFunc   ;==>Example

Local $srch = "µ∫˙√†‰∂ˇî®ßÃĀåḌḤĪïùàḶḸṂṆñìṄṚṜṢŚṬāḍḥīḷḹṃṁṇṅṛṝṣśṭūäÇéüöëò"
Local $repl = "mnhntrdtirsnaadhinhmllmnnnnrrsstadhiiimmnnrrsstuasiutnd"

for $i = 1 to StringLen($srch)
    _ReplaceStringInFile($sFileName, StringMid($srch,$i,1), StringMid($repl,$i,1))
Next

there is no error notification, but transliteration doesnt happen

Link to comment
Share on other sites

i am not sure with this part

how exactly to get, the selected file from dialog box, even if that is multiple file, how to select those file and label it as variable..thats i am not sure
 

Quote



Global $sFileName = "$sFileOpenDialog"

 

Edited by subuddhi
Link to comment
Share on other sites

The actual conversion needs to be done in the function. Tried to explain what the script does with some comment-lines:

#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $srch = "µ∫˙√†‰∂ˇî®ßÃĀåḌḤĪïùàḶḸṂṆñìṄṚṜṢŚṬāḍḥīḷḹṃṁṇṅṛṝṣśṭūäÇéüöëò"
    Local $repl = "mnhntrdtirsnaadhinhmllmnnnnrrsstadhiiimmnnrrsstuasiutnd"

    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Hold down Ctrl or Shift to choose multiple files."

    ; Display an open dialog to select a list of file(s).
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @ScriptDir & "\", "Text (*.txt)|Au3 (*.au3)", BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT))

    If @error Then
        Exit MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.")
    Else
        ; split up the selected files into an array
        $sFileOpenDialog = StringSplit($sFileOpenDialog, "|")

        ; walk through the array, convert one file after another
        For $file = 2 To $sFileOpenDialog[0]
            ; get the next filename from the array
            $sFileName = $sFileOpenDialog[$file]
            ; notify us about the file name
            ConsoleWrite("Parsing file: " & $sFileName & @CRLF)
            ; now make all conversions with this file
            For $i = 1 To StringLen($srch)
                _ReplaceStringInFile($sFileName, StringMid($srch, $i, 1), StringMid($repl, $i, 1))
            Next
        Next
    EndIf
EndFunc   ;==>Example

 

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

5 minutes ago, subuddhi said:

i am not sure with this part

how exactly to get, the selected file from dialog box, even if that is multiple file, how to select those file and label it as variable..thats i am not sure
 

 

You need to remove the quotations.

Global $sFileName = $sFileOpenDialog

Thrown this together - not tested:

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <StringConstants.au3>

Global $g_sFilePaths = FileOpenDialog('Open', @ScriptDir, 'Text (*.txt)|Au3 (*.au3)', BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT))
If @ERROR Then
    Exit
EndIf
Global $g_aFiles = StringSplit($g_sFilePaths, '|', $STR_NOCOUNT)
Global $g_sDir = $g_aFiles[0]
; Global $g_sFilePath = @ScriptDir & '\test.au3'
Global $g_sSearch = "µ∫˙√†‰∂ˇî®ßÃĀåḌḤĪïùàḶḸṂṆñìṄṚṜṢŚṬāḍḥīḷḹṃṁṇṅṛṝṣśṭūäÇéüöëò"
Global $g_sReplace = "mnhntrdtirsnaadhinhmllmnnnnrrsstadhiiimmnnrrsstuasiutnd"

Global $g_hFileOpen, $g_sFileRead

For $i = 1 To (UBound($g_aFiles) - 1) Step 1
    $g_hFileOpen = FileOpen($g_sDir & '\' & $g_aFiles[$i], $FO_READ)
    If $g_hFileOpen = -1 Then
        ConsoleWrite('ERROR: FileOpen' & @CRLF)
        Exit
    EndIf
    $g_sFileRead = FileRead($g_hFileOpen)
    FileClose($g_hFileOpen)
    For $j = 1 To StringLen($g_sSearch) Step 1
        $g_sFileRead = StringReplace($g_sFileRead, StringMid($g_sSearch, $j, 1), StringMid($g_sReplace, $j, 1))
    Next
    $g_hFileOpen = FileOpen($g_sDir & '\' & $g_aFiles[$i], $FO_OVERWRITE)
    If $g_hFileOpen = -1 Then
        ConsoleWrite('ERROR: FileOpen' & @CRLF)
        Exit
    EndIf
    FileWrite($g_hFileOpen, $g_sFileRead)
    FileClose($g_hFileOpen)
Next

Edit: This opens and reads each file once rather than multiple times.

Edited by Luke94
Link to comment
Share on other sites

8 minutes ago, Marc said:

The actual conversion needs to be done in the function. Tried to explain what the script does with some comment-lines:

#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $srch = "µ∫˙√†‰∂ˇî®ßÃĀåḌḤĪïùàḶḸṂṆñìṄṚṜṢŚṬāḍḥīḷḹṃṁṇṅṛṝṣśṭūäÇéüöëò"
    Local $repl = "mnhntrdtirsnaadhinhmllmnnnnrrsstadhiiimmnnrrsstuasiutnd"

    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Hold down Ctrl or Shift to choose multiple files."

    ; Display an open dialog to select a list of file(s).
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @ScriptDir & "\", "Text (*.txt)|Au3 (*.au3)", BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT))

    If @error Then
        Exit MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.")
    Else
        ; split up the selected files into an array
        $sFileOpenDialog = StringSplit($sFileOpenDialog, "|")

        ; walk through the array, convert one file after another
        For $file = 2 To $sFileOpenDialog[0]
            ; get the next filename from the array
            $sFileName = $sFileOpenDialog[$file]
            ; notify us about the file name
            ConsoleWrite("Parsing file: " & $sFileName & @CRLF)
            ; now make all conversions with this file
            For $i = 1 To StringLen($srch)
                _ReplaceStringInFile($sFileName, StringMid($srch, $i, 1), StringMid($repl, $i, 1))
            Next
        Next
    EndIf
EndFunc   ;==>Example

 

its work for 2 or more files, i have test up to 5 files

but for 1 files, its not working marc

Link to comment
Share on other sites

  • Solution
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Func convert($file)
    Local $srch = "µ∫˙√†‰∂ˇî®ßÃĀåḌḤĪïùàḶḸṂṆñìṄṚṜṢŚṬāḍḥīḷḹṃṁṇṅṛṝṣśṭūäÇéüöëò"
    Local $repl = "mnhntrdtirsnaadhinhmllmnnnnrrsstadhiiimmnnrrsstuasiutnd"

    Local $check = FileGetAttrib($file)
    If StringInStr($check, "D") Then
        ConsoleWrite("Skipping the directory " & $file & @CRLF)
        Return
    Else
        ConsoleWrite("Parsing file: " & $file & @CRLF)
    EndIf

    ; load file content into memory
    $filereader = FileOpen($file)
    $content = FileRead($filereader)
    FileClose($filereader)

    ; change all characters in memory
    For $i = 1 To StringLen($srch)
        $content = StringReplace($content, StringMid($srch, $i, 1), StringMid($repl, $i, 1))
    Next

    ; write back file to disk
    $filewriter = FileOpen($file, 2)
    FileWrite($filewriter, $content)
    FileClose($filewriter)
EndFunc   ;==>convert

; Display an open dialog to select a list of file(s).
Local $sFileOpenDialog = FileOpenDialog("Hold down Ctrl or Shift to choose multiple files.", @ScriptDir & "\", "Text (*.txt)|Au3 (*.au3)", BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT))

If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.")
; split up the selected files into an array
$sFileOpenDialog = StringSplit($sFileOpenDialog, "|")

; walk through the array, convert one file after another
For $file = 1 To $sFileOpenDialog[0]
    convert($sFileOpenDialog[$file])
Next

Fixed problem with only one file.

Now using the approach of reading the file into memory, convert the characters and writing the file back.

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...