Jump to content

Prevent User From Using Reserved Filenames (nul, Etc.)?


Recommended Posts

Is there any built-in way to prevent a script from accepting a filename or directory name that is one of the forbidden "reserved" filenames, such as NUL, CON, LPT1, COM1, etc?

I'd like this code to prevent someone from trying to set a director named NUL, for example. Do I have to list them all explicitly in the script?

Thanks for any help. Code follows:

Func SetTempDir()
    $drv = @HomeDrive
; MsgBox(8192, $t, "home drive letter is " & $drv)
    $dLoop = 1
    $dmtmpdir = ""
    While $dLoop = 1
        $dmtmpdir = InputBox($t, "Folder for temporary printfiles in root directory of drive " & $drv, "temp", "", -1, 140)
        If @error = 1 Then Cancelled()
        If StringLeft($dmtmpdir, 1) = "\" Then $dmtmpdir = StringTrimLeft($dmtmpdir, 1)
        If $dmtmpdir = "" Then
            $a = MsgBox(8241, $t, "You must enter a subdirectory.")
            If $a = 2 Then Cancelled()
        ElseIf $dmtmpdir = "\" Then
            $a = MsgBox(8241, "Error", "You must enter a subdirectory.")
            If $a = 2 Then Cancelled()
        ElseIf StringIsAlNum($dmtmpdir) = 0 Then
            $a = MsgBox(8241, "Error", "Alphanumeric characters only. Please try again.")
            If $a = 2 Then Cancelled()
        ElseIf StringLen($dmtmpdir) > 8 Then
            $a = MsgBox(8241, "Error", "Maximum 8 characters. Please try again.")
            If $a = 2 Then Cancelled()
        Else
            ExitLoop
        EndIf
    WEnd
    $dmtmppath = $drv & "\" & $dmtmpdir
    If FileExists($dmtmppath) = 0 Then
        $a = MsgBox(8225, $t, "Create " & $dmtmppath & " directory?")
        If $a = 2 Then Cancelled()
        $e = DirCreate($dmtmppath)
        If $e = 0 Then
            MsgBox(8209, $t, "Error creating directory.")
            Exit
        EndIf
        MsgBox(8256, $t, $dmtmppath & " created.")
    Else
        $a = MsgBox(8225, $t, "Use existing " & $dmtmppath & " directory?")
        If $a = 2 Then Cancelled()
    EndIf
    MsgBox(8256, $t, "WPDOS printfile must be set to " & $dmtmppath & "\wpdos.ps in WP printer driver.")
EndFunc  ;==>SetTempDir
Link to comment
Share on other sites

  • Moderators

I'm trying to understand what you're wanting, you might try storing all the non-allowed options in an array, and then go through them with either _PathSplit() or StringInStr() or Both.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

maybe

$dtest = StringSplit("\NUL,\CON,\LPT1,\COM1", ",")
For $x = 1 to $dtest[0]
If StringInStr($dmtmpdir, $dtest[$x]) Then
    $a = MsgBox(8241, "Error", $dtest[$x] & " is not allowed. Please try again.")
    If $a = 2 Then Cancelled()
; Exitloop
EndIf
Next

******* not tested

8)

.... SmOke'd me

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

maybe

$dtest = StringSplit("\NUL,\CON,\LPT1,\COM1", ",")
For $x = 1 to $dtest[0]
If StringInStr($dmtmpdir, $dtest[$x]) Then
    $a = MsgBox(8241, "Error", $dtest[$x] & " is not allowed. Please try again.")
    If $a = 2 Then Cancelled()
; Exitloop
EndIf
Next

******* not tested

Valuater,

That's clearly the right direction - many thanks. I've still got to figure out exactly where to put it so that it goes back to the "Enter a directory" prompt when a reserved name is typed in. Also, I need to set it so that it blocks "\NUL" and "\CON" but not "\NULLITY" or "\CONMAN" which are both perfectly all right.

I'll keep experimenting with it. Thanks again!

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