You are not logged in.

#1 26 Oct 2007 08:57

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Omiting invalid directory path characters...

This is what I came up with without using any external application to remove the invalid directory path characters. If you have a better alternative, please post it! The code below is well commented:

@ECHO OFF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::                                                                              ::
:: omitchars.cmd                                                                ::
:: -------------                                                                ::
:: Prompts User to enter a string with invalid directory path characters:       ::
:: ( ", ?, |, *, < and > )                                                      ::
::                                                                              ::
:: Then the batch removes the invalid characters from the string inputed by the ::
:: User and prints the remaining string onto the screen including the           ::
:: remaining string character count.                                            ::
::                                                                              ::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Variables Used

set _charsLeft=
set _cntAstr=
set _cntChar=
set _cntChar_usrin=
set _tmpChar=
set _usrin=

:: Prompt User to Enter String

echo.
echo. Enter a string with invalid directory path characters
echo.
set /P _usrin=String:
echo.

:: Check User Input, quit batch if no User input

if NOT DEFINED _usrin goto END

:: Remove invalid directory path characters from string

:: Assigning a double quote ( " ) in the front of the string value in the
:: the variable after removing the original "s in the string will keep the
:: remaining valid and invalid characters in the string value so the rest of
:: the invalid characters can be removed from the string. This will keep the
:: variable containing a value until the invalid character check and removal
:: is finished.
::
set _usrin="%_usrin:"=%

set _usrin=%_usrin:|=%
set _usrin=%_usrin:>=%
set _usrin=%_usrin:<=%
set _usrin=%_usrin:?=%

:: The Asterick( * ) cannot be removed by subparsing the string.
:: Instead, it will be required to get the count of characters in the
:: string to find the astericks.

if NOT "%_usrin:~1%"=="" call :GET_CHAR_COUNT

:: Print results of omiting invalid path chars
::
:: If there are any remaing characters in the string then display the string value

if NOT DEFINED _usrin    echo. No string value after eliminating invalid characters

if DEFINED _usrin    echo. Results:
if DEFINED _usrin    echo.         "%_usrin:~1%"
if DEFINED _usrin    echo.         %_cntChar_usrin% character(s) in the string...

goto END

::%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%::
:: SUB-ROUTINE FUNCTIONS
::%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%::

:: GET_CHAR_COUNT
:: --------------
:: Gets the the number of characters used in the string and also looks for
:: any asterick character. If one is found, it will be removed from the
:: string.
::
::%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%::
:GET_CHAR_COUNT

if NOT DEFINED _cntChar set /A _cntChar=1
if NOT DEFINED _cntAstr set /A _cntAstr=1

:: Since there's characters that are considered valid for a directory path
:: that cannot be assigned in a variable on their own such as the and ( & )
:: character, a " is required to keep these type of characters as a string
:: character value.
::
call set _tmpChar="%%_usrin:~%_cntChar%,1%%

if "%_tmpChar:~1%"=="" set _tmpChar=

if NOT DEFINED _tmpChar call :END_CHAR_COUNT
if NOT DEFINED _tmpChar goto :eof

:: Look for the asterick character in the string

if "%_tmpChar:~1%"=="*" call :OMIT_ASTERICK
set /A _cntChar+=1
call :GET_CHAR_COUNT

goto :eof
::%%%%%::

    :: OMIT_ASTERICK
    :: -------------
    :: Replaces and counts the astericks found in the string used in the
    :: GET_CHAR_COUNT sub-routin function with a question mark ( ? ). The ?s found
    :: later at the end of the character count will be removed from the
    :: string value.
    ::
    ::%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%::
    :OMIT_ASTERICK

    set /A _charsLeft=%_cntChar% + 1
    set /A _cntAstr+=1

    call set _usrin=%%_usrin:~0,%_cntChar%%%?%%_usrin:~%_charsLeft%%%

    goto :eof
    ::%%%%%::

    :: END_CHAR_COUNT
    :: --------------
    :: Force exits the GET_CHAR_COUNT function loop and subtracts the asterick
    :: character count found in the string to the final count of characters for
    :: the resulting string value.
    ::
    ::%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%::
    :END_CHAR_COUNT

    :: Remove all ? characters found in the string

    set _usrin=%_usrin:?=%

    :: Check to see if there is any remaining valid characters left in the string

    if NOT "%_usrin:~1%"=="" set /A _cntChar_usrin=%_cntChar% - %_cntAstr%
    if "%_usrin:~1%"=="" set _usrin=

    if DEFINED _charsLeft set _charsLeft=

    set _cntAstr=
    set _cntChar=

    goto :eof
    ::%%%%%::

::%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%::
:END
set _cntChar_usrin=
set _usrin=
 Enter a string with invalid directory path characters

String:C:\>"Just"\"<><A<n<*> Example? ||& ?!****L<oo>k!*<***\"

 Results:
         "C:\Just\An Example & !Look!\"
         28 character(s) in the string...

Offline

Board footer

Powered by