Jump to content

Small regexpreplace issue


Chimaera
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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

Ok so i need to assign where it writes the filename

$sNewPath = @ScriptDir & "\" & $sFolderName & "\" & StringRegExpReplace($aSubItemCSV[$i][0], '[\/\?<>\\\:\|\*"]', '_') & ".csv"

That seems to work

Thx

Link to comment
Share on other sites

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

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 by Chimaera
Link to comment
Share on other sites

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

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 here
RegExp tutorial: enough to get started
PCRE 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

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