Check the validity of the directories and files names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Check the validity of the directories and files names

#16 Post by thefeduke » 11 Jul 2016 16:03

aGerman wrote:German language has some funny characters too (ÄÖÜäöüß) which may cause problems.
I stumbled upon an approach that might sidestep this language related problem. I cannot test it. The function is simple: It returns an error message variable if a specified absolute path does not exist in the current environment.

Code: Select all

:VetDir
:: %~1   variable to set
:: %~2   folder to check
@echo off
   If "%~1" EQU "" echo.No first argument for Fail message. Exiting.&Exit /B 1
   setlocal
   set "input=%~2"
   If "%input%" EQU "" endlocal & set "%~1=Failed: filename is an empty string." &Exit /B 1
   if "%input:~1,2%" neq ":\" endlocal & set "%~1=Failed: filename is not absolute." &Exit /B 1

   :: test for existing folder                                   
   2>nul PushD "%input%"
   if errorlevel 1 (endlocal&set "%~1=Failed: filename is not a folder.") Else (PopD&endlocal&set "%~1=")
   Exit /B


If it is called from the command line like:

Code: Select all

call vetdir invalid g:\scripts
then the variable 'invalid' should be undefined if all is OK.

penpen wrote: Replace the lines containing

Code: Select all

   :: insert code for additional validity checking
with
this, my suggested inline call:

Code: Select all

call :vetdir invalid "%input%"
and include my function code above at the end of penpen's script.

John A.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check the validity of the directories and files names

#17 Post by aGerman » 12 Jul 2016 04:37

This wouldn't work around different encodings of the text editor and the cmd console. Characters that are inside of the range of printable ASCII characters won't ever cause problems. As soon as you exceed the ASCII value of a tilde (~) it's getting critical :wink:

Regards
aGerman

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Check the validity of the directories and files names

#18 Post by balubeto » 13 Jul 2016 10:26

I created this script

Code: Select all

@echo off &setlocal DisableDelayedExpansion

call :loop "Windows_Files_Path" "Enter the directory in which put the content of the Windows Setup Media volume image:"
call :loop "iso_Path" "Enter the directory in which put the iso image file created:"
call :loop "esd_File_Path" "Enter the directory in which put the esd unencrypted file:"
call :loop "esd_File" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"

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%"

goto :eof

:loop
set "mypath="
set /p "mypath=%~2 "
call :validate mypath
if errorlevel 1 goto loop

echo Valid path: "%mypath%"

exit /b


:validate
setlocal EnableDelayedExpansion
set "p=!%~1!"

:: characters /<>|*?" are invalid
for /f delims^=/^<^>^|*?^" %%i in ("!p!") do (
  setlocal DisableDelayedExpansion
  set "s=%%i"
  setlocal EnableDelayedExpansion
  if "!s!" neq "!p!" (
    endlocal&endlocal&endlocal
    echo Invalid character found.
    exit /b 1
  )
  endlocal&endlocal
)

:: if the 2nd and 3rd characters are not :\ it's not a local absolute path
if "!p:~1,2!" neq ":\" (
  endlocal
  echo No absolute path entered.
  exit /b 1
)

:remove_trailing_backslash if any
if "!p:~-1!"=="\" (set "p=!p:~,-1!"&goto remove_trailing_backslash)

:: check if the path exists
if not exist "!p!\" (
  endlocal
  echo The entered path doesn't exist.
exit /b 1
)

:: try to write a temporary file in order to check if you have permissions
2>nul (>"!p!\test.~tmp" type nul) || (
  endlocal
  echo Access denied.
  exit /b 1
)

:: file deletion fails if the path contains consecutive backslashes
2>nul del "!p!\test.~tmp" || (
  endlocal
  echo Multiple backslashes found.
  exit /b 1
)

endlocal &set "%~1=%p%"
exit /b 0


but I have problems because, when I insert a new directory nonexistent, the script does to know correctly that this directory does not exist but, then, it continues to repeat the request, and it does not automatically go to the next request.

Also, when I enter only the name of a file with the extension into the specific request, the script tells me that is not a directory.

So, how do I solve these problems and make sure that this script also validates the directory and file names with the symbols of each language?

Thanks

Bye

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Check the validity of the directories and files names

#19 Post by thefeduke » 13 Jul 2016 13:34

balubeto wrote:However, the already existing directory should be considered valid from the script; while, if a user enters an X:\A\B.ext directory that does not exist, the script should consider it invalid.

balubeto wrote:I created this script
. . .
but I have problems because, when I insert a new directory nonexistent, the script does to know correctly that this directory does not exist but, then, it continues to repeat the request, and it does not automatically go to the next request.

More making up of mind seems required.

balubeto wrote:Also, when I enter only the name of a file with the extension into the specific request, the script tells me that is not a directory.
You cannot expect a routine that checks paths to work with name.ext. There is no such thing as an absolute name.ext.
You have however established %esd_File_Path% by this time, so apply a second routine that accepts %esd_File_Path% and %esd_File% as arguments and build an absolute full file name of %~1\%~2 to validate within this routine, cycling %esd_File% until correct. The method in the script in the first post applied different criteria for the file as opposed to the paths.

John A.

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Check the validity of the directories and files names

#20 Post by balubeto » 14 Jul 2016 01:34

thefeduke wrote:
balubeto wrote:However, the already existing directory should be considered valid from the script; while, if a user enters an X:\A\B.ext directory that does not exist, the script should consider it invalid.

balubeto wrote:I created this script
. . .
but I have problems because, when I insert a new directory nonexistent, the script does to know correctly that this directory does not exist but, then, it continues to repeat the request, and it does not automatically go to the next request.

More making up of mind seems required.

balubeto wrote:Also, when I enter only the name of a file with the extension into the specific request, the script tells me that is not a directory.
You cannot expect a routine that checks paths to work with name.ext. There is no such thing as an absolute name.ext.
You have however established %esd_File_Path% by this time, so apply a second routine that accepts %esd_File_Path% and %esd_File% as arguments and build an absolute full file name of %~1\%~2 to validate within this routine, cycling %esd_File% until correct. The method in the script in the first post applied different criteria for the file as opposed to the paths.

John A.


So, I can not understand what changes should I do to the script to solve my problems.

Thanks

Bye

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check the validity of the directories and files names

#21 Post by penpen » 14 Jul 2016 08:52

It is hard to see, what you want to do:

See this post on page 1:
balubeto wrote:while, if a user enters an X:\A\B.ext directory that does not exist, the script should consider it invalid.


And your second last post above:
balubeto wrote: when I insert a new directory nonexistent, the script does to know correctly that this directory does not exist but, then, it continues to repeat the request, and it does not automatically go to the next request.


The path either "valid and not invalid" or "not valid and invalid":
It cannot be both "valid and invalid".

Actually the path is invalid if it does not exist, so you get this behaviour.


Sidenote:
The function ":setValidPath" checks if a given path is valid within the section ":validatePath":
If the path is not valid then it asks the user to enter a new (hopefully valid) path,
else the path is valid and the function stores the path within the specified environment variable and returns to the calling context.

In a post of your last thread you wanted this loop (if i haven't misinterpreted what you've written):
balubeto wrote:When I press only the Enter key when the script displays an entry, it is possible to use a For loop or other structure to redisplay the entry?



In addition:
I'm still unsure why you don't just create the path and check if it exists (removing the sample validation example) - it seems you seem want to create the directory:

Code: Select all

@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

   setlocal EnableDelayedExpansion
   if not defined invalid for /f tokens^=1*^ delims^=/^<^>^|*?^" %%a in ("#!input!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   endlocal & set "invalid=%invalid%"

   if not defined invalid for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("%input:~0,3%") do if not "%%~a" == ":\" set "invalid=No absolute path entered."
   if not defined invalid if not "%input:\\=##%" == "%input%" set "invalid=Double backslashes."
   if not defined invalid if not exist "%input%" md "%input%"
   if not defined invalid if not exist "%input%" set "invalid=The Path you specified for %~1 ^("%input%"^) is invalid."
   if not defined invalid for %%a in ("%input%") do for /f "tokens=1* delims=dD" %%b in ("-%%~aa") do if "%%~c" == "" set "invalid=Name collision: File found, but path needed."
   if not defined invalid if not "%input:~-1%" == "\" 2>nul (>>"%input%\test.~tmp" type nul) && del "%input%\test.~tmp" || set "invalid=Access denied."
   if not defined invalid if "%input:~-1%" == "\" 2>nul (>>"%input%test.~tmp" type nul) && del "%input%test.~tmp" || set "invalid=Access denied."

   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
   if not defined invalid if "" == "%input%" set "invalid=The Filename you specified for %~1 is the empty string."

   setlocal EnableDelayedExpansion
   if not defined invalid for /f tokens^=1*^ delims^=^\^:/^<^>^|*?^" %%a in ("#!input!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   endlocal & set "invalid=%invalid%"

   if not defined invalid 2>nul (>>"%input%.~tmp" type nul) && del "%input%.~tmp" || set "invalid=Access denied."

   if not defined invalid endlocal & set "%~1=%input%" & echo Valid: %~1=%input%& goto :eof
   echo(Invalid: %invalid%
   set /P ^"input=%text:""="% "
   goto :validateFile

Or do you want to have differet validity checks for the different pathes?
If you want, then just describe when the different directories (stored in "Windows_Files_Path", "iso_Path", and "esd_File_Path"; also maybe the same for the "esd_File") is valid (== function doesn't loop anymore).


penpen

Edit: I've added aGerman's validity checks; i also modified them to be used within my validity chain style.
Last edited by penpen on 14 Jul 2016 14:46, edited 2 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check the validity of the directories and files names

#22 Post by foxidrive » 14 Jul 2016 10:12

balubeto wrote:So, how do I solve these problems and make sure that this script also validates the directory and file names with the symbols of each language?


This should do what your script is aiming to do but it's untested.
Some script behaviour is changed and it asks to re-enter the data.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

set "repeat="
set /p "Windows_Files_Path=Enter the directory in which put the content of the Windows Setup Media volume image: "
set /p "iso_Path=Enter the directory in which put the iso image file created: "
set /p "esd_File_Path=Enter the directory in which put the esd unencrypted file: "
set /p "esd_File=Enter the file to be converted which should be put in the %esd_File_Path% directory: "
echo.
for %%a in (
  "%Windows_Files_Path%"
  "%iso_Path%"
  "%esd_File_Path%"
) do (

 rem check if the folder path exists
     if not exist "%%~a\"         echo The "%%~a" directory does not exist. & set repeat=1
 rem check if invalid characters "/<>|*?" are entered
     for /f "delims=/<>|*?" %%b in ("%%~a") do if not "%%~b"=="%%~a" (
                                  echo The line "%%~a" contains invalid characters. & set repeat=1
     )
 rem check if it's local absolute path. A check above detects if the :\ is in an invalid position
echo "%%~a"|find ":\" >nul ||    (echo The line "%%~a" is not an absolute path. & set repeat=1)
 rem check if the path contains consecutive backslashes
echo "%%~a"|find "\\" >nul &&    (echo Multiple backslashes found in "%%~a" & set repeat=1)
 rem try to write a temporary file in order to check if you have permissions
if exist "%%~a\" break >"%%~a\test.~tmp" 2>nul || (echo Access denied in "%%~a" & exit /b 1)
                    del "%%~a\test.~tmp" 2>nul
)
 rem check if the file exists
   if not exist "%esd_File_Path%\%esd_File%"    echo The "%esd_File%" file does not exist. & set repeat=1
if defined repeat echo Re-enter the data listed above & timeout 10 & "%~0"


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%"
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check the validity of the directories and files names

#23 Post by foxidrive » 14 Jul 2016 11:13

balubeto wrote:I created this script


viewtopic.php?p=47682#p47682

Is that where you created your script, balubeto? :D

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Check the validity of the directories and files names

#24 Post by balubeto » 14 Jul 2016 11:41

The last penpen's script works but:

1) When I enter an absolute path to a valid directory, it continues to display the "invalid=The Path you specified for %~1 ^("%input%"^) is invalid." message. How do I remove it?

2) It does not control more the validity of the directories with absolute paths and files. The result of this is that a user, for example, could be assigned to the esd_File_Path and esd_File variables the respective X:\A\B? and AB*.esd invalid values. How can I prevent this?

The last foxidrive's script can not even recognize the validity of the existing absolute paths and not; If a valid file does not exist, the script does not save it in its variable. Why?

Thanks

Bye

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Check the validity of the directories and files names

#25 Post by penpen » 14 Jul 2016 14:52

Ad 1):
Which absolute path to a valid directory provokes the message (stored in invalid; i've tried some, but didn't found any)?


Ad 2):
I hadn't added aGerman's validity checks.
Now i've corrected and updated the code above on that point.


penpen

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Check the validity of the directories and files names

#26 Post by thefeduke » 14 Jul 2016 15:08

thefeduke wrote:More making up of mind seems required..
Recent posts have clarified that you do indeed want both ways. So I have used your latest framework. You incorporated aGerman's fine directory validation code in a way that the setlocal and endlocal statements need a little work. I modified both to give you some more control, but this does not yet address the esd file verification for which you need a similar, yet different, function.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
::http://www.dostips.com/forum/viewtopic.php?p=47747#p47747
:: Post subject: Validity of the directories and files names
::Posted: Wed Jul 13, 2016 12:26 pm by balubeto
::modified: Thu Jul 14, 2016 by thefeduke adding create as optional

Set "MaxRC=0"
call :VetDir "Windows_Files_Path" "Enter the directory in which put the content of the Windows Setup Media volume image:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetDir "iso_Path" "Enter the directory in which put the iso image file created:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetDir "esd_File_Path" "Enter the directory in which put the esd unencrypted file:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
If "%errorlevel%" EQU "0" (
  call :VetFile "esd_File" "Enter the file to be converted which should be put in the %esd_File_Path% directory:" "%esd_File_Path%"
)
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"

Echo.
If "%MaxRC%" NEQ "0" Echo.iso build abandoned during validation.
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%"

goto :eof

:VetDir
@echo off &setlocal DisableDelayedExpansion
::http://www.dostips.com/forum/viewtopic.php?p=47682#p47682
:: Post subject: Re: Check the validity of the directories and files names
::Posted: Sun Jul 10, 2016 5:07 pm by aGerman
::modified: Thu Jul 14, 2016 by thefeduke adding create as optional

:loop
set "%~1="
set /p "%~1=%~2 "
call :validate "%~1" validpath
if errorlevel 2 Exit /b 1
if errorlevel 1 goto loop

call echo Valid path: "%validpath%"
rem.pause
endlocal &set "%~1=%validpath%"
exit /b

:validate
setlocal EnableDelayedExpansion
set "p=!%~1!"

:: characters /<>|*?" are invalid
for /f delims^=/^<^>^|*?^" %%i in ("!p!") do (
  setlocal DisableDelayedExpansion
  set "s=%%i"
  setlocal EnableDelayedExpansion
  if "!s!" neq "!p!" (
    endlocal&endlocal&endlocal
    echo Invalid character found.
    exit /b 1
  )
  endlocal&endlocal
)

:: if the 2nd and 3rd characters are not :\ it's not a local absolute path
if "!p:~1,2!" neq ":\" (
  endlocal
  echo No absolute path entered.
  exit /b 1
)

:remove_trailing_backslash if any
if "!p:~-1!"=="\" (set "p=!p:~,-1!"&goto remove_trailing_backslash)

:: check if the path exists
if not exist "!p!\" (
  echo The entered path doesn't exist.
  Call CHOICE /C BRC /N /M "Press B to Build, R to Retry with a different path or C to Cancel."
  If "!errorlevel!" EQU "3" (
    endlocal &set "%~2="
    exit /b 2
  )
  If "!errorlevel!" EQU "2" (
    endlocal
    exit /b 1
  )
  If "!errorlevel!" EQU "1" (
    mkdir !p!
  )
)

:: try to write a temporary file in order to check if you have permissions
2>nul (>"!p!\test.~tmp" type nul) || (
  endlocal
  echo Access denied.
  exit /b 1
)

:: file deletion fails if the path contains consecutive backslashes
2>nul del "!p!\test.~tmp" || (
  endlocal
  echo Multiple backslashes found.
  exit /b 1
)

endlocal &set "%~2=%p%"
exit /b 0

:VetFile
@echo off &setlocal DisableDelayedExpansion
::http://www.dostips.com/forum/viewtopic.php?p=47682#p47682
:: Post subject: Re: Check the validity of the directories and files names
::Posted: Sun Jul 10, 2016 5:07 pm by aGerman
::modified: Thu Jul 15, 2016 by thefeduke - adding semi-autmatic file selection

set "folder=%~3"
set "files=0"
For /f "usebackq tokens=1-2" %%F in (`dir "%folder%" /a-d`) DO (
  If /I "%%~G" EQU "File(s)" Set "files=%%~F"
)
If "%files%" EQU "0" (Echo.No files in '%folder%'.&endlocal &set "%~1=" &Exit /b 1)
If "%files%" EQU "1" For /f "usebackq tokens=1" %%V in (`dir "%folder%" /b /a-d`) DO Set "Valid=%%V"
If "%files%" EQU "1" (
  Echo.'%valid%' is the only file in '%folder%'.
  endlocal &set "%~1=%valid%"
  Exit /b 0
)
dir "%folder%" /b /a-d
Echo.Enter one of the above files in '%folder%'.
:loopfile
set "%~1="
set /p "%~1=%~2 "
call :validatefile "%~1" validFile
if errorlevel 2 Exit /b 1
if errorlevel 1 goto loopfile

call echo Valid file: "%validfile%"
rem.pause
endlocal &set "%~1=%validfile%"
exit /b

:validatefile
setlocal EnableDelayedExpansion
set "p=!%~1!"

:: characters /<>|*?" are invalid
for /f delims^=/^<^>^|*?^" %%i in ("!p!") do (
  setlocal DisableDelayedExpansion
  set "s=%%i"
  setlocal EnableDelayedExpansion
  if "!s!" neq "!p!" (
    endlocal&endlocal&endlocal
    echo Invalid character found.
    exit /b 1
  )
  endlocal&endlocal
)

:: check if the file exists
if not exist "%folder%\!p!" (
  echo The entered file doesn't exist.
  Call CHOICE /C RC /N /M "Press R to Retry with a different file or C to Cancel."
  If "!errorlevel!" EQU "2" (
    endlocal &set "%~2="
    exit /b 2
  )
  If "!errorlevel!" EQU "1" (
    endlocal
    exit /b 1
  )
)

endlocal &set "%~2=%p%"
exit /b 0
The main feature is that now you can choose to re-input or create non-existant folders or even abandon ship, while continuing validation.

Edit: modified to add esd file selection.

John A.
Last edited by thefeduke on 15 Jul 2016 00:19, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check the validity of the directories and files names

#27 Post by foxidrive » 14 Jul 2016 15:53

balubeto wrote:The last foxidrive's script can not even recognize the validity of the existing absolute paths and not; If a valid file does not exist, the script does not save it in its variable. Why?


Because there was one logic flaw and my understanding of the location of your %esd_File% was wrong.

I used the && operator instead of the || operator
and I didn't add the %esd_File_Path% when I checked for the file.

I tested it for some aspects and added a check to remove a harmless error message, but you can test it further yourself. I edited the code in the same post.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check the validity of the directories and files names

#28 Post by foxidrive » 14 Jul 2016 16:24

This is a little more polished.

The third entry accepts the entire c:\esd-folder\esd-file.ext in one line as it is easier to input the data that way.


Code: Select all

@echo off &setlocal DisableDelayedExpansion

set "repeat="
set /p "Windows_Files_Path=Enter the folder for the Windows Setup Media volume image: "
set /p "iso_Path=Enter the folder for the new iso image file: "
set /p "esd_File=Enter the drv:\folder\filename for the esd unencrypted file: "

for %%a in ("%esd_File%") do set "esd_path=%%~dpa" & set "esd_path_file=%%~nxa"
set "esd_path=%esd_path:~0,-1%"

echo.
for %%a in (
  "%Windows_Files_Path%"
  "%iso_Path%"
  "%esd_path%"
) do (

 rem check if the folder path exists
     if not exist "%%~a\"         echo The "%%~a" directory does not exist. & set repeat=1
 rem check if invalid characters "/<>|*?" are entered
     for /f "delims=/<>|*?" %%b in ("%%~a") do if not "%%~b"=="%%~a" (
                                  echo The "%%~a" entry contains invalid characters. & set repeat=1
     )
 rem check if it's local absolute path. A check above detects if the :\ is in an invalid position
echo "%%~a"|find ":\" >nul ||    (echo The "%%~a" entry is not an absolute path. & set repeat=1)
 rem check if the path contains consecutive backslashes
echo "%%~a"|find "\\" >nul &&    (echo Multiple backslashes were found in "%%~a" & set repeat=1)
 rem try to write a temporary file in order to check if you have permissions
if exist "%%~a\" break >"%%~a\test.~tmp" 1>nul 2>&1 || (echo You do not have access to the "%%~a" folder & exit /b 1)
                    del "%%~a\test.~tmp" 2>nul
)
 rem check if the file exists

if exist "%esd_path%" (
  pushd "%esd_path%" & if not exist "%esd_path_file%"  echo The "%esd_File%" file does not exist. & popd & set repeat=1
  )
if defined repeat echo Re-enter the data listed above & timeout 10 & "%~0"


echo Result:
echo Windows_Files_Path: "%Windows_Files_Path%"
echo iso_Path          : "%iso_Path%"
echo esd_File          : "%esd_File%"
pause

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Check the validity of the directories and files names

#29 Post by thefeduke » 15 Jul 2016 00:35

thefeduke wrote:You have however established %esd_File_Path% by this time, so apply a second routine that accepts %esd_File_Path% and %esd_File% as arguments and build an absolute full file name of %~1\%~2 to validate within this routine, cycling %esd_File% until correct.
I have updated my most recent post including this very function.

The esd file name validation is very friendly. It takes automatic action if there is only one file or none at all in the esd folder and offers a list to guide the typing if there are multiple input files in the esd folder.

John A.

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Check the validity of the directories and files names

#30 Post by balubeto » 15 Jul 2016 09:55

thefeduke wrote:
thefeduke wrote:More making up of mind seems required..
Recent posts have clarified that you do indeed want both ways. So I have used your latest framework. You incorporated aGerman's fine directory validation code in a way that the setlocal and endlocal statements need a little work. I modified both to give you some more control, but this does not yet address the esd file verification for which you need a similar, yet different, function.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
::http://www.dostips.com/forum/viewtopic.php?p=47747#p47747
:: Post subject: Validity of the directories and files names
::Posted: Wed Jul 13, 2016 12:26 pm by balubeto
::modified: Thu Jul 14, 2016 by thefeduke adding create as optional

Set "MaxRC=0"
call :VetDir "Windows_Files_Path" "Enter the directory in which put the content of the Windows Setup Media volume image:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetDir "iso_Path" "Enter the directory in which put the iso image file created:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetDir "esd_File_Path" "Enter the directory in which put the esd unencrypted file:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
If "%errorlevel%" EQU "0" (
  call :VetFile "esd_File" "Enter the file to be converted which should be put in the %esd_File_Path% directory:" "%esd_File_Path%"
)
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"

Echo.
If "%MaxRC%" NEQ "0" Echo.iso build abandoned during validation.
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%"

goto :eof

:VetDir
@echo off &setlocal DisableDelayedExpansion
::http://www.dostips.com/forum/viewtopic.php?p=47682#p47682
:: Post subject: Re: Check the validity of the directories and files names
::Posted: Sun Jul 10, 2016 5:07 pm by aGerman
::modified: Thu Jul 14, 2016 by thefeduke adding create as optional

:loop
set "%~1="
set /p "%~1=%~2 "
call :validate "%~1" validpath
if errorlevel 2 Exit /b 1
if errorlevel 1 goto loop

call echo Valid path: "%validpath%"
rem.pause
endlocal &set "%~1=%validpath%"
exit /b

:validate
setlocal EnableDelayedExpansion
set "p=!%~1!"

:: characters /<>|*?" are invalid
for /f delims^=/^<^>^|*?^" %%i in ("!p!") do (
  setlocal DisableDelayedExpansion
  set "s=%%i"
  setlocal EnableDelayedExpansion
  if "!s!" neq "!p!" (
    endlocal&endlocal&endlocal
    echo Invalid character found.
    exit /b 1
  )
  endlocal&endlocal
)

:: if the 2nd and 3rd characters are not :\ it's not a local absolute path
if "!p:~1,2!" neq ":\" (
  endlocal
  echo No absolute path entered.
  exit /b 1
)

:remove_trailing_backslash if any
if "!p:~-1!"=="\" (set "p=!p:~,-1!"&goto remove_trailing_backslash)

:: check if the path exists
if not exist "!p!\" (
  echo The entered path doesn't exist.
  Call CHOICE /C BRC /N /M "Press B to Build, R to Retry with a different path or C to Cancel."
  If "!errorlevel!" EQU "3" (
    endlocal &set "%~2="
    exit /b 2
  )
  If "!errorlevel!" EQU "2" (
    endlocal
    exit /b 1
  )
  If "!errorlevel!" EQU "1" (
    mkdir !p!
  )
)

:: try to write a temporary file in order to check if you have permissions
2>nul (>"!p!\test.~tmp" type nul) || (
  endlocal
  echo Access denied.
  exit /b 1
)

:: file deletion fails if the path contains consecutive backslashes
2>nul del "!p!\test.~tmp" || (
  endlocal
  echo Multiple backslashes found.
  exit /b 1
)

endlocal &set "%~2=%p%"
exit /b 0

:VetFile
@echo off &setlocal DisableDelayedExpansion
::http://www.dostips.com/forum/viewtopic.php?p=47682#p47682
:: Post subject: Re: Check the validity of the directories and files names
::Posted: Sun Jul 10, 2016 5:07 pm by aGerman
::modified: Thu Jul 15, 2016 by thefeduke - adding semi-autmatic file selection

set "folder=%~3"
set "files=0"
For /f "usebackq tokens=1-2" %%F in (`dir "%folder%" /a-d`) DO (
  If /I "%%~G" EQU "File(s)" Set "files=%%~F"
)
If "%files%" EQU "0" (Echo.No files in '%folder%'.&endlocal &set "%~1=" &Exit /b 1)
If "%files%" EQU "1" For /f "usebackq tokens=1" %%V in (`dir "%folder%" /b /a-d`) DO Set "Valid=%%V"
If "%files%" EQU "1" (
  Echo.'%valid%' is the only file in '%folder%'.
  endlocal &set "%~1=%valid%"
  Exit /b 0
)
dir "%folder%" /b /a-d
Echo.Enter one of the above files in '%folder%'.
:loopfile
set "%~1="
set /p "%~1=%~2 "
call :validatefile "%~1" validFile
if errorlevel 2 Exit /b 1
if errorlevel 1 goto loopfile

call echo Valid file: "%validfile%"
rem.pause
endlocal &set "%~1=%validfile%"
exit /b

:validatefile
setlocal EnableDelayedExpansion
set "p=!%~1!"

:: characters /<>|*?" are invalid
for /f delims^=/^<^>^|*?^" %%i in ("!p!") do (
  setlocal DisableDelayedExpansion
  set "s=%%i"
  setlocal EnableDelayedExpansion
  if "!s!" neq "!p!" (
    endlocal&endlocal&endlocal
    echo Invalid character found.
    exit /b 1
  )
  endlocal&endlocal
)

:: check if the file exists
if not exist "%folder%\!p!" (
  echo The entered file doesn't exist.
  Call CHOICE /C RC /N /M "Press R to Retry with a different file or C to Cancel."
  If "!errorlevel!" EQU "2" (
    endlocal &set "%~2="
    exit /b 2
  )
  If "!errorlevel!" EQU "1" (
    endlocal
    exit /b 1
  )
)

endlocal &set "%~2=%p%"
exit /b 0
The main feature is that now you can choose to re-input or create non-existant folders or even abandon ship, while continuing validation.

Edit: modified to add esd file selection.

John A.


Almost perfect.

Why the script never ask you to enter the name (with its extension) of esd files?

Thanks

Bye

Locked