Bat script with some problems (explanation)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Bat script with some problems (explanation)

#1 Post by balubeto » 26 Jul 2016 04:50

Hi

Explanation:

I'm trying to create a bat script that will convert an unencrypted esd file, containing four volume images and placed in any directory of a local disk, in an iso image.

My script would be this:

Code: Select all

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

Set "MaxRC=0"
call :VetDir "esd_Genuine_File_Path" "Enter the directory of the esd genuine unencrypted file:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
if "%errorlevel%" EQU "0" (
  call :VetFile "esd_File" "Enter the esd unencrypted file to be converted which should be put in the %esd_Genuine_File_Path% directory:" "%esd_Genuine_File_Path%"
)
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetDir "esd_File_Path" "Enter the directory in which put the esd unencrypted file:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetDir "Windows_Files_Path" "Enter the directory in which put the Windows files:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetDir "iso_Path" "Enter the directory in which put the iso image file created:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
If "%errorlevel%" EQU "0" (
  call :VetFile "iso_File" "Enter the file for the creation of the iso image in the %iso_Path% directory:" "%iso_Path%"
)
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetISOLabel iso_Label "Enter the iso image label:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"
call :VetCreationDate Creation_Date "Enter the creation date of the iso image in the mm/dd/yyyy,hh:mm:ss format:"
If "%errorlevel%" GTR "%MaxRC%" Set "MaxRc=%errorlevel%"

echo.
If "%MaxRC%" NEQ "0" Echo.iso build abandoned during validation.

mv "%esd_Genuine_File_Path%\%esd_File%" "%esd_File_Path%"

Dism /Apply-Image /ImageFile:"%esd_File_Path%\%esd_File%" /Index:1 /ApplyDir:"%Windows_Files_Path%" /Verify /CheckIntegrity

Dism /Export-image /SourceImageFile:"%esd_File_Path%\%esd_File%" /SourceIndex:2 /DestinationImageFile:"%Windows_Files_Path%\sources\boot.wim" /Compress:max /Bootable /CheckIntegrity

Dism /Export-image /SourceImageFile:"%esd_File_Path%\%esd_File%" /SourceIndex:3 /DestinationImageFile:"%Windows_Files_Path%\sources\boot.wim" /Compress:max /Bootable /CheckIntegrity

Dism /Export-image /SourceImageFile:"%esd_File_Path%\%esd_File%" /SourceIndex:4 /DestinationImageFile:"%Windows_Files_Path%\sources\install.esd" /Compress:Recovery /CheckIntegrity

del "%esd_File_Path%\%esd_File%"

oscdimg -o -u2 -udfver102 -l"%iso_Label%" -t%Creation_Date% -bootdata:2#p0,e,b"%Windows_Files_Path%\boot\etfsboot.com"#pEF,e,b"%Windows_Files_Path%\efi\microsoft\boot\efisys.bin" "%Windows_Files_Path%" "%iso_Path%\%iso_file%"

pushd "%Windows_Files_Path%" && ( rd /S /Q "%Windows_Files_Path%" 2>nul & popd )

rd "%Windows_Files_Path%"

goto :eof

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


but I have some problems:

1) Even if I put the esd file in the %esd_Genuine_File_Path% directory, the %esd_File% variable is always empty. Why? It is possible insert it directly, logically controlling its validity of its name and its existence?

2) Since the iso file is created by the script, how do I ensure that the script does not verify its existence? In other words, the script should only check the validity of its name with its extension.

3) How do I create the %iso_Label% and %Creation_Date% procedures to verify the validity of the volume label of the iso file and the creation date of its files?

Thanks

Bye

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

Re: Bat script with some problems (explanation)

#2 Post by balubeto » 30 Jul 2016 04:04

Some might help correct and complete this script?

Thanks

Bye

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

Re: Bat script with some problems (explanation)

#3 Post by aGerman » 30 Jul 2016 05:56

balubeto

This is a support forum. Its main objective is to support questioners for problems that they have with their own codes. Of course the volunteers may also write a whole code in case the requirements are clearly defined by the questioner. But also in that case it's critical if the questioner didn't understand the code and mindlessly copy/paste it without even to try to understand what's going on.

Some people already tried to help out. They already asked you for specific informations that you didn't provide yet. That way you will ask the same question a hundred years long without getting the answer you're after.
I'm on my wits end. But I don't want to lock the thread. Maybe somebody wants to fight your problem and - who knows - maybe will find the solution by a fluke.

Regards
aGerman

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Bat script with some problems (explanation)

#4 Post by ShadowThief » 30 Jul 2016 10:47

Your questions are incoherent. What is this "validity" you keep babbling on about? Nothing you are asking makes any sense.

http://ell.stackexchange.com/

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

Re: Bat script with some problems (explanation)

#5 Post by penpen » 30 Jul 2016 18:58

I reread all your posts and hope very much, that 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" "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
Tested on Win 10 (only a little bit: may still contain errors).

Important:
If the above is not what you need, then you should tell us which is your native tongue:
Maybe someone could tell you in your own language what information we need from you (in that case only).
Maybe this also shows, why we need more information; also available in other languages.


penpen

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

Re: Bat script with some problems (explanation)

#6 Post by balubeto » 01 Aug 2016 02:45

penpen wrote:I reread all your posts and hope very much, that 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" "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
Tested on Win 10 (only a little bit: may still contain errors).

Important:
If the above is not what you need, then you should tell us which is your native tongue:
Maybe someone could tell you in your own language what information we need from you (in that case only).
Maybe this also shows, why we need more information; also available in other languages.


penpen


At this moment I can not verify the script but I see that there are no routines on the verification of the validity of the volume label and of the file creation date included on the iso image. Someone could help me do these verification routines as indicated in the first post?

Thanks

Bye

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

Re: Bat script with some problems (explanation)

#7 Post by penpen » 02 Aug 2016 06:30

balubeto wrote:but I see that there are no routines on the verification of the validity of the volume label
The validity of the volume label is checked here:

Code: Select all

   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!"
   )


balubeto wrote:file creation date included on the iso image.
You didn't say how you define a "valid date".


penpen

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

Re: Bat script with some problems (explanation)

#8 Post by balubeto » 03 Aug 2016 09:21

penpen wrote:
balubeto wrote:but I see that there are no routines on the verification of the validity of the volume label
The validity of the volume label is checked here:

Code: Select all

   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!"
   )


balubeto wrote:file creation date included on the iso image.
You didn't say how you define a "valid date".


penpen


Basically, I'd like that the user also enter the iso name, the iso volume label and file date stamp that will be included in the iso image so that the oscdimg command to work properly (see the script of my first post).

So, how do I do this?

Thanks

Bye

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

Re: Bat script with some problems (explanation)

#9 Post by aGerman » 03 Aug 2016 09:41

belubeto

penpen asked you to define what a valid date is. The date format is language dependent. E.g.
Wed 08/03/2016
03.08.2016
2016-08-03
could all mean the same and they are all valid!

Furthermore - How shall the time stamp look like that you want to append to your iso name? Something like 20160803?

If you are not able to explain your requirements then at least provide an example as I did above.

Regards
aGerman

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

Re: Bat script with some problems (explanation)

#10 Post by balubeto » 03 Aug 2016 10:30

aGerman wrote:belubeto

penpen asked you to define what a valid date is. The date format is language dependent. E.g.
Wed 08/03/2016
03.08.2016
2016-08-03
could all mean the same and they are all valid!

Furthermore - How shall the time stamp look like that you want to append to your iso name? Something like 20160803?

If you are not able to explain your requirements then at least provide an example as I did above.

Regards
aGerman


Since I use the Oscdimg command https://msdn.microsoft.com/en-us/en-en/ ... 24847.aspx , the three variables, that I asked, must be compatible with the same command in the script of my first post.

Thanks

Bye

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

Re: Bat script with some problems (explanation)

#11 Post by aGerman » 03 Aug 2016 10:53

balubeto wrote:Since I use the Oscdimg command https://msdn.microsoft.com/en-us/en-en/ ... 24847.aspx , the three variables, that I asked, must be compatible with the same command in the script of my first post.

Thanks

Bye


You neither explained your requirement nor did you provide an example.
Ignorance leads to locking of the topic and longer periods of being unable to post on the forum.

Regards
aGerman

*** locked ***

Locked