Checking input for special characters
Moderator: DosItHelp
Checking input for special characters
Could any one help on this.
Need to check if the entered argument has any special characters in it and throw an error/exit if it has any.
Need to check if the entered argument has any special characters in it and throw an error/exit if it has any.
Re: newbie to batch
Please define your task more clearly and provide examples.
Re: newbie to batch
I have reached till here...
The below script replaces the "old name" with "new name", I got a replace function "replace.bat" which does that. Now I need to make sure the second argument does not contain any special characters it it before replacing "old name" with "new name". Thanks.
"old name" --> first argument
"new name" --> second argument
The below script replaces the "old name" with "new name", I got a replace function "replace.bat" which does that. Now I need to make sure the second argument does not contain any special characters it it before replacing "old name" with "new name". Thanks.
"old name" --> first argument
"new name" --> second argument
Code: Select all
echo off &setlocal
set "search=%~1"
set "replace=%~2"
set "textfile=mc.ini"
set "newfile=Output.txt"
call replace.bat "%search%" "%replace%" L < "%textfile%" >"%newfile%"
del "%textfile%"
rename "%newfile%" "%textfile%"
Re: newbie to batch
Can you show us the contents of replace.bat. Please use CODE tags.
I am not understanding why you are redirecting another text file into it.
I am not understanding why you are redirecting another text file into it.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: newbie to batch
Exactly which symbols are you referring to when you say "special symbols?"
Re: newbie to batch
@Squashman Thats pretty long code, got it from https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
@ShadowThief I mean anything other than alphabets and numericals
thought of doing with 'sed' pretty cool command in linux. Got to know we can execute it in windows as well by installing sed utility, but my concern here is to find whether entered argument has any special characters.
Thanks to both of you.
@ShadowThief I mean anything other than alphabets and numericals
thought of doing with 'sed' pretty cool command in linux. Got to know we can execute it in windows as well by installing sed utility, but my concern here is to find whether entered argument has any special characters.
Thanks to both of you.
Re: newbie to batch
shetty wrote:@Squashman Thats pretty long code, got it from https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
@ShadowThief I mean anything other than alphabets and numericals
thought of doing with 'sed' pretty cool command in linux. Got to know we can execute it in windows as well by installing sed utility, but my concern here is to find whether entered argument has any special characters.
Thanks to both of you.
Well thanks for letting us know you are using an old version of Dave's Repl.bat. That version has been deprecated and the new version is JREPL.
You can most likely use JREPL to also check to make sure there are not any other non alpha or non numeric characters in your replacement strings before you use JREPL to do the replacement.
Re: newbie to batch
shetty wrote:@ShadowThief I mean anything other than alphabets and numericals
Are the alphabet characters purely English a-z ? No foreign characters?
Re: Checking input for special characters
@Squashman. Thank you for letting me know the new version. But as for me the older version seems more suitable since I have to search and replace the same string on multiple files. I can add on the required files on my main script and call repl to search and replace.
And I was not able to figure out.. , how to check if the second argument has special characters using JREPL. Any help appreciated.
@foxidrive yes the alphabet characters should be purely English or numerical.
And I was not able to figure out.. , how to check if the second argument has special characters using JREPL. Any help appreciated.
@foxidrive yes the alphabet characters should be purely English or numerical.
Re: Checking input for special characters
shetty wrote:@foxidrive yes the alphabet characters should be purely English or numerical.
Here's one method given the above:
Code: Select all
@echo off
set "replace=%~1"
set "string="
for /f "delims=" %%a in ('echo("%replace%"^|repl "[a-zA-Z0-9\x22]" "" x ') do set "string=%%a"
if defined string (echo illegal characters detected) else (echo all ok!)
pause
Re: Checking input for special characters
shetty wrote:@Squashman. Thank you for letting me know the new version. But as for me the older version seems more suitable since I have to search and replace the same string on multiple files. I can add on the required files on my main script and call repl to search and replace.
You can use whatever makes you feel warm and fuzzy but there is no limitation with jrepl that would keep you from using it.
Re: Checking input for special characters
@foxidrive
Thanks a lot, that works like a charm. But guess it doesn't like "
Code: Select all
@echo off
set "replace=%~1"
set "string="
for /f "delims=" %%a in ('echo("%replace%"^|repl "[a-zA-Z0-9\x22]" "" x ') do set "string=%%a"
if defined string (echo illegal characters detected) else (echo all ok!)
pause
Thanks a lot, that works like a charm. But guess it doesn't like "
Re: Checking input for special characters
shetty wrote:
Thanks a lot, that works like a charm. But guess it doesn't like "
Could you be more specific? I am not seeing any mismatched double quotes in the code.
Re: Checking input for special characters
My crystal ball shows unpaired quotation marks in %replace%
Regards
aGerman
Code: Select all
@echo off &setlocal
set "replace=;!$%%&<>|"/()=?*"
call :check && (
echo all OK
) || (
echo illegal characters detected
)
pause
exit /b
:check
setlocal EnableDelayedExpansion
echo !replace!
for /f delims^=1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ^"^ eol^= %%i in ("!replace!") do endlocal &exit /b1
endlocal &exit /b 0
Regards
aGerman
Re: Checking input for special characters
aGerman wrote:My crystal ball shows unpaired quotation marks in %replace%
I just got new glasses yesterday and I still can't see it.