Jump to content

Multiple character replacement


Recommended Posts

Currently I am working on a small script that pulls a file from a folder and then replaces the filename with lines from a textfile. The replacement is based on the location of the character and which character it is. For example, if the first character of the filename is "A", then replace it with Line1 from the textfile, if the first character is "B", then replace it with Line2 from the textfile, etc. So far my script looks like this:

$FolderName = "C:/" ;this is the folder to pull the files from

$file = FileOpen("string.txt", 0) ;the file that contains the lines that will replace the original characters
If $file = -1 Then
    MsgBox(0, "Error", "Cannot open the requested file.")
    Exit
EndIf
$sOriginalFileName = "AGFBDC" ;without extension, this file is pulled from $FolderName in the real script
If StringLen($sOriginalFileName) < 5 Then
    
If StringMid($sOriginalFileName, 1,1) = "A" Then
$sLetter1Output = FileReadLine($file, 1)) ElseIf 
StringMid($sOriginalFileName, 1,1) = "B" Then 
$sLetter1Output = FileReadLine($file, 2)) ElseIf 
StringMid($sOriginalFileName, 1,1) = "C" Then
$sLetter1Output = FileReadLine($file, 3)) ElseIf 
StringMid($sOriginalFileName, 1,1) = "D" Endif
;etc untill StringMid($sOriginalFileName, 1,1) = "Z" with FileReadLine($file, 26))

If StringMid($sOriginalFileName, 2,1) = "A" Then ;same procedure for the 2nd character
$sLetter1Output = FileReadLine($file, 27)) ElseIf 
;etc untill all (max. 4) characters from $sOriginalFileName are replaced

Elseif StringLen($sOriginalFileName) > 4 Then

If StringMid($sOriginalFileName, 1,1) = "A" Then
$sLetter1Output = FileReadLine($file, 100)) ElseIf 
StringMid($sOriginalFileName, 1,1) = "B" Then 
$sLetter1Output = FileReadLine($file, 101)) ElseIf 
StringMid($sOriginalFileName, 1,1) = "C" Then
$sLetter1Output = FileReadLine($file, 102)) ElseIf 
StringMid($sOriginalFileName, 1,1) = "D" Endif
;etc untill StringMid($sOriginalFileName, 2,1) = "Z" with FileReadLine($file, 126))

If StringMid($sOriginalFileName, 2,1) = "A" Then ;same procedure for the 2nd character
$sLetter1Output = FileReadLine($file, 127)) ElseIf 
;etc untill all (max. 4) characters from $sOriginalFileName are replaced
EndIf
    
    ;in the real script the line below is FileMove, but here it's replaced with a Messagebox to check if the code above works
    MsgBox(0, "New filename is", $FolderName & "\" & $sLetter1Output & $sLetter2Output & $sLetter3Output) ; & $sLetter4Output, etc
FileClose($file)

But the problem is I keep getting syntaxerrors. I've tried so many other ways of replacing the filename based on it's length, the character and characters position, but none of them worked so far. StringReplace didn't work out, because if an "A" became "AIBH" (from line 1 in textfile), the script also replaced all those new characters when it reached the B, H and I... Does anyone knows how to get my script working without the syntax errors?

Link to comment
Share on other sites

  • Moderators

Michiel78,

Do not change the filename until you have finished parsing it - like this: ;)

$hFile = FileOpen("string.txt", 0) ;the file that contains the lines that will replace the original characters
If $hFile = -1 Then
    MsgBox(0, "Error", "Cannot open the requested file.")
    Exit
EndIf

$sOriginalFileName = "AEFBDC" ;without extension, this file is pulled from $FolderName in the real script

; Blank new name
$sNewFilename = ""

For $i = 1 To 4
    ; Get current letter
    $sLetter = StringMid($sOriginalFileName, $i, 1)
    ; Convert to an index
    $iIndex = Asc(StringUpper($sLetter)) - 64
    ; Add the line required to the new name
    $sNewFilename &= FileReadLine($hFile, $iIndex)
Next

MsgBox(0, "New Name", $sNewFilename)

FileClose($hFile)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...