Chimaera Posted November 10, 2013 Share Posted November 10, 2013 Im trying to replace some characters which violate windows filenames with an underscore For $i = 0 To UBound($aSubItemCSV) - 1 If StringRegExp($aSubItemCSV[$i][0], '[\/\?<>\\\:\|\*"]') Then StringRegExpReplace($aSubItemCSV[$i][0], '[\/\?<>\\\:\|\*"]', '_') ;<<<<<<<<<<<<<<<<<<<<<<<<<<< EndIf $sNewPath = @ScriptDir & "\" & $sFolderName & "\" & $aSubItemCSV[$i][0] & ".csv" $hFile = FileOpen($sNewPath, BitOR(8, 1)) If $hFile = -1 Then MsgBox(0, "", "Unable to open file") Exit EndIf $iSuccess = FileWrite($hFile, $aSubItemCSV[$i][1]) FileClose($hFile) If $iSuccess = 0 Then MsgBox(0, "", "Unable to write to file") Exit EndIf Next The highlighted line which ive added seems right but when the code runs and it hits a file with one of the forbidden chars it creates a folder with half the filename inside it ;( Is the regexreplace not right? it seems to be in the helpfile The chars im trying to change is / ? < > : | " * but czardas wrote the original so im sure thats right Any thoughts? If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
jchd Posted November 10, 2013 Share Posted November 10, 2013 You don't assign the result of the highlighted line. Also the test is superfluous. Do the replace in all case. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Chimaera Posted November 10, 2013 Author Share Posted November 10, 2013 Ok so i need to assign where it writes the filename $sNewPath = @ScriptDir & "\" & $sFolderName & "\" & StringRegExpReplace($aSubItemCSV[$i][0], '[\/\?<>\\\:\|\*"]', '_') & ".csv" That seems to work Thx If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
iCode Posted November 10, 2013 Share Posted November 10, 2013 might be worng, but an issue i'm seeing is ":" - i understand escaping the backslash, but you don't need to escape a colon AFAIK, at least not in this case FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences) CODE SNIPPITS: Dynamic tab width (set tab control width according to window width) Link to comment Share on other sites More sharing options...
Chimaera Posted November 10, 2013 Author Share Posted November 10, 2013 (edited) From Msdn for file creation The following reserved characters: < (less than) > (greater than) : (colon) " (double quote) / (forward slash) (backslash) | (vertical bar or pipe) ? (question mark) * (asterisk) Edited November 10, 2013 by Chimaera If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
iCode Posted November 10, 2013 Share Posted November 10, 2013 that's for folder names, but why does a colon need to be escaped in the exp? he has 3 backslashes, so he is escaping 1 backslash and the colon ( :) FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences) CODE SNIPPITS: Dynamic tab width (set tab control width according to window width) Link to comment Share on other sites More sharing options...
jchd Posted November 10, 2013 Share Posted November 10, 2013 That doesn't hurt to escape any literal character. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Chimaera Posted November 10, 2013 Author Share Posted November 10, 2013 It started as an error check for folder creation but it is now this which is only about the folder name For $i = 0 To UBound($aSubItemCSV) - 1 $sNewPath = @ScriptDir & "\" & $sFolderName & "\" & StringRegExpReplace($aSubItemCSV[$i][0], '[\/\?<>\\\:\|\*"]', '-') & ".csv" $hFile = FileOpen($sNewPath, BitOR(8, 1)) If $hFile = -1 Then MsgBox(0, "", "Unable to open file") Exit EndIf $iSuccess = FileWrite($hFile, $aSubItemCSV[$i][1]) FileClose($hFile) If $iSuccess = 0 Then MsgBox(0, "", "Unable to write to file") Exit EndIf Next If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
mikell Posted November 10, 2013 Share Posted November 10, 2013 iCode, In fact the backslash is the only character from this list which needs to be escaped Local $n, $str = '<>:"/\|?*' Do $n += 1 $str = StringRegExpReplace($str, '[/?<>\\:|*"]', $n, 1) Until not @extended Msgbox(0,"", $str) 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