How to check the name of a given file in a variable ?

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi,

    I would like to create a text file where the name would be in a variable.

    This is for my project, I have to create a text file

    and the user has to choose the name of it.

    I don't want to use a window or a control, the user chooses the name and puts it in a text variable.

    Then I have to check that the name is valid, but I don't really know how to do that!

    For example, the user enters the file name in the variable -> FileName -> "ab\c f*z+4/f-1"

    Here, the name is incorrect, "\*+/-" are not accepted in the name of a file !

    Do you understand?

    I would need a function that tells me if the name is valid or not.

    Ex: Resultat = CheckFilename(NameFile$)

  • Use find(string, substring) expression. It returns -1 if the substring was not found and a number >=0 if it was found.

    So you can add several conditions like this:

    find(filename,"/")<0
    find(filename,"\")<0
    find(filename,"?")<0
    find(filename,"*")<0
    .....Set text "File name is valid"
    Else
    .....Set text "Invalid character!"
    
    
  • Hi,

    Oh, yes, indeed, it's so simple, and I hadn't even thought of it.

    Thank you. Thank you dop2000 ;).

  • Is my way of doing things good, optimized ?:

    drive.google.com/open

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This can also be done in one line of code with regex. I'm not very good with regular expressions, but this example from stackoverflow seems to work:

    if RegexSearch( FileName, "^[\w\-. ]+$", "g")<0 ...Set text "Invalid character"
    

    \w means characters [0-9 a-z A-Z _], also allowed are dash "-", dot "." and space.

  • Hello,

    Great, thanks, I don't know the regular expressions either but I know it's very powerful!

    Your example doesn't work with that name, in real life in a file name, it works.

    aàáâããäåbcçdeèêë£fghiìíîïjklmnñoòóôóôõöpqrsštuùúûüµµvwxyýÿ¥zžAÀÁÂÃÄÅBßCÇDEÈÉÊËFGHIÌÌÍÎÏJKLMNÑOÒÓÔÕÖPQRSŠTUÙÚÚÛÜVWXYÝŸZŽ0123456789

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)