Checking input for special characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
shetty
Posts: 5
Joined: 01 Apr 2015 05:55

Checking input for special characters

#1 Post by shetty » 01 Apr 2015 06:23

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: newbie to batch

#2 Post by Squashman » 01 Apr 2015 07:09

Please define your task more clearly and provide examples.

shetty
Posts: 5
Joined: 01 Apr 2015 05:55

Re: newbie to batch

#3 Post by shetty » 01 Apr 2015 08:46

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




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%"

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: newbie to batch

#4 Post by Squashman » 01 Apr 2015 09:27

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.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: newbie to batch

#5 Post by ShadowThief » 01 Apr 2015 09:31

Exactly which symbols are you referring to when you say "special symbols?"

shetty
Posts: 5
Joined: 01 Apr 2015 05:55

Re: newbie to batch

#6 Post by shetty » 01 Apr 2015 11:54

@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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: newbie to batch

#7 Post by Squashman » 01 Apr 2015 12:47

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.

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

Re: newbie to batch

#8 Post by foxidrive » 01 Apr 2015 19:32

shetty wrote:@ShadowThief I mean anything other than alphabets and numericals


Are the alphabet characters purely English a-z ? No foreign characters?

shetty
Posts: 5
Joined: 01 Apr 2015 05:55

Re: Checking input for special characters

#9 Post by shetty » 02 Apr 2015 05:22

@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.. :cry: , 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.

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

Re: Checking input for special characters

#10 Post by foxidrive » 02 Apr 2015 06:07

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Checking input for special characters

#11 Post by Squashman » 02 Apr 2015 06:29

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.

shetty
Posts: 5
Joined: 01 Apr 2015 05:55

Re: Checking input for special characters

#12 Post by shetty » 02 Apr 2015 10:34

@foxidrive

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 "

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Checking input for special characters

#13 Post by Squashman » 02 Apr 2015 10:40

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.

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

Re: Checking input for special characters

#14 Post by aGerman » 02 Apr 2015 12:53

My crystal ball shows unpaired quotation marks in %replace% :wink:

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Checking input for special characters

#15 Post by Squashman » 02 Apr 2015 13:12

aGerman wrote:My crystal ball shows unpaired quotation marks in %replace% :wink:

I just got new glasses yesterday and I still can't see it. :D

Post Reply