bat script with parameters and personalized message

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: bat script with parameters and personalized message

#31 Post by penpen » 04 Jul 2016 11:37

balubeto wrote:Does anyone know how to solve my problem in a more suitable form?
You should precise what you want to do... .

Maybe something like this may help you (but that's just guessed):

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


penpen

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: bat script with parameters and personalized message

#32 Post by sambul35 » 04 Jul 2016 18:10

penpen wrote:You should precise what you want to do... .

I know from own experience, Der Hunger kommt beim Essen. :D I'm not sure to grasp how do you check the path\file validity, but its a cumbersome task anyway, probably just giving a draft example? I think he wants to enter data with batch arguments (parameters), and if not correct, re-enter manually. Using batch args allows to run tasks by a Task Scheduler.

It looks like after yesterday's discussion, dbenham posted a different snippet to check paths & files validity. Not sure if its shorter compare to the approach I use in FileNameValidator to check validity by test writing a folder to disk, neither if they're equivalent in scope. Will try to integrate it into next FileNameValidator beta and see if it results in a shorter overall code. :wink:
Last edited by sambul35 on 05 Jul 2016 11:36, edited 2 times in total.

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

Re: bat script with parameters and personalized message

#33 Post by balubeto » 05 Jul 2016 02:48

penpen wrote:
balubeto wrote:Does anyone know how to solve my problem in a more suitable form?
You should precise what you want to do... .

Maybe something like this may help you (but that's just guessed):

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


penpen


I tried this script and I noticed that the directories, which are valid but do not exist, are not saved in their respective variables. Why?

Thanks

Bye

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

Re: bat script with parameters and personalized message

#34 Post by penpen » 05 Jul 2016 05:58

sambul35 wrote:I'm not sure to grasp how do you check the path\file validity, but its a cumbersome task anyway
balubeto wrote:I tried this script and I noticed that the directories, which are valid but do not exist, are not saved in their respective variables. Why?
I didn't check the validity of the file-/pathname;
i only built a framework (with sample validity checks) where you could add your own validity checking as you wish, search these lines:
penpen wrote:

Code: Select all

   :: example code for invalidating input if the given path does not exist
   :: example code for invalidating input if the given filename is empty string

Full Filename / Pathname validation is not cumbersome, but nearly impossible to be solved in common:
In common it's hard to check the limitations of the underlying file system, or to even realize if you have direct or indirect hardware access (for example: hdd in your system versus some network store accessed by own file api runing under foreign OS).

Also it depends on the level of the used win API how relative or absolute pathes are defined:
There are some slight differences, if you use the win I/O api, or the shell api (and there are more).

Example:
- Using Windows APIs for file and device I/O an absolute path starts with a '\' (or '/' which will be replaced with a '\') character, and cannot contain any ".." path in it.
- Using the Windows Shell API the path "C:\test\..\b" is also absolute, because it contains all the information necessary to locate the File object (path or file).

Therefore a more precise description is needed on how you want to define a valid (absolute) (file-/path-)name.

The recommended way to check if a (path-/file-) name is valid in common is:
Try to create it - if it succeeds, then the name is valid, else not.
(As far as i know, there is no other reliable validity check.)


penpen

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

Re: bat script with parameters and personalized message

#35 Post by balubeto » 05 Jul 2016 09:57

penpen wrote:
sambul35 wrote:I'm not sure to grasp how do you check the path\file validity, but its a cumbersome task anyway
balubeto wrote:I tried this script and I noticed that the directories, which are valid but do not exist, are not saved in their respective variables. Why?
I didn't check the validity of the file-/pathname;
i only built a framework (with sample validity checks) where you could add your own validity checking as you wish, search these lines:
penpen wrote:

Code: Select all

   :: example code for invalidating input if the given path does not exist
   :: example code for invalidating input if the given filename is empty string

Full Filename / Pathname validation is not cumbersome, but nearly impossible to be solved in common:
In common it's hard to check the limitations of the underlying file system, or to even realize if you have direct or indirect hardware access (for example: hdd in your system versus some network store accessed by own file api runing under foreign OS).

Also it depends on the level of the used win API how relative or absolute pathes are defined:
There are some slight differences, if you use the win I/O api, or the shell api (and there are more).

Example:
- Using Windows APIs for file and device I/O an absolute path starts with a '\' (or '/' which will be replaced with a '\') character, and cannot contain any ".." path in it.
- Using the Windows Shell API the path "C:\test\..\b" is also absolute, because it contains all the information necessary to locate the File object (path or file).

Therefore a more precise description is needed on how you want to define a valid (absolute) (file-/path-)name.

The recommended way to check if a (path-/file-) name is valid in common is:
Try to create it - if it succeeds, then the name is valid, else not.
(As far as i know, there is no other reliable validity check.)


penpen


So, how can I modify this script so that it can verify the validity of the full paths of the directories or the validity of the directories or files names independent of their existence?

Thanks

Bye

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: bat script with parameters and personalized message

#36 Post by sambul35 » 05 Jul 2016 11:21

balubeto wrote:how can I modify this script so that it can verify the validity of the full paths of the directories or the validity of the directories or files names independent of their existence

penpen wrote:Try to create it (i.e. write it to disk) - if it succeeds, then the name is valid, else not.


Try to add your own code to the penpen's draft, post it and let us know what output errors you're facing. If you're looking for a ready-to-go solution, its linked above. Pls try to quote in forum posts only the text or code that you want to clarify, not the entire post, to make the thread easy to follow. :wink:

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

Re: bat script with parameters and personalized message

#37 Post by penpen » 05 Jul 2016 12:07

You may add your own validity checking code after that line(s):

Code: Select all

   :: insert code for additional validity checking
There are two of these comments in the source -
one after the label ":validatePath", and one after the ":validateFile":
the environment variable "input" contains the path-/filename.
If one of your validity checks you may add fail,
then set "invalid" to the error message you want to see.

I could also add validity checks to the above source,
if you explain (in detail) what i should do:
So what is an absolute path, and a valid path for you.


penpen

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

Re: bat script with parameters and personalized message

#38 Post by balubeto » 06 Jul 2016 01:38

penpen wrote:You may add your own validity checking code after that line(s):

Code: Select all

   :: insert code for additional validity checking
There are two of these comments in the source -
one after the label ":validatePath", and one after the ":validateFile":
the environment variable "input" contains the path-/filename.
If one of your validity checks you may add fail,
then set "invalid" to the error message you want to see.

I could also add validity checks to the above source,
if you explain (in detail) what i should do:
So what is an absolute path, and a valid path for you.


penpen


Sorry but I still do not understand how to modify this script so that it can verify the validity of the full paths of the directories and the validity of directories or files names regardless of their existence so that it can also save the valid directories and files that do not exist in their respective variables.

Thanks

Bye

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

Re: bat script with parameters and personalized message

#39 Post by foxidrive » 06 Jul 2016 06:06

This is another situation where a solution is sought but not enough details are provided to give a good idea of what the problems are. The thread just gets longer and longer without achieving anything.


balubeto, the thread has been locked and you may contact a moderator to discuss unlocking it.

Locked