esd ---> iso script (Testing in progress)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

esd ---> iso script (Testing in progress)

#1 Post by balubeto » 01 Sep 2016 11:38

If I run this script from the command prompt:

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:" "absolutePath" "mustExist" "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"  "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist" "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist"  "file"      "rw" "esd_File_Path"

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
:: %~4   Text equals: (forced to relativePath if %~6 equals file)
::     absolutePath  if path must be absolute
::     relativePath  if path must be relative
::     anyPath       if path may be relative or absolute (any other value)
:: %~5   Text equals: (forced to mayExist if %~8 does not exist)
::     mustExist     if file/directory must exist,
::     doesntExist   if file/directory is not allowed to exist.
::     mayExist      if file/directory may or may not exist (any other value).
:: %~6   Text equals:
::     file          if object to test is a file
::     directory     if object to test is a directory (any other value).
:: %~7   Text equals: (only checked if file/directory exists)
::     r             check read access for specified file/directory
::     w             check write access for specified file/directory
::     rw            check read and write access for specified file/directory
::     -             check no access for specified file/directory (any other value)
::     directory     if object to test is a directory (any other value).
::     Note: testing write access on a directory creates a file "test.tmp"
:: %~8   Only if /I %~6 == "file": environment variable with an path to store the file to
::   (forced to empty word, if %~6 equals directory).
::

   setlocal
   set "input=%~2"
   set "text=%~3"
   if        "%~4" == "absolutePath" ( set "pathType=%~4"
   ) else if "%~4" == "relativePath" ( set "pathType=%~4"
   ) else set "existType=anyPath"
   if        "%~5" == "mustExist" ( set "existType=%~5"
   ) else if "%~5" == "doesntExist" ( set "existType=%~5"
   ) else set "existType=mayExist"
   setlocal enableDelayedExpansion
   set "parent= "
   if "%~6" == "file" (
      set "parent= !%~8!"
      if not "!parent:~-1!" == ":" (
         set "parent=!parent!\"
         if "!parent:~-2!" == "\\" set "parent=!parent:~0,-1!"
      )
   )
   endlocal & set "parent=%parent:~1%"
   if "%~6" == "file" (
      set "fileType=file"
      set "pathType=relativePath"
      if defined parent (
         if not exist "%parent%" set "existType=mayExist"
      ) else set "existType=mayExist"
   ) else set "fileType=directory"
   if        "%~7" == "r"  ( set "checkAccess=r-"
   ) else if "%~7" == "w"  ( set "checkAccess=-w"
   ) else if "%~7" == "rw" ( set "checkAccess=rw"
   ) else                  ( set "checkAccess=--"
   )

:validatePath
   :: validating
   set "invalid="
   set "input=%parent%%input%"
   call :isValidPathName "input" || goto :invalidatedPath
   if        /I     "!pathType!" == "absolutePath" ( call :isAbsolutePathName "input" || ( set "invalid=Path must be absolute" & goto :invalidatedPath )
   ) else if /I     "!pathType!" == "relativePath" ( call :isAbsolutePathName "input" && ( set "invalid=Path must be relative" & goto :invalidatedPath )
   )
   setlocal enableExtensions enableDelayedExpansion
   if        /I     "%existType%" == "mustExist"   ( if not exist "!input!" ( endlocal & set "invalid=Path must exist." & goto :invalidatedPath           )
   ) else if /I     "%existType%" == "doesntExist" ( if     exist "!input!" ( endlocal & set "invalid=Path must be non existent." & goto :invalidatedPath )
   )
   if exist "!input!" for %%b in (!input!) do (
      set "attribs=%%~ab"
      if /I "%fileType%" == "file" (
         if not "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a file (not a directory)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" 2>nul (
               <"!input!" set /p "check=" || ( endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath )
            ) else if "%checkAccess:~1,1%" == "w" 2>nul (
               >"!input!" <nul set /p "check=" || ( endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
            )
         )
      ) else (
         if "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a directory (not a file)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" 2>nul (
               >nul dir "!input!" || ( endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath )
            )
            if "%checkAccess:~1,1%" == "w" 2>nul (
               >nul pushd "!input!"
               >"test.tmp" echo test || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul del "test.tmp"   || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul popd
            )
         )
      )
   )

   endlocal   
   endlocal & set "%~1=%input%"
   goto :eof






:invalidatedPath
   echo(Invalid: %invalid%
   set /P ^"input=%text:""="% "
   goto :validatePath
   goto :eof


:: Check if the pathname follows mainly the naming convention:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
:: Exceptions:
::  - Only a drive descriptor (for example "C:") is allowed for volume names.
::  - Not tested if characters 0x00-0x31, or any other forbidden character (of the target filesystem) are present. (disallowed)
::  - Not tested on alternative data streams
:isValidPathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   set "pathName=!%~1!"
   set "invalid="
   if not defined invalid if "!pathName!" == "" set "invalid=The pathname cannot be empty."
   if not defined invalid for /f tokens^=1^*^ delims^=^/^<^>^|^*^?^" %%a in ("#!pathName!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   if defined invalid set ^"pathname=!pathname:"=!"

   if not defined invalid (
      set "pathName= !pathName:/=\!"
      set "pathName= !pathName:\..\=\!"
      set "pathName= !pathName:\.\=\!"
      if  "%pathName:~-3%" == "\.." set "pathName= !pathName:~0,-3!"
      if  "%pathName:~-2%" == "\." set "pathName= !pathName:~0,-2!"
   )

   if not defined invalid if not "!pathName:\\=__!" == "!pathname!" set "invalid=Empty string is no valid path component."
   if not defined invalid if "!pathName:~1,1!" == ":" (
      for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("#!pathName:~0,1!#") do if not "%%~a" == "#" set "invalid=Invalid drive name."
      set "pathName=!pathName:~2!"
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /I /R /C:"[\\]AUX[\\]" /C:"[\\]CON[\\]" /C:"[\\]NUL[\\]" /C:"[\\]PRN[\\]" /C:"[\\]COM[0-9][\\]" /C:"[\\]LPT[0-9][\\]" && (
         set "invalid=The following names are not allowed for file/directory names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9."
      )
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /R /C:"[\ ][\\]" /C:"[\.][\\]" && (
         set "invalid=It is not allowed to end file/directory names with a space (' ') or a period ('.') character."
      )
   )
   if not defined invalid (
      endlocal
      exit /b 0
   ) else (
      endlocal & set "invalid=%invalid%"
      exit /b 1
   )
   goto :eof


:: Using mainly the definition of the Windows Shell API, see:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773660(v=vs.85).aspx
:: Exception:
::  - Only accepts a drive descriptor (for example "C:") is allowed for volume names.
::
:: does not check validity (must be valid)
:isAbsolutePathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   if not "!%~1:~1,2!" == ":\" (
      endlocal
      exit /b 1
   ) else (
      endlocal
      exit /b 0
   )
   goto :eof


this error appears:

Code: Select all

D:\Users\Public\Documents\Balubeto\Test_script>Test_01.bat
=" == "\.." set "pathName= non atteso.
D:\Users\Public\Documents\Balubeto\Test_script>


Why?

Thanks

Bye
Last edited by balubeto on 27 Sep 2016 09:13, edited 4 times in total.

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

Re: Script error during the startup

#2 Post by penpen » 01 Sep 2016 15:12

There was a little bug (if variable pathName is empty); easily fixed set pathname to a space character:

Code: Select all

if not defined invalid if "!pathName!" == "" set "invalid=The pathname cannot be empty." & set "pathName= "


I fixed another issue, hopefully it is "bugfree" now:

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:" "absolutePath" "mustExist" "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"  "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist" "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist"  "file"      "rw" "esd_File_Path"

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
:: %~4   Text equals: (forced to relativePath if %~6 equals file)
::     absolutePath  if path must be absolute
::     relativePath  if path must be relative
::     anyPath       if path may be relative or absolute (any other value)
:: %~5   Text equals: (forced to mayExist if %~8 does not exist)
::     mustExist     if file/directory must exist,
::     doesntExist   if file/directory is not allowed to exist.
::     mayExist      if file/directory may or may not exist (any other value).
:: %~6   Text equals:
::     file          if object to test is a file
::     directory     if object to test is a directory (any other value).
:: %~7   Text equals: (only checked if file/directory exists)
::     r             check read access for specified file/directory
::     w             check write access for specified file/directory
::     rw            check read and write access for specified file/directory
::     -             check no access for specified file/directory (any other value)
::     directory     if object to test is a directory (any other value).
::     Note: testing write access on a directory creates a file "test.tmp"
:: %~8   Only if /I %~6 == "file": environment variable with an path to store the file to
::   (forced to empty word, if %~6 equals directory).
::

   setlocal
   set "input=%~2"
   set "text=%~3"
   if        "%~4" == "absolutePath" ( set "pathType=%~4"
   ) else if "%~4" == "relativePath" ( set "pathType=%~4"
   ) else set "existType=anyPath"
   if        "%~5" == "mustExist" ( set "existType=%~5"
   ) else if "%~5" == "doesntExist" ( set "existType=%~5"
   ) else set "existType=mayExist"
   setlocal enableDelayedExpansion
   set "parent= "
   if "%~6" == "file" (
      set "parent= !%~8!"
      if not "!parent:~-1!" == ":" (
         set "parent=!parent!\"
         if "!parent:~-2!" == "\\" set "parent=!parent:~0,-1!"
      )
   )
   endlocal & set "parent=%parent:~1%"
   if "%~6" == "file" (
      set "fileType=file"
      set "pathType=relativePath"
      if defined parent (
         if not exist "%parent%" set "existType=mayExist"
      ) else set "existType=mayExist"
   ) else set "fileType=directory"
   if        "%~7" == "r"  ( set "checkAccess=r-"
   ) else if "%~7" == "w"  ( set "checkAccess=-w"
   ) else if "%~7" == "rw" ( set "checkAccess=rw"
   ) else                  ( set "checkAccess=--"
   )

:validatePath
   :: validating
   set "invalid="
   call :isValidPathName "input" || goto :invalidatedPath
   if        /I     "%pathType%" == "absolutePath" ( call :isAbsolutePathName "input" || ( set "invalid=Path must be absolute" & goto :invalidatedPath )
   ) else if /I     "%pathType%" == "relativePath" ( call :isAbsolutePathName "input" && ( set "invalid=Path must be relative" & goto :invalidatedPath ) || >&2 >nul (echo @maybe: replace z: in !input!)
   )
   setlocal enableExtensions enableDelayedExpansion
   set "input=!parent!!input!"
   if        /I     "%existType%" == "mustExist"   ( if not exist "!input!" ( endlocal & set "invalid=Path must exist." & goto :invalidatedPath           )
   ) else if /I     "%existType%" == "doesntExist" ( if     exist "!input!" ( endlocal & set "invalid=Path must be non existent." & goto :invalidatedPath )
   )

   if exist "!input!" for %%b in ("!input!") do (
      set "attribs=%%~ab"
      if /I "%fileType%" == "file" (
         if not "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a file (not a directory)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" (
               2>nul <"!input!" set /p "check=" || (
                  for %%a in ("!input!") do if "%%~za" == "0" (
                     >&2 echo(Warning: Could not verify read access: Reason filesize of "!input!" is 0.
                  ) else (
                     endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath
                  )
               )
            ) else if "%checkAccess:~1,1%" == "w" 2>nul (
               >"!input!" <nul set /p "check=" || ( endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
            )
         )
      ) else (
         if "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a directory (not a file)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" 2>nul (
               >nul dir "!input!" || ( endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath )
            )
            if "%checkAccess:~1,1%" == "w" 2>nul (
               >nul pushd "!input!"
               >"test.tmp" echo test || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul del "test.tmp"   || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul popd
            )
         )
      )
   )

   endlocal
   endlocal & set "%~1=%input%"
   goto :eof






:invalidatedPath
   echo(Invalid: %invalid%
   set /P ^"input=%text:""="% "
   goto :validatePath
   goto :eof


:: Check if the pathname follows mainly the naming convention:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
:: Exceptions:
::  - Only a drive descriptor (for example "C:") is allowed for volume names.
::  - Not tested if characters 0x00-0x31, or any other forbidden character (of the target filesystem) are present. (disallowed)
::  - Not tested on alternative data streams
:isValidPathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   set "pathName=!%~1!"
   set "invalid="
   if not defined invalid if "!pathName!" == "" set "invalid=The pathname cannot be empty." & set "pathName= "
   if not defined invalid for /f tokens^=1^*^ delims^=^/^<^>^|^*^?^" %%a in ("#!pathName!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   if not defined invalid set ^"pathname=!pathname:"=!"

   if not defined invalid (
      set "pathName= !pathName:/=\!"
>nul echo @maybe:set "pathName= !pathName:\..\=\!"
>nul echo @maybe:set "pathName= !pathName:\.\=\!"
>nul echo @maybe:if "%pathName:~-3%" == "\.." set "pathName= !pathName:~0,-3!"
>nul echo @maybe:if "%pathName:~-2%" == "\." set "pathName= !pathName:~0,-2!"
   )

   if not defined invalid if not "!pathName:\\=__!" == "!pathname!" set "invalid=Empty string is no valid path component."
   if not defined invalid if "!pathName:~1,1!" == ":" (
      for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("#!pathName:~0,1!#") do if not "%%~a" == "#" set "invalid=Invalid drive name."
      set "pathName=!pathName:~2!"
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /I /R /C:"[\\]AUX[\\]" /C:"[\\]CON[\\]" /C:"[\\]NUL[\\]" /C:"[\\]PRN[\\]" /C:"[\\]COM[0-9][\\]" /C:"[\\]LPT[0-9][\\]" && (
         set "invalid=The following names are not allowed for file/directory names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9."
      )
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /R /C:"[\ ][\\]" /C:"[\.][\\]" && (
         set "invalid=It is not allowed to end file/directory names with a space (' ') or a period ('.') character."
      )
   )
   if not defined invalid (
      endlocal
      exit /b 0
   ) else (
      endlocal & set "invalid=%invalid%"
      exit /b 1
   )
   goto :eof


:: Using mainly the definition of the Windows Shell API, see:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773660(v=vs.85).aspx
:: Exception:
::  - Only accepts a drive descriptor (for example "C:") is allowed for volume names.
::
:: does not check validity (must be valid)
:isAbsolutePathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   if not "!%~1:~1,2!" == ":\" (
      endlocal
      exit /b 1
   ) else (
      endlocal
      exit /b 0
   )
   goto :eof


Call it somehow like this:

Code: Select all

test.bat "Z:\1" "Z:\2" "Z:\3" "0.txt"


penpen

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

Re: Script error during the startup

#3 Post by balubeto » 02 Sep 2016 03:58

penpen wrote:There was a little bug (if variable pathName is empty); easily fixed set pathname to a space character:

Code: Select all

if not defined invalid if "!pathName!" == "" set "invalid=The pathname cannot be empty." & set "pathName= "


I fixed another issue, hopefully it is "bugfree" now:

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:" "absolutePath" "mustExist" "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"  "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist" "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist"  "file"      "rw" "esd_File_Path"

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
:: %~4   Text equals: (forced to relativePath if %~6 equals file)
::     absolutePath  if path must be absolute
::     relativePath  if path must be relative
::     anyPath       if path may be relative or absolute (any other value)
:: %~5   Text equals: (forced to mayExist if %~8 does not exist)
::     mustExist     if file/directory must exist,
::     doesntExist   if file/directory is not allowed to exist.
::     mayExist      if file/directory may or may not exist (any other value).
:: %~6   Text equals:
::     file          if object to test is a file
::     directory     if object to test is a directory (any other value).
:: %~7   Text equals: (only checked if file/directory exists)
::     r             check read access for specified file/directory
::     w             check write access for specified file/directory
::     rw            check read and write access for specified file/directory
::     -             check no access for specified file/directory (any other value)
::     directory     if object to test is a directory (any other value).
::     Note: testing write access on a directory creates a file "test.tmp"
:: %~8   Only if /I %~6 == "file": environment variable with an path to store the file to
::   (forced to empty word, if %~6 equals directory).
::

   setlocal
   set "input=%~2"
   set "text=%~3"
   if        "%~4" == "absolutePath" ( set "pathType=%~4"
   ) else if "%~4" == "relativePath" ( set "pathType=%~4"
   ) else set "existType=anyPath"
   if        "%~5" == "mustExist" ( set "existType=%~5"
   ) else if "%~5" == "doesntExist" ( set "existType=%~5"
   ) else set "existType=mayExist"
   setlocal enableDelayedExpansion
   set "parent= "
   if "%~6" == "file" (
      set "parent= !%~8!"
      if not "!parent:~-1!" == ":" (
         set "parent=!parent!\"
         if "!parent:~-2!" == "\\" set "parent=!parent:~0,-1!"
      )
   )
   endlocal & set "parent=%parent:~1%"
   if "%~6" == "file" (
      set "fileType=file"
      set "pathType=relativePath"
      if defined parent (
         if not exist "%parent%" set "existType=mayExist"
      ) else set "existType=mayExist"
   ) else set "fileType=directory"
   if        "%~7" == "r"  ( set "checkAccess=r-"
   ) else if "%~7" == "w"  ( set "checkAccess=-w"
   ) else if "%~7" == "rw" ( set "checkAccess=rw"
   ) else                  ( set "checkAccess=--"
   )

:validatePath
   :: validating
   set "invalid="
   call :isValidPathName "input" || goto :invalidatedPath
   if        /I     "%pathType%" == "absolutePath" ( call :isAbsolutePathName "input" || ( set "invalid=Path must be absolute" & goto :invalidatedPath )
   ) else if /I     "%pathType%" == "relativePath" ( call :isAbsolutePathName "input" && ( set "invalid=Path must be relative" & goto :invalidatedPath ) || >&2 >nul (echo @maybe: replace z: in !input!)
   )
   setlocal enableExtensions enableDelayedExpansion
   set "input=!parent!!input!"
   if        /I     "%existType%" == "mustExist"   ( if not exist "!input!" ( endlocal & set "invalid=Path must exist." & goto :invalidatedPath           )
   ) else if /I     "%existType%" == "doesntExist" ( if     exist "!input!" ( endlocal & set "invalid=Path must be non existent." & goto :invalidatedPath )
   )

   if exist "!input!" for %%b in ("!input!") do (
      set "attribs=%%~ab"
      if /I "%fileType%" == "file" (
         if not "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a file (not a directory)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" (
               2>nul <"!input!" set /p "check=" || (
                  for %%a in ("!input!") do if "%%~za" == "0" (
                     >&2 echo(Warning: Could not verify read access: Reason filesize of "!input!" is 0.
                  ) else (
                     endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath
                  )
               )
            ) else if "%checkAccess:~1,1%" == "w" 2>nul (
               >"!input!" <nul set /p "check=" || ( endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
            )
         )
      ) else (
         if "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a directory (not a file)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" 2>nul (
               >nul dir "!input!" || ( endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath )
            )
            if "%checkAccess:~1,1%" == "w" 2>nul (
               >nul pushd "!input!"
               >"test.tmp" echo test || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul del "test.tmp"   || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul popd
            )
         )
      )
   )

   endlocal
   endlocal & set "%~1=%input%"
   goto :eof






:invalidatedPath
   echo(Invalid: %invalid%
   set /P ^"input=%text:""="% "
   goto :validatePath
   goto :eof


:: Check if the pathname follows mainly the naming convention:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
:: Exceptions:
::  - Only a drive descriptor (for example "C:") is allowed for volume names.
::  - Not tested if characters 0x00-0x31, or any other forbidden character (of the target filesystem) are present. (disallowed)
::  - Not tested on alternative data streams
:isValidPathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   set "pathName=!%~1!"
   set "invalid="
   if not defined invalid if "!pathName!" == "" set "invalid=The pathname cannot be empty." & set "pathName= "
   if not defined invalid for /f tokens^=1^*^ delims^=^/^<^>^|^*^?^" %%a in ("#!pathName!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   if not defined invalid set ^"pathname=!pathname:"=!"

   if not defined invalid (
      set "pathName= !pathName:/=\!"
>nul echo @maybe:set "pathName= !pathName:\..\=\!"
>nul echo @maybe:set "pathName= !pathName:\.\=\!"
>nul echo @maybe:if "%pathName:~-3%" == "\.." set "pathName= !pathName:~0,-3!"
>nul echo @maybe:if "%pathName:~-2%" == "\." set "pathName= !pathName:~0,-2!"
   )

   if not defined invalid if not "!pathName:\\=__!" == "!pathname!" set "invalid=Empty string is no valid path component."
   if not defined invalid if "!pathName:~1,1!" == ":" (
      for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("#!pathName:~0,1!#") do if not "%%~a" == "#" set "invalid=Invalid drive name."
      set "pathName=!pathName:~2!"
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /I /R /C:"[\\]AUX[\\]" /C:"[\\]CON[\\]" /C:"[\\]NUL[\\]" /C:"[\\]PRN[\\]" /C:"[\\]COM[0-9][\\]" /C:"[\\]LPT[0-9][\\]" && (
         set "invalid=The following names are not allowed for file/directory names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9."
      )
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /R /C:"[\ ][\\]" /C:"[\.][\\]" && (
         set "invalid=It is not allowed to end file/directory names with a space (' ') or a period ('.') character."
      )
   )
   if not defined invalid (
      endlocal
      exit /b 0
   ) else (
      endlocal & set "invalid=%invalid%"
      exit /b 1
   )
   goto :eof


:: Using mainly the definition of the Windows Shell API, see:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773660(v=vs.85).aspx
:: Exception:
::  - Only accepts a drive descriptor (for example "C:") is allowed for volume names.
::
:: does not check validity (must be valid)
:isAbsolutePathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   if not "!%~1:~1,2!" == ":\" (
      endlocal
      exit /b 1
   ) else (
      endlocal
      exit /b 0
   )
   goto :eof


Call it somehow like this:

Code: Select all

test.bat "Z:\1" "Z:\2" "Z:\3" "0.txt"


penpen


The only elements that must be present on the computer are logically the ESD directory and file.

Now, when I run the script with four arguments, this

Code: Select all

D:\Users\Public\Documents\Balubeto\Test_script>Test_01.bat "D:\Users\Public\Documents\Balubeto\Test_script\WSM" "D:\Users\Public\Documents\Balubeto\Test_script\iso" "D:\Users\Public\Documents\Balubeto\Test_script\esd" "Install.esd"
Invalid: Path must exist.
Enter the directory in which put the content of the "Windows Setup Media" volume image:


is displayed instead of the end result. Why?

While, if I run the script without arguments, I reach the end result but, before the first input prompt, the message "Invalid: The pathname cannot be empty." is displayed. Why?

Thanks

Bye

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

Re: Script error during the startup

#4 Post by penpen » 02 Sep 2016 06:24

balubeto wrote:The only elements that must be present on the computer are logically the ESD directory and file.

Now, when I run the script with four arguments, this

Code: Select all

D:\Users\Public\Documents\Balubeto\Test_script>Test_01.bat "D:\Users\Public\Documents\Balubeto\Test_script\WSM" "D:\Users\Public\Documents\Balubeto\Test_script\iso" "D:\Users\Public\Documents\Balubeto\Test_script\esd" "Install.esd"
Invalid: Path must exist.
Enter the directory in which put the content of the "Windows Setup Media" volume image:


is displayed instead of the end result. Why?
I probably explained to few:
Sorry for that.

There are some options to control the verification (params from left to right):
See section ":setValidPath"; (Params are %~1 %~2, ... %~8) and ask if something is still unclear.

In your case the option "mustExist" is wrong for the first path, so you have to replace it by "mayExist":

Code: Select all

call :setValidPath "Windows_Files_Path" "%~1" "Enter the directory in which put the content of the ""Windows Setup Media"" volume image:" "absolutePath" "mayExist"  "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"  "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist" "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist" "file"      "rw" "esd_File_Path"

Check the other parameter values, if they meet your rewuirements.


balubeto wrote:While, if I run the script without arguments, I reach the end result but, before the first input prompt, the message "Invalid: The pathname cannot be empty." is displayed. Why?
Because the initial value (defined by the associated parameter) is an empty string.
Is it not supposed to work that way?
Or should an empty string be ignored the first time?


penpen

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

Re: Script error during the startup

#5 Post by balubeto » 02 Sep 2016 10:19

penpen wrote:
balubeto wrote:The only elements that must be present on the computer are logically the ESD directory and file.

Now, when I run the script with four arguments, this

Code: Select all

D:\Users\Public\Documents\Balubeto\Test_script>Test_01.bat "D:\Users\Public\Documents\Balubeto\Test_script\WSM" "D:\Users\Public\Documents\Balubeto\Test_script\iso" "D:\Users\Public\Documents\Balubeto\Test_script\esd" "Install.esd"
Invalid: Path must exist.
Enter the directory in which put the content of the "Windows Setup Media" volume image:


is displayed instead of the end result. Why?
I probably explained to few:
Sorry for that.

There are some options to control the verification (params from left to right):
See section ":setValidPath"; (Params are %~1 %~2, ... %~8) and ask if something is still unclear.

In your case the option "mustExist" is wrong for the first path, so you have to replace it by "mayExist":

Code: Select all

call :setValidPath "Windows_Files_Path" "%~1" "Enter the directory in which put the content of the ""Windows Setup Media"" volume image:" "absolutePath" "mayExist"  "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"  "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist" "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist" "file"      "rw" "esd_File_Path"

Check the other parameter values, if they meet your rewuirements.


balubeto wrote:While, if I run the script without arguments, I reach the end result but, before the first input prompt, the message "Invalid: The pathname cannot be empty." is displayed. Why?
Because the initial value (defined by the associated parameter) is an empty string.
Is it not supposed to work that way?
Or should an empty string be ignored the first time?


penpen


The first problem is solved with your last change. If necessary, it is possible to make sure that the script asks to enter only the third or fourth argument when these do not exist on the computer (also because the first and second argument have already been written on the command line)?

Regarding the second problem, it is possible to prevent that the script analyzes the first argument null only when the script is run without arguments to avoid the writing "Invalid: The pathname cannot be empty." before the first input prompt?

Thanks

Bye

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

Re: Script error during the startup

#6 Post by penpen » 02 Sep 2016 12:37

balubeto wrote:The first problem is solved with your last change. If necessary, it is possible to make sure that the script asks to enter only the third or fourth argument when these do not exist on the computer (also because the first and second argument have already been written on the command line)?
I'm not sure if i understand that question right... .
As you said the problem is solved, so the answer to your question is (obviously):
Yes, it is possible, as long as the first two arguments contain valid pathes.
Or do i misinterpret your question?

balubeto wrote:Regarding the second problem, it is possible to prevent that the script analyzes the first argument null only when the script is run without arguments to avoid the writing "Invalid: The pathname cannot be empty." before the first input prompt?
Yes, it is; this may be what you want:

Code: Select all

@echo off
cls
setlocal enableExtensions disableDelayedExpansion

call :setValidPath "Windows_Files_Path" "%~1" "Enter the directory in which put the content of the ""Windows Setup Media"" volume image:" "absolutePath" "mayExist"   "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"   "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist"  "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist"  "file"      "rw" "esd_File_Path"

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
:: %~4   Text equals: (forced to relativePath if %~6 equals file)
::     absolutePath  if path must be absolute
::     relativePath  if path must be relative
::     anyPath       if path may be relative or absolute (any other value)
:: %~5   Text equals: (forced to mayExist if %~8 does not exist)
::     mustExist     if file/directory must exist,
::     doesntExist   if file/directory is not allowed to exist.
::     mayExist      if file/directory may or may not exist (any other value).
:: %~6   Text equals:
::     file          if object to test is a file
::     directory     if object to test is a directory (any other value).
:: %~7   Text equals: (only checked if file/directory exists)
::     r             check read access for specified file/directory
::     w             check write access for specified file/directory
::     rw            check read and write access for specified file/directory
::     -             check no access for specified file/directory (any other value)
::     directory     if object to test is a directory (any other value).
::     Note: testing write access on a directory creates a file "test.tmp"
:: %~8   Only if /I %~6 == "file": environment variable with an path to store the file to
::   (forced to empty word, if %~6 equals directory).
::

   setlocal
   set "firstTime=1"
   set "input=%~2"
   set "text=%~3"
   if        "%~4" == "absolutePath" ( set "pathType=%~4"
   ) else if "%~4" == "relativePath" ( set "pathType=%~4"
   ) else set "existType=anyPath"
   if        "%~5" == "mustExist" ( set "existType=%~5"
   ) else if "%~5" == "doesntExist" ( set "existType=%~5"
   ) else set "existType=mayExist"
   setlocal enableDelayedExpansion
   set "parent= "
   if "%~6" == "file" (
      set "parent= !%~8!"
      if not "!parent:~-1!" == ":" (
         set "parent=!parent!\"
         if "!parent:~-2!" == "\\" set "parent=!parent:~0,-1!"
      )
   )
   endlocal & set "parent=%parent:~1%"
   if "%~6" == "file" (
      set "fileType=file"
      set "pathType=relativePath"
      if defined parent (
         if not exist "%parent%" set "existType=mayExist"
      ) else set "existType=mayExist"
   ) else set "fileType=directory"
   if        "%~7" == "r"  ( set "checkAccess=r-"
   ) else if "%~7" == "w"  ( set "checkAccess=-w"
   ) else if "%~7" == "rw" ( set "checkAccess=rw"
   ) else                  ( set "checkAccess=--"
   )

:validatePath
   :: validating
   set "invalid="
   call :isValidPathName "input" || goto :invalidatedPath
   if        /I     "%pathType%" == "absolutePath" ( call :isAbsolutePathName "input" || ( set "invalid=Path must be absolute" & goto :invalidatedPath )
   ) else if /I     "%pathType%" == "relativePath" ( call :isAbsolutePathName "input" && ( set "invalid=Path must be relative" & goto :invalidatedPath ) || >&2 >nul (echo @maybe: replace "<volume label>:" in !input!)
   )
   setlocal enableExtensions enableDelayedExpansion
   set "input=!parent!!input!"
   if        /I     "%existType%" == "mustExist"   ( if not exist "!input!" ( endlocal & set "invalid=Path must exist." & goto :invalidatedPath           )
   ) else if /I     "%existType%" == "doesntExist" ( if     exist "!input!" ( endlocal & set "invalid=Path must be non existent." & goto :invalidatedPath )
   )

   if exist "!input!" for %%b in ("!input!") do (
      set "attribs=%%~ab"
      if /I "%fileType%" == "file" (
         if not "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a file (not a directory)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" (
               2>nul <"!input!" set /p "check=" || (
                  for %%a in ("!input!") do if "%%~za" == "0" (
                     >&2 echo(Warning: Could not verify read access: Reason filesize of "!input!" is 0.
                  ) else (
                     endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath
                  )
               )
            ) else if "%checkAccess:~1,1%" == "w" 2>nul (
               >"!input!" <nul set /p "check=" || ( endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
            )
         )
      ) else (
         if "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a directory (not a file)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" 2>nul (
               >nul dir "!input!" || ( endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath )
            )
            if "%checkAccess:~1,1%" == "w" 2>nul (
               >nul pushd "!input!"
               >"test.tmp" echo test || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul del "test.tmp"   || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul popd
            )
         )
      )
   )

   endlocal
   endlocal & set "%~1=%input%"
   goto :eof

:invalidatedPath
   if defined invalid echo(Invalid: %invalid%
   set /P ^"input=%text:""="% "
   goto :validatePath
   goto :eof


:: Check if the pathname follows mainly the naming convention:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
:: Exceptions:
::  - Only a drive descriptor (for example "C:") is allowed for volume names.
::  - Not tested if characters 0x00-0x31, or any other forbidden character (of the target filesystem) are present. (disallowed)
::  - Not tested on alternative data streams
:isValidPathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   set "pathName=!%~1!"
   set "invalid="
   if not defined invalid if "!pathName!" == "" if defined firstTime (exit /b 1 & goto :test) else set "invalid=The pathname cannot be empty." & set "pathName= "
   if not defined invalid for /f tokens^=1^*^ delims^=^/^<^>^|^*^?^" %%a in ("#!pathName!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   if not defined invalid set ^"pathname=!pathname:"=!"

   if not defined invalid (
      set "pathName= !pathName:/=\!"
>nul echo @maybe:set "pathName= !pathName:\..\=\!"
>nul echo @maybe:set "pathName= !pathName:\.\=\!"
>nul echo @maybe:if "%pathName:~-3%" == "\.." set "pathName= !pathName:~0,-3!"
>nul echo @maybe:if "%pathName:~-2%" == "\." set "pathName= !pathName:~0,-2!"
   )

   if not defined invalid if not "!pathName:\\=__!" == "!pathname!" set "invalid=Empty string is no valid path component."
   if not defined invalid if "!pathName:~1,1!" == ":" (
      for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("#!pathName:~0,1!#") do if not "%%~a" == "#" set "invalid=Invalid drive name."
      set "pathName=!pathName:~2!"
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /I /R /C:"[\\]AUX[\\]" /C:"[\\]CON[\\]" /C:"[\\]NUL[\\]" /C:"[\\]PRN[\\]" /C:"[\\]COM[0-9][\\]" /C:"[\\]LPT[0-9][\\]" && (
         set "invalid=The following names are not allowed for file/directory names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9."
      )
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /R /C:"[\ ][\\]" /C:"[\.][\\]" && (
         set "invalid=It is not allowed to end file/directory names with a space (' ') or a period ('.') character."
      )
   )
   if not defined invalid (
      endlocal
      exit /b 0
   ) else (
      endlocal & set "invalid=%invalid%"
      exit /b 1
   )
   goto :eof


:: Using mainly the definition of the Windows Shell API, see:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773660(v=vs.85).aspx
:: Exception:
::  - Only accepts a drive descriptor (for example "C:") is allowed for volume names.
::
:: does not check validity (must be valid)
:isAbsolutePathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   if not "!%~1:~1,2!" == ":\" (
      endlocal
      exit /b 1
   ) else (
      endlocal
      exit /b 0
   )
   goto :eof


penpen

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

Re: Script error during the startup

#7 Post by balubeto » 03 Sep 2016 10:00

penpen wrote:
balubeto wrote:The first problem is solved with your last change. If necessary, it is possible to make sure that the script asks to enter only the third or fourth argument when these do not exist on the computer (also because the first and second argument have already been written on the command line)?
I'm not sure if i understand that question right... .
As you said the problem is solved, so the answer to your question is (obviously):
Yes, it is possible, as long as the first two arguments contain valid pathes.
Or do i misinterpret your question?

balubeto wrote:Regarding the second problem, it is possible to prevent that the script analyzes the first argument null only when the script is run without arguments to avoid the writing "Invalid: The pathname cannot be empty." before the first input prompt?
Yes, it is; this may be what you want:

Code: Select all

@echo off
cls
setlocal enableExtensions disableDelayedExpansion

call :setValidPath "Windows_Files_Path" "%~1" "Enter the directory in which put the content of the ""Windows Setup Media"" volume image:" "absolutePath" "mayExist"   "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"   "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist"  "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist"  "file"      "rw" "esd_File_Path"

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
:: %~4   Text equals: (forced to relativePath if %~6 equals file)
::     absolutePath  if path must be absolute
::     relativePath  if path must be relative
::     anyPath       if path may be relative or absolute (any other value)
:: %~5   Text equals: (forced to mayExist if %~8 does not exist)
::     mustExist     if file/directory must exist,
::     doesntExist   if file/directory is not allowed to exist.
::     mayExist      if file/directory may or may not exist (any other value).
:: %~6   Text equals:
::     file          if object to test is a file
::     directory     if object to test is a directory (any other value).
:: %~7   Text equals: (only checked if file/directory exists)
::     r             check read access for specified file/directory
::     w             check write access for specified file/directory
::     rw            check read and write access for specified file/directory
::     -             check no access for specified file/directory (any other value)
::     directory     if object to test is a directory (any other value).
::     Note: testing write access on a directory creates a file "test.tmp"
:: %~8   Only if /I %~6 == "file": environment variable with an path to store the file to
::   (forced to empty word, if %~6 equals directory).
::

   setlocal
   set "firstTime=1"
   set "input=%~2"
   set "text=%~3"
   if        "%~4" == "absolutePath" ( set "pathType=%~4"
   ) else if "%~4" == "relativePath" ( set "pathType=%~4"
   ) else set "existType=anyPath"
   if        "%~5" == "mustExist" ( set "existType=%~5"
   ) else if "%~5" == "doesntExist" ( set "existType=%~5"
   ) else set "existType=mayExist"
   setlocal enableDelayedExpansion
   set "parent= "
   if "%~6" == "file" (
      set "parent= !%~8!"
      if not "!parent:~-1!" == ":" (
         set "parent=!parent!\"
         if "!parent:~-2!" == "\\" set "parent=!parent:~0,-1!"
      )
   )
   endlocal & set "parent=%parent:~1%"
   if "%~6" == "file" (
      set "fileType=file"
      set "pathType=relativePath"
      if defined parent (
         if not exist "%parent%" set "existType=mayExist"
      ) else set "existType=mayExist"
   ) else set "fileType=directory"
   if        "%~7" == "r"  ( set "checkAccess=r-"
   ) else if "%~7" == "w"  ( set "checkAccess=-w"
   ) else if "%~7" == "rw" ( set "checkAccess=rw"
   ) else                  ( set "checkAccess=--"
   )

:validatePath
   :: validating
   set "invalid="
   call :isValidPathName "input" || goto :invalidatedPath
   if        /I     "%pathType%" == "absolutePath" ( call :isAbsolutePathName "input" || ( set "invalid=Path must be absolute" & goto :invalidatedPath )
   ) else if /I     "%pathType%" == "relativePath" ( call :isAbsolutePathName "input" && ( set "invalid=Path must be relative" & goto :invalidatedPath ) || >&2 >nul (echo @maybe: replace "<volume label>:" in !input!)
   )
   setlocal enableExtensions enableDelayedExpansion
   set "input=!parent!!input!"
   if        /I     "%existType%" == "mustExist"   ( if not exist "!input!" ( endlocal & set "invalid=Path must exist." & goto :invalidatedPath           )
   ) else if /I     "%existType%" == "doesntExist" ( if     exist "!input!" ( endlocal & set "invalid=Path must be non existent." & goto :invalidatedPath )
   )

   if exist "!input!" for %%b in ("!input!") do (
      set "attribs=%%~ab"
      if /I "%fileType%" == "file" (
         if not "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a file (not a directory)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" (
               2>nul <"!input!" set /p "check=" || (
                  for %%a in ("!input!") do if "%%~za" == "0" (
                     >&2 echo(Warning: Could not verify read access: Reason filesize of "!input!" is 0.
                  ) else (
                     endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath
                  )
               )
            ) else if "%checkAccess:~1,1%" == "w" 2>nul (
               >"!input!" <nul set /p "check=" || ( endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
            )
         )
      ) else (
         if "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a directory (not a file)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" 2>nul (
               >nul dir "!input!" || ( endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath )
            )
            if "%checkAccess:~1,1%" == "w" 2>nul (
               >nul pushd "!input!"
               >"test.tmp" echo test || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul del "test.tmp"   || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul popd
            )
         )
      )
   )

   endlocal
   endlocal & set "%~1=%input%"
   goto :eof

:invalidatedPath
   if defined invalid echo(Invalid: %invalid%
   set /P ^"input=%text:""="% "
   goto :validatePath
   goto :eof


:: Check if the pathname follows mainly the naming convention:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
:: Exceptions:
::  - Only a drive descriptor (for example "C:") is allowed for volume names.
::  - Not tested if characters 0x00-0x31, or any other forbidden character (of the target filesystem) are present. (disallowed)
::  - Not tested on alternative data streams
:isValidPathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   set "pathName=!%~1!"
   set "invalid="
   if not defined invalid if "!pathName!" == "" if defined firstTime (exit /b 1 & goto :test) else set "invalid=The pathname cannot be empty." & set "pathName= "
   if not defined invalid for /f tokens^=1^*^ delims^=^/^<^>^|^*^?^" %%a in ("#!pathName!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   if not defined invalid set ^"pathname=!pathname:"=!"

   if not defined invalid (
      set "pathName= !pathName:/=\!"
>nul echo @maybe:set "pathName= !pathName:\..\=\!"
>nul echo @maybe:set "pathName= !pathName:\.\=\!"
>nul echo @maybe:if "%pathName:~-3%" == "\.." set "pathName= !pathName:~0,-3!"
>nul echo @maybe:if "%pathName:~-2%" == "\." set "pathName= !pathName:~0,-2!"
   )

   if not defined invalid if not "!pathName:\\=__!" == "!pathname!" set "invalid=Empty string is no valid path component."
   if not defined invalid if "!pathName:~1,1!" == ":" (
      for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("#!pathName:~0,1!#") do if not "%%~a" == "#" set "invalid=Invalid drive name."
      set "pathName=!pathName:~2!"
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /I /R /C:"[\\]AUX[\\]" /C:"[\\]CON[\\]" /C:"[\\]NUL[\\]" /C:"[\\]PRN[\\]" /C:"[\\]COM[0-9][\\]" /C:"[\\]LPT[0-9][\\]" && (
         set "invalid=The following names are not allowed for file/directory names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9."
      )
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /R /C:"[\ ][\\]" /C:"[\.][\\]" && (
         set "invalid=It is not allowed to end file/directory names with a space (' ') or a period ('.') character."
      )
   )
   if not defined invalid (
      endlocal
      exit /b 0
   ) else (
      endlocal & set "invalid=%invalid%"
      exit /b 1
   )
   goto :eof


:: Using mainly the definition of the Windows Shell API, see:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773660(v=vs.85).aspx
:: Exception:
::  - Only accepts a drive descriptor (for example "C:") is allowed for volume names.
::
:: does not check validity (must be valid)
:isAbsolutePathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   if not "!%~1:~1,2!" == ":\" (
      endlocal
      exit /b 1
   ) else (
      endlocal
      exit /b 0
   )
   goto :eof


penpen


You have understood perfectly what I wanted.

Now, I tried to add an argument associated with the iso_File variable

Code: Select all

@echo off
cls
setlocal enableExtensions disableDelayedExpansion

call :setValidPath "Windows_Files_Path" "%~1" "Enter the directory in which put the content of the ""Windows Setup Media"" volume image:" "absolutePath" "mayExist"   "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"   "directory"  "--"
call :setValidPath "esd_Path"           "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist"  "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_Path% directory:"           "relativePath" "mustExist"  "file"       "rw" "esd_Path"
call :setValidPath "iso_File"           "%~5" "Enter the iso file that will be saved in the %iso_Path% directory:"                        "relativePath" "mayExist"   "file"       "rw" "iso_Path"

echo(
echo Result:
echo Windows_Files_Path: "%Windows_Files_Path%"
echo iso_Path          : "%iso_Path%"
echo esd_Path          : "%esd_Path%"
echo esd_File          : "%esd_File%"
echo iso_File          : "%iso_File%"

:: put your code here

endlocal
goto :eof


:setValidPath
:: %~1   variable to set
:: %~2   default value
:: %~3   text if default value is invalid
:: %~4   Text equals: (forced to relativePath if %~6 equals file)
::     absolutePath  if path must be absolute
::     relativePath  if path must be relative
::     anyPath       if path may be relative or absolute (any other value)
:: %~5   Text equals: (forced to mayExist if %~8 does not exist)
::     mustExist     if file/directory must exist,
::     doesntExist   if file/directory is not allowed to exist.
::     mayExist      if file/directory may or may not exist (any other value).
:: %~6   Text equals:
::     file          if object to test is a file
::     directory     if object to test is a directory (any other value).
:: %~7   Text equals: (only checked if file/directory exists)
::     r             check read access for specified file/directory
::     w             check write access for specified file/directory
::     rw            check read and write access for specified file/directory
::     -             check no access for specified file/directory (any other value)
::     directory     if object to test is a directory (any other value).
::     Note: testing write access on a directory creates a file "test.tmp"
:: %~8   Only if /I %~6 == "file": environment variable with an path to store the file to
::   (forced to empty word, if %~6 equals directory).
::
 
   setlocal
   set "firstTime=1"
   set "input=%~2"
   set "text=%~3"
   if        "%~4" == "absolutePath" ( set "pathType=%~4"
   ) else if "%~4" == "relativePath" ( set "pathType=%~4"
   ) else set "existType=anyPath"
   if        "%~5" == "mustExist" ( set "existType=%~5"
   ) else if "%~5" == "doesntExist" ( set "existType=%~5"
   ) else set "existType=mayExist"
   setlocal enableDelayedExpansion
   set "parent= "
   if "%~6" == "file" (
      set "parent= !%~8!"
      if not "!parent:~-1!" == ":" (
         set "parent=!parent!\"
         if "!parent:~-2!" == "\\" set "parent=!parent:~0,-1!"
      )
   )
   endlocal & set "parent=%parent:~1%"
   if "%~6" == "file" (
      set "fileType=file"
      set "pathType=relativePath"
      if defined parent (
         if not exist "%parent%" set "existType=mayExist"
      ) else set "existType=mayExist"
   ) else set "fileType=directory"
   if        "%~7" == "r"  ( set "checkAccess=r-"
   ) else if "%~7" == "w"  ( set "checkAccess=-w"
   ) else if "%~7" == "rw" ( set "checkAccess=rw"
   ) else                  ( set "checkAccess=--"
   )

:validatePath
   :: validating
   set "invalid="
   call :isValidPathName "input" || goto :invalidatedPath
   if        /I     "%pathType%" == "absolutePath" ( call :isAbsolutePathName "input" || ( set "invalid=Path must be absolute" & goto :invalidatedPath )
   ) else if /I     "%pathType%" == "relativePath" ( call :isAbsolutePathName "input" && ( set "invalid=Path must be relative" & goto :invalidatedPath ) || >&2 >nul (echo @maybe: replace "<volume label>:" in !input!)
   )
   setlocal enableExtensions enableDelayedExpansion
   set "input=!parent!!input!"
   if        /I     "%existType%" == "mustExist"   ( if not exist "!input!" ( endlocal & set "invalid=Path must exist." & goto :invalidatedPath           )
   ) else if /I     "%existType%" == "doesntExist" ( if     exist "!input!" ( endlocal & set "invalid=Path must be non existent." & goto :invalidatedPath )
   )

   if exist "!input!" for %%b in ("!input!") do (
      set "attribs=%%~ab"
      if /I "%fileType%" == "file" (
         if not "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a file (not a directory)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" (
               2>nul <"!input!" set /p "check=" || (
                  for %%a in ("!input!") do if "%%~za" == "0" (
                     >&2 echo(Warning: Could not verify read access: Reason filesize of "!input!" is 0.
                  ) else (
                     endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath
                  )
               )
            ) else if "%checkAccess:~1,1%" == "w" 2>nul (
               >"!input!" <nul set /p "check=" || ( endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
            )
         )
      ) else (
         if "!attribs:d=!" == "!attribs!" (
            endlocal & set "invalid=Path must denote a directory (not a file)." & goto :invalidatedPath
         ) else (
            if "%checkAccess:~0,1%" == "r" 2>nul (
               >nul dir "!input!" || ( endlocal & set "invalid=No read access (wanted)." & goto :invalidatedPath )
            )
            if "%checkAccess:~1,1%" == "w" 2>nul (
               >nul pushd "!input!"
               >"test.tmp" echo test || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul del "test.tmp"   || ( >nul popd & endlocal & set "invalid=No write access (wanted)." & goto :invalidatedPath )
               >nul popd
            )
         )
      )
   )

   endlocal
   endlocal & set "%~1=%input%"
   goto :eof

:invalidatedPath
   if defined invalid echo(Invalid: %invalid%
   set /P ^"input=%text:""="% "
   goto :validatePath
   goto :eof


:: Check if the pathname follows mainly the naming convention:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
:: Exceptions:
::  - Only a drive descriptor (for example "C:") is allowed for volume names.
::  - Not tested if characters 0x00-0x31, or any other forbidden character (of the target filesystem) are present. (disallowed)
::  - Not tested on alternative data streams
:isValidPathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   set "pathName=!%~1!"
   set "invalid="
   if not defined invalid if "!pathName!" == "" if defined firstTime (exit /b 1 & goto :test) else set "invalid=The pathname cannot be empty." & set "pathName= "
   if not defined invalid for /f tokens^=1^*^ delims^=^/^<^>^|^*^?^" %%a in ("#!pathName!#") do if not "%%~b" == "" set "invalid=Invalid character found."
   if not defined invalid set ^"pathname=!pathname:"=!"

   if not defined invalid (
      set "pathName= !pathName:/=\!"
>nul echo @maybe:set "pathName= !pathName:\..\=\!"
>nul echo @maybe:set "pathName= !pathName:\.\=\!"
>nul echo @maybe:if "%pathName:~-3%" == "\.." set "pathName= !pathName:~0,-3!"
>nul echo @maybe:if "%pathName:~-2%" == "\." set "pathName= !pathName:~0,-2!"
   )

   if not defined invalid if not "!pathName:\\=__!" == "!pathname!" set "invalid=Empty string is no valid path component."
   if not defined invalid if "!pathName:~1,1!" == ":" (
      for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" %%a in ("#!pathName:~0,1!#") do if not "%%~a" == "#" set "invalid=Invalid drive name."
      set "pathName=!pathName:~2!"
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /I /R /C:"[\\]AUX[\\]" /C:"[\\]CON[\\]" /C:"[\\]NUL[\\]" /C:"[\\]PRN[\\]" /C:"[\\]COM[0-9][\\]" /C:"[\\]LPT[0-9][\\]" && (
         set "invalid=The following names are not allowed for file/directory names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9."
      )
   )
   if not defined invalid 2>nul (
      echo(\!pathname!\|>nul findstr /R /C:"[\ ][\\]" /C:"[\.][\\]" && (
         set "invalid=It is not allowed to end file/directory names with a space (' ') or a period ('.') character."
      )
   )
   if not defined invalid (
      endlocal
      exit /b 0
   ) else (
      endlocal & set "invalid=%invalid%"
      exit /b 1
   )
   goto :eof


:: Using mainly the definition of the Windows Shell API, see:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773660(v=vs.85).aspx
:: Exception:
::  - Only accepts a drive descriptor (for example "C:") is allowed for volume names.
::
:: does not check validity (must be valid)
:isAbsolutePathName
:: %~1   environment variable containing the path to test.
   setlocal enableExtensions enableDelayedExpansion
   if not "!%~1:~1,2!" == ":\" (
      endlocal
      exit /b 1
   ) else (
      endlocal
      exit /b 0
   )
   goto :eof


but does not work properly because does not check the validity of the iso file name.

In addition, the script should check the existence of this iso file on the computer:

If it does not exist, the script should check the validity of his name; while if it there is, the script should ask if is necessary to overwrite it or not. If the user does not want to overwrite it, the script should request the user to another name and so on.

So, how do I do this?

Thanks

Bye

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

Re: Script error during the startup

#8 Post by penpen » 06 Sep 2016 08:06

Sorry, i'd few time the last days.

balubeto wrote:but does not work properly because does not check the validity of the iso file name.
It seems the validity is checked properly (you only added the lines "call :setValidPath "iso_File"..." and "echo iso_File : "%iso_File%"").
Which input leads to errors?

balubeto wrote:In addition, the script should check the existence of this iso file on the computer:

If it does not exist, the script should check the validity of his name; while if it there is, the script should ask if is necessary to overwrite it or not. If the user does not want to overwrite it, the script should request the user to another name and so on.

So, how do I do this?
You could add that to the validity check (if you want), or
you could also do this in the "main part" of the batch program (which i prefer), so this may help you:

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:" "absolutePath" "mayExist"   "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"   "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist"  "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist"  "file"      "rw" "esd_File_Path"

set "iso_File=%~5"
:set_iso_File
call :setValidPath "iso_File"           "%iso_File%" "Enter the iso file that will be saved in the %iso_Path% directory:"                        "relativePath" "mayExist"   "file"       "rw" "iso_Path"
call
if exist "%iso_Path%\%iso_File%" choice /M "Iso File exists; overwrite it " /C:YN
if not "%errorlevel%" == "1" set "iso_File=" & goto :set_iso_File


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%"
echo iso_File          : "%iso_File%"

:: put your code here

endlocal
goto :eof

:: ...


penpen

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

Re: Script error during the startup

#9 Post by balubeto » 06 Sep 2016 10:19

penpen wrote:Sorry, i'd few time the last days.

balubeto wrote:but does not work properly because does not check the validity of the iso file name.
It seems the validity is checked properly (you only added the lines "call :setValidPath "iso_File"..." and "echo iso_File : "%iso_File%"").
Which input leads to errors?

balubeto wrote:In addition, the script should check the existence of this iso file on the computer:

If it does not exist, the script should check the validity of his name; while if it there is, the script should ask if is necessary to overwrite it or not. If the user does not want to overwrite it, the script should request the user to another name and so on.

So, how do I do this?
You could add that to the validity check (if you want), or
you could also do this in the "main part" of the batch program (which i prefer), so this may help you:

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:" "absolutePath" "mayExist"   "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"   "directory"  "--"
call :setValidPath "esd_File_Path"      "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist"  "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_File_Path% directory:"      "relativePath" "mustExist"  "file"      "rw" "esd_File_Path"

set "iso_File=%~5"
:set_iso_File
call :setValidPath "iso_File"           "%iso_File%" "Enter the iso file that will be saved in the %iso_Path% directory:"                        "relativePath" "mayExist"   "file"       "rw" "iso_Path"
call
if exist "%iso_Path%\%iso_File%" choice /M "Iso File exists; overwrite it " /C:YN
if not "%errorlevel%" == "1" set "iso_File=" & goto :set_iso_File


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%"
echo iso_File          : "%iso_File%"

:: put your code here

endlocal
goto :eof

:: ...


penpen


When I insert an iso file that exists, the message "Warning: Could not verify read access: Reason filesize of "D:\Users\Public\Documents\Balubeto\Test_script\iso\Test.iso" is 0." is displayed before the request of overwriting the file.

Why this warning? How do I correct it in order to avoid its visualization?

Thanks

Bye

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

Re: Script error during the startup

#10 Post by penpen » 06 Sep 2016 12:07

balubeto wrote:When I insert an iso file that exists, the message "Warning: Could not verify read access: Reason filesize of "D:\Users\Public\Documents\Balubeto\Test_script\iso\Test.iso" is 0." is displayed before the request of overwriting the file.

Why this warning? How do I correct it in order to avoid its visualization?
This is only a warning and no error.
The text should be written to stderr stream (">&2 echo ...").
This warning is displayed if a file with a size of 0 bytes is verified for reading, because you cannot read from this file because it has no content.

There are multiple options to retrieve this goal:
1) You could avoid checking if read is possible from files you only want to write to, by using "-w" instead "rw" (param %~7; i recommend this way of handling this issue):

Code: Select all

call :setValidPath "iso_File"           "%iso_File%" "Enter the iso file that will be saved in the %iso_Path% directory:"                        "relativePath" "mayExist"   "file"       "-w" "iso_Path"
(Same for the other file if you don't want to read from it.)
2) You could also redirect stderr to null (using "2>nul call :setValidPath "iso_File" ...") when calling ":setValidPath".
3) Another option were to redirect stderr stream to nul when executing the batch (using: "2>nul test.bat "Z:\" ...").

penpen

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

esd ---> iso script (Work in Progress)

#11 Post by balubeto » 07 Sep 2016 02:13

penpen wrote:
balubeto wrote:When I insert an iso file that exists, the message "Warning: Could not verify read access: Reason filesize of "D:\Users\Public\Documents\Balubeto\Test_script\iso\Test.iso" is 0." is displayed before the request of overwriting the file.

Why this warning? How do I correct it in order to avoid its visualization?
This is only a warning and no error.
The text should be written to stderr stream (">&2 echo ...").
This warning is displayed if a file with a size of 0 bytes is verified for reading, because you cannot read from this file because it has no content.

There are multiple options to retrieve this goal:
1) You could avoid checking if read is possible from files you only want to write to, by using "-w" instead "rw" (param %~7; i recommend this way of handling this issue):

Code: Select all

call :setValidPath "iso_File"           "%iso_File%" "Enter the iso file that will be saved in the %iso_Path% directory:"                        "relativePath" "mayExist"   "file"       "-w" "iso_Path"
(Same for the other file if you don't want to read from it.)
2) You could also redirect stderr to null (using "2>nul call :setValidPath "iso_File" ...") when calling ":setValidPath".
3) Another option were to redirect stderr stream to nul when executing the batch (using: "2>nul test.bat "Z:\" ...").

penpen


I understood and, now, my piece of script is:

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:" "absolutePath" "mayExist"   "directory"  "rw"
call :setValidPath "iso_Path"           "%~2" "Enter the directory in which put the iso image file created:"                              "absolutePath" "mayExist"   "directory"  "--"
call :setValidPath "esd_Path"           "%~3" "Enter the directory in which put the esd unencrypted file:"                                "absolutePath" "mustExist"  "directory"  "--"
call :setValidPath "esd_File"           "%~4" "Enter the file to be converted which should be put in the %esd_Path% directory:"           "relativePath" "mustExist"  "file"       "rw" "esd_Path"
set "iso_File=%~5"
:set_iso_File
call :setValidPath "iso_File"           "%iso_File%" "Enter the iso file that will be saved in the %iso_Path% directory:"                        "relativePath" "mayExist"   "file"       "-w" "iso_Path"
call
if exist "%iso_Path%\%iso_File%" choice /M "Iso File exists; overwrite it " /C:YN
if not "%errorlevel%" == "1" set "iso_File=" & goto :set_iso_File


echo(
echo Result:
echo Windows_Files_Path: "%Windows_Files_Path%"
echo iso_Path          : "%iso_Path%"
echo esd_Path          : "%esd_Path%"
echo esd_File          : "%esd_File%"
echo iso_File          : "%iso_File%"

:: put your code here

endlocal

goto :eof


At this point, in the script, I should use the Oscdimg utility to create an iso image with only the UDF 1.02 file system.

So, I should prompt the user to also enter its volume label and the timestamp (in the mm/dd/yyyy,hh:mm:ss format) for all its files and directories, checking, logically, the validity of these two elements.

Now, how should I do to get to this result:

Code: Select all

echo Result:
echo Windows_Files_Path: "%Windows_Files_Path%"
echo iso_Path          : "%iso_Path%"
echo esd_Path          : "%esd_Path%"
echo esd_File          : "%esd_File%"
echo iso_File          : "%iso_File%"
echo Timestamp         : "%Timestamp%"
echo iso_Label         : "%iso_label%"


Thanks

Bye

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

Re: esd ---> iso script (Work in Progress)

#12 Post by penpen » 07 Sep 2016 06:21

The timestamp should be built easily, but your local time/date string it is highly system dependent.
You could probably use the helper bat file getTimeStamp.bat created by dbenham.

If that doesn't help you, then please post the result of:

Code: Select all

echo %date%,%time%


I'm not familiar with UDF 1.02 - i only know that some old versions of that only allows capital letters ('A' ... 'Z'), digits ('0' ... '9') and underscore ('_') for label names (with a maximum length of 12 characters), but i cannot say if that's also true for UDF 1.02.
(It could be that UDF 1.02 also supports Unicode and more characters: I don't know.)
Maybe you could test it (with a virtual machine like qemu, vbox, ... and iso files built with Oscdimg), and post your results.


penpen

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

Re: esd ---> iso script (Work in Progress)

#13 Post by balubeto » 07 Sep 2016 11:37

penpen wrote:The timestamp should be built easily, but your local time/date string it is highly system dependent.
You could probably use the helper bat file getTimeStamp.bat created by dbenham.

If that doesn't help you, then please post the result of:

Code: Select all

echo %date%,%time%


I'm not familiar with UDF 1.02 - i only know that some old versions of that only allows capital letters ('A' ... 'Z'), digits ('0' ... '9') and underscore ('_') for label names (with a maximum length of 12 characters), but i cannot say if that's also true for UDF 1.02.
(It could be that UDF 1.02 also supports Unicode and more characters: I don't know.)
Maybe you could test it (with a virtual machine like qemu, vbox, ... and iso files built with Oscdimg), and post your results.


penpen


A moment :

Since the -t option of the Oscdimg command https://msdn.microsoft.com/en-us/library/hh824847.aspx is independent of the operating system language used, the string entered by the user should always be compatible with the format mm/dd/yyyy,hh:mm:ss. Therefore, the script should test this and that this string has a logical sense. Right?

Regarding the second problem, I wrote here https://social.technet.microsoft.com/Fo ... itprosetup .

Thanks

Bye

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: esd ---> iso script (Work in Progress)

#14 Post by Squashman » 07 Sep 2016 12:15

balubeto wrote:Since the -t option of the Oscdimg command https://msdn.microsoft.com/en-us/library/hh824847.aspx is independent of the operating system language used, the string entered by the user should always be compatible with the format mm/dd/yyyy,hh:mm:ss. Therefore, the script should test this and that this string has a logical sense. Right?

Did you perhaps attempt to figure out how to code this or even at least Google Search for a batch file that already does date and time validation?

Date Validation
http://stackoverflow.com/a/20415739/1417694

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: esd ---> iso script (Work in Progress)

#15 Post by Squashman » 07 Sep 2016 12:28

Here is a dirty trick to validate a date.

Code: Select all

C:\BatchFiles>xcopy /d:02-29-2016 /l . .. >nul 2>&1 &&echo a valid date
a valid date

C:\BatchFiles>xcopy /d:02-29-2015 /l . .. >nul 2>&1 ||echo not a valid date
not a valid date

Post Reply