unerlaubte zeichen aus dateinamen umwandeln

  • Hallo

    ich will sicherstellen, daß eine datei korrekt gespeichert wird und habe aus diesem grund folgende funktion gebastelt:

    [autoit]

    Global $String=""

    [/autoit][autoit][/autoit][autoit]

    For $i=1 to 255

    [/autoit][autoit][/autoit][autoit]

    $String&=Chr($i)

    [/autoit][autoit][/autoit][autoit]

    Next

    [/autoit][autoit][/autoit][autoit][/autoit][autoit][/autoit][autoit]

    MsgBox(0,"erlaubte zeichen",_FileNameEncode($String))

    [/autoit][autoit][/autoit][autoit]

    MsgBox(0,"TestDateiname",_FileNameEncode(" Test/Dateinaäme?Test<Test>.txt"))

    [/autoit][autoit][/autoit][autoit][/autoit][autoit][/autoit][autoit]

    Func _FileNameEncode($Filename)

    [/autoit][autoit][/autoit][autoit]

    Local Const $ValidChars[256] = _

    [/autoit][autoit][/autoit][autoit]

    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _

    [/autoit][autoit][/autoit][autoit]

    0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, _

    [/autoit][autoit][/autoit][autoit]

    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _

    [/autoit][autoit][/autoit][autoit]

    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, _

    [/autoit][autoit][/autoit][autoit]

    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _

    [/autoit][autoit][/autoit][autoit]

    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _

    [/autoit][autoit][/autoit][autoit]

    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _

    [/autoit][autoit][/autoit][autoit]

    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    [/autoit][autoit][/autoit][autoit]

    Local $sEncoded=""

    [/autoit][autoit][/autoit][autoit]

    For $i = 1 To StringLen($Filename)

    [/autoit][autoit][/autoit][autoit]

    Local $c = StringMid($Filename, $i, 1)

    [/autoit][autoit][/autoit][autoit]

    If Number($ValidChars[Asc($c)]) Then

    [/autoit][autoit][/autoit][autoit]

    $sEncoded &= $c

    [/autoit][autoit][/autoit][autoit]

    Else

    [/autoit][autoit][/autoit][autoit]

    $sEncoded &= StringFormat("_", Asc($c))

    [/autoit][autoit][/autoit][autoit]

    EndIf

    [/autoit][autoit][/autoit][autoit]

    Next

    [/autoit][autoit][/autoit][autoit]

    Return $sEncoded

    [/autoit][autoit][/autoit][autoit]

    EndFunc

    [/autoit]


    aus dem ascii-satz werden nur folgende zeichen zugelassen (alle unerlaubten werden durch "_" ersetzt)

    Code
    ________________________________!__$___()_+,-._0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______abcdefghijklmnopqrstuvwxyz_____________________________________________________________________________________________________________________________________


    weiß zufällig jemand, welche zeichen erlaubt sind und welche dateinamen ungültig, dann könnte man diese funktion noch erweitern...
    momentan lasse ich ja nicht alle gültigen zeichen zu...

    und zb.: der dateiname "LPT1" wäre ungültig!?

    falls sich damit jemand auskennt, dann bitte ich hiermit um ergänzungen ;)

    lgE

    • Offizieller Beitrag

    Hi,

    Spoiler anzeigen
    [autoit]

    #include<GUIConstantsEx.au3>
    #include <GUIConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $GUI = GUICreate("Enter a name for a new folder....", 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
    $file = GUICtrlCreateInput("", 10, 20, 300, 20)
    $btn = GUICtrlCreateButton("Ok", 40, 95, 60, 20)
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    Dim $previousText

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()
    If $msg = $btn Or $msg = $GUI_EVENT_CLOSE Then Exit

    [/autoit] [autoit][/autoit] [autoit]

    $text = GUICtrlRead($file)
    If $previousText <> $text Then ToolTip("")

    [/autoit] [autoit][/autoit] [autoit]

    If StringRegExp($text, '\\|/|:|\*|\?|\"|\<|\>|\|') Then
    GUICtrlSetData($file, StringRegExpReplace($text, '\\|/|:|\*|\?|\"|\<|\>|\|', ""))
    DllCall("user32.dll", "int", "MessageBeep", "int", 0xFFFFFFFF) ;Beep
    Local $tooltipPos = WinGetPos($GUI)
    ToolTip("A file name cannot contain any of the following characters:" & @LF & _
    ' \ / : * ? " < > |', $tooltipPos [0] + 160, $tooltipPos [1] + 60, Default, Default, 3)
    $previousText = GUICtrlRead($file)
    EndIf
    WEnd

    [/autoit]

    Mega