JREPL: Problem with special characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vin97
Posts: 5
Joined: 17 Apr 2020 08:30

JREPL: Problem with special characters

#1 Post by vin97 » 22 Apr 2020 10:56

I want to use JREPL to replace all special characters (% ^ & ' ` ; = ( ) !) in a user-defined filename with their appropriate cmd-escape-sequence in order to allow for correct handling later on in the batch script.
The following characters are sadly returning false or no outputs: ( ) % ^ !

Are there any tricks around this or is this a limitation of JREPL/batch?
Am I simply missing some escape sequences for the JREPL inputs? I use \c for ^ but it's not working properly either. I am using the /S option to read directly from the user-defined variable.

Hackoo
Posts: 85
Joined: 15 Apr 2014 17:59

Re: JREPL: Problem with special characters

#2 Post by Hackoo » 22 Apr 2020 23:34

Hi :wink:
I don't know if this batch can help you in your case ?

Code: Select all

@echo off
:Check
Cls & Color 0A
Title How to verify if a variable contains a valid filename in Windows Batch
echo(
Set "my_filename="
Echo Enter filename for this project
set /p "my_filename="
echo(
echo Before Removing the special char the filename is like this : "%my_filename%"
echo(
Call :Remove_Special_Char "%my_filename%" NewFileName
echo After Removing the special char the filename becomes like this : "%NewFileName%"
echo(
pause & Goto Check
::-----------------------------------------------------------------------------------
:Remove_Special_Char <String> <Variable to Set>
(
    echo WScript.StdOut.WriteLine Search_Replace(Data^)
    echo Function Search_Replace(Data^)
    echo Dim strPattern, strReplace, strResult,oRegExp
    echo Data = wscript.Arguments(0^) 
    echo strPattern = "[\\\/:*?\x22<>|()%&^^\x27!=;`]"
    echo strReplace = ""
    echo Set oRegExp = New RegExp
    echo oRegExp.Global = True 
    echo oRegExp.IgnoreCase = True 
    echo oRegExp.Pattern = strPattern
    echo strResult = oRegExp.Replace(Data,strReplace^)
    echo Search_Replace = strResult
    echo End Function
)>"%tmp%\%~n0.vbs"
@For /f "delims=" %%i in ('cscript //nologo "%tmp%\%~n0.vbs" "%~1"') do ( Set "%2=%%i" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------

vin97
Posts: 5
Joined: 17 Apr 2020 08:30

Re: JREPL: Problem with special characters

#3 Post by vin97 » 26 Apr 2020 05:57

The script crashes for some combinations of | " and < >.
That can be solved by calling a separate CMD instance and checking for the creation of a check-file in the original instance.
But even then I would like to allow for &, ! % and ^ since those are valid characters for Windows filenames.

Post Reply