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.
JREPL: Problem with special characters
Moderator: DosItHelp
Re: JREPL: Problem with special characters
Hi
I don't know if this batch can help you in your case ?
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
::----------------------------------------------------------------------------------
Re: JREPL: Problem with special characters
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.
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.