Check the validity of the directories and files names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Check the validity of the directories and files names

#1 Post by balubeto » 06 Jul 2016 12:02

Hi

I have this script

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

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

echo(
echo Result:
echo Windows_Files_Path: "%Windows_Files_Path%"
echo iso_Path          : "%iso_Path%"
echo esd_File_Path     : "%esd_File_Path%"
echo esd_File          : "%esd_File%"

:: put your code here

endlocal
goto :eof


:setValidPath
:: %~1   variable to set
:: %~2   default value
:: %~3   text if default value is invalid
   setlocal
   set "input=%~2"
   set "text=%~3"
   set "first=1"

:validatePath
   set "invalid="
   :: validating
   :: example code for invalidating input if the given path does not exist
   if not exist "%input%" set "invalid=The Path you specified for %~1 ^("%input%"^) does not exist."

   :: insert code for additional validity checking

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



:setValidFile
:: %~1   variable to set
:: %~2   default value
:: %~3   text if default value is invalid
   setlocal
   set "input=%~2"
   set "text=%~3"
   set "first=1"

:validateFile
   set "invalid="
   :: validating
   :: example code for invalidating input if the given filename is empty string
   if "" == "%input%" set "invalid=The Filename you specified for %~1 is the empty string."

   :: insert code for additional validity checking

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


that should check the validity of the directories (with absolute paths) and files names in Windows 7 and above and save these elements in the respective variables.

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

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

So, how can I modify this script?

Thanks

Bye
Last edited by balubeto on 08 Jul 2016 02:05, edited 1 time in total.

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

Re: Validity of the directories and files names

#2 Post by balubeto » 07 Jul 2016 11:38

Sorry again but does anyone know how to solve my problem?

Thanks

Bye

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

Re: Validity of the directories and files names

#3 Post by foxidrive » 08 Jul 2016 01:59

The script is rather different here: http://forum.html.it/forum/showthread.p ... id=2940940

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

Re: Validity of the directories and files names

#4 Post by balubeto » 08 Jul 2016 02:12

foxidrive wrote:The script is rather different here: http://forum.html.it/forum/showthread.p ... id=2940940


Because it was an old version.

Thanks

Bye


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

Re: Check the validity of the directories and files names

#6 Post by foxidrive » 08 Jul 2016 18:54

Some people don't realise that describing what the task entails, and how it is being used, is part of the knowledge that is needed to help them.


balubeto is showing code that has variables but without any information about what will be in those variables.
The requirement is to get a fully qualified path - but where is that path supposed to come from?

Dave recently commented in another thread that it's useful to step back from your question and read it as though you don't know anything about the task.

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

Re: Check the validity of the directories and files names

#7 Post by aGerman » 09 Jul 2016 04:37

I agree. As long as the requirements are not defined crystal ball gazing doesn't make sense.

Some thoughts:
Apart from invalid characters as /:*?"<>| for folder/file names ...
1) You wrote something about absolute pathes. Thus, "A\B.ext" wouldn't be valid, not even if folder "A" already exists in the current directory?
2) Assuming "X:\A\B.ext". Is the path valid if X: is no existing drive?
3) Assuming "X:\A\B.ext". Is the path valid if A is no existing folder?
4) Assuming "\\server\share\B.ext" where "\\server\share" is existing. Is the path valid?
5) Assuming "X:\A\B". Is the path valid even if B has no file extension? If not, what extension would be valid?
6) Assuming "C:\Windows\B.ext". Is the path valid even if you don't have writing permissions without elevated rights or if the ACL doesn't allow it?

What else is valid or invalid for your own definition of validity?

Regards
aGerman

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

Re: Check the validity of the directories and files names

#8 Post by penpen » 09 Jul 2016 04:51

Sorry, i hadn't much time the last days.

I only can say repeat, what i've written before:
http://www.dostips.com/forum/viewtopic.php?p=47575#p47575
In short: Replace the lines containing

Code: Select all

   :: insert code for additional validity checking
with the code that verifies the validity.

balubeto wrote:Sorry but I still do not understand how to modify this script so that it can verify the validity of the full paths of the directories and the validity of directories or files names regardless of their existence so that it can also save the valid directories and files that do not exist in their respective variables.
As i told you, in common there exist only one way (i know) to check the full validity of a path: Create it, and test if it is there.

If you don't want to do it, then you need to forbid the usage of some filenames, so you should define "validity of the full paths of the directories" and "validity of directories or files names" (for example only allowed characters are 'A'-'Z', 'a'-'z', '0'-'9', '.', '_' which is allowed in most filenames, only allowed format is 8.3), and restrict your hardware environment use case to for example:
- linux server network access over samba server, or
- own pc only using FAT12 or
- own pc using NTFS, or
- own PC using network share to win 98, or
- network share on NAT (using interface XY to set the names) using EncFS/ZFS/EAFS/..., or
- ...

Also you have to define what is an "absolute path", and a relative path" because it depends on the level of the used win API how relative or absolute pathes are defined (also mentioned my above linked post).


One cannot help you until you have described what you want to do.


penpen

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

Re: Check the validity of the directories and files names

#9 Post by balubeto » 10 Jul 2016 01:27

This script should work only with existing local NTFS partitions that support long names.

If the file has no extension, the path X:\A\B should be considered the absolute path of a directory.

If X:\A\B.ext is an existing directory, the script should treat it as a directory.

If a user does not have the write or access permission to a directory, he should be warned of this and the script should repeat its request.

Therefore, someone would be able to modify the initial script?

Thanks

Bye
Last edited by balubeto on 10 Jul 2016 10:34, edited 1 time in total.

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

Re: Check the validity of the directories and files names

#10 Post by aGerman » 10 Jul 2016 06:14

It's pretty annoying to ask you several times for each single detail. Keep in mind that you are the only source for informations that potential helpers have. If you're not able to answer questions properly I will lock this thread too.

You missed question 3. Shall the script only consider already existing folders as valid or are non-existing folders of the entered path generated and thus valid as well?

Regards
aGerman

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

Re: Check the validity of the directories and files names

#11 Post by balubeto » 10 Jul 2016 10:59

aGerman wrote:It's pretty annoying to ask you several times for each single detail. Keep in mind that you are the only source for informations that potential helpers have. If you're not able to answer questions properly I will lock this thread too.

You missed question 3. Shall the script only consider already existing folders as valid or are non-existing folders of the entered path generated and thus valid as well?

Regards
aGerman


I did not understand clearly this question.

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

This was that you wanted to know?

Thanks

Bye

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

Re: Check the validity of the directories and files names

#12 Post by aGerman » 10 Jul 2016 15:07

Try if that performs the kind of validation that you want. If so, include it into penpen's script.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

:loop
set "mypath="
set /p "mypath=Enter an absolute path: "
call :validate mypath
if errorlevel 1 goto loop

echo Valid path: "%mypath%"
pause
exit /b


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

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

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

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

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

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

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

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

Regards
aGerman

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

Re: Check the validity of the directories and files names

#13 Post by foxidrive » 10 Jul 2016 18:24

balubeto has a mother tongue that is not English and it occurred to me that the character set being used in his files and folders may not be plain latin characters.

edited comment.

I removed the part that mentioned that non-latin characters can break batch code, which aGerman kindly filled in below.

See here balubeto: viewtopic.php?f=3&t=6108

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

Re: Check the validity of the directories and files names

#14 Post by aGerman » 11 Jul 2016 11:51

German language has some funny characters too (ÄÖÜäöüß) which may cause problems. If you enter them using SET /P you won't run into trouble. But if you save them in a Batch script - it depends. Most of the text editors save the code with the current ANSI Code Page (most likely Windows-1252 for balubeto too). In that case you either have to change the Code Page used in the text editor (if even possible) or you have to change the Code Page of the console window programmatically using CHCP.

a.bat

Code: Select all

@echo off &setlocal

set "p=D:\äöüß"
call "b.bat" "%p%"

for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
for /f "tokens=3" %%i in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "ACP"') do set /a acp=%%i
>nul chcp %acp%
set "p=D:\äöüß"
>nul chcp %oemcp%
call "b.bat" "%p%"

pause


b.bat

Code: Select all

@echo off

set "arg=%~1"
echo "%arg%"



Running a.bat displays

Code: Select all

"D:\õ÷³▀"
"D:\äöüß"
Drücken Sie eine beliebige Taste . . .

An implicite conversion will be performed if the string literal was read with the ANSI Code Page that the script was saved.

Regards
aGerman

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

Re: Check the validity of the directories and files names

#15 Post by foxidrive » 11 Jul 2016 12:21

aGerman wrote:German language has some funny characters too (ÄÖÜäöüß) which may cause problems.


Thanks for explaining that aGerman.

I don't think I've mentioned here that my family is of German extraction. I'm first generation Aussie but my parents were German people. My Mother was born down South near the Oder river in a town called Dammfeld.

I'm lousy at any language other than English though I studied German in school - for three weeks until the course was cancelled. :)

German wasn't actually spoken much at home due to living in a different country and there was a need to communicate with the locals for a single Mother. Just some rare visits from German friends and the German music on records.

Locked