Check the validity of the directories and files names

Discussion in 'Scripting' started by balubeto, Jul 8, 2016.

  1. balubeto

    balubeto MDL Addicted

    Dec 22, 2009
    581
    10
    30
    #1 balubeto, Jul 8, 2016
    Last edited by a moderator: Apr 20, 2017
    Hi

    I have this script

    Code:
    @echo off
    setlocal enableExtensions disableDelayedExpansion
    call :setValidPath "Windows_Files_Path" "%~1" "Enter the directory in which put the content of the ""Windows Setup Media"" volume image:"
    call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"
    call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"
    call :setValidFile "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"
    echo(
    echo Result:
    echo Windows_Files_Path: "%Windows_Files_Path%"
    echo iso_Path          : "%iso_Path%"
    echo esd_File_Path     : "%esd_File_Path%"
    echo esd_File          : "%esd_File%"
    :: put your code here
    endlocal
    goto :eof
    
    :setValidPath
    :: %~1 variable to set
    :: %~2 default value
    :: %~3 text if default value is invalid
     setlocal
     set "input=%~2"
     set "text=%~3"
     set "first=1"
    :validatePath
     set "invalid="
     :: validating
     :: example code for invalidating input if the given path does not exist
     if not exist "%input%" set "invalid=The Path you specified for %~1 ^("%input%"^) does not exist."
     :: insert code for additional validity checking
     if not defined invalid endlocal & set "%~1=%input%" & echo Valid: %~1=%input%& goto :eof
     echo(Invalid: %invalid%
     set /P ^"input=%text:""="% "
     goto :validatePath
     
    :setValidFile
    :: %~1 variable to set
    :: %~2 default value
    :: %~3 text if default value is invalid
     setlocal
     set "input=%~2"
     set "text=%~3"
     set "first=1"
    :validateFile
     set "invalid="
     :: validating
     :: example code for invalidating input if the given filename is empty string
     if "" == "%input%" set "invalid=The Filename you specified for %~1 is the empty string."
     :: insert code for additional validity checking
     if not defined invalid endlocal & set "%~1=%input%" & echo Valid: %~1=%input%& goto :eof
     echo(Invalid: %invalid%
     set /P ^"input=%text:""="% "
     goto :validateFile
    
    that should check the validity of directory names (with absolute path) and file in Windows 7 and above and save these elements in the respective variables.

    I noticed that if I insert some valid directories or files but non-existent, the values of these elements are not saved in these variables. Why?

    The validity of these values is used to make sure that the creation of these elements have always success using the above variables with the md and oscdimg commands.

    So, how can I modify this script?

    Thanks

    Bye
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. ofernandofilo

    ofernandofilo MDL Member

    Sep 26, 2015
    236
    140
    10
    #2 ofernandofilo, Jul 8, 2016
    Last edited by a moderator: Apr 20, 2017
    I believe because of this line:
    Code:
    if not exist "%input%" set "invalid=The Path you specified for %~1 ^("%input%"^) does not exist."
    
    Please, try to explain exactly what do you want. Like if you hadn't a script.

    cheers
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. balubeto

    balubeto MDL Addicted

    Dec 22, 2009
    581
    10
    30
    #3 balubeto, Jul 8, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    So, how do I save even the absolute path of the directories that this script will have to create them by checking, however, whether their syntax is valid for Wwindows 7 and above.

    My request is to avoid mistakes during their creation by using their respective variables.

    Thanks

    Bye
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. arnyc23

    arnyc23 MDL Novice

    Oct 12, 2009
    20
    1
    0
    #4 arnyc23, Jul 9, 2016
    Last edited: Jul 9, 2016
    balubeto

    Did you try this: FileNameValidator? It does what you are looking for, and more... I don't think the code you posted will be simpler once all restrictions are counted in.