Michiel78 Posted October 3, 2010 Share Posted October 3, 2010 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: expandcollapse popup$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 More sharing options...
Moderators Melba23 Posted October 3, 2010 Moderators Share Posted October 3, 2010 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now