Searching for redirection chars with findstr

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Searching for redirection chars with findstr

#1 Post by MrKnowItAllxx » 22 Jun 2012 17:53

Here is problematic code:

Code: Select all

for /f "tokens=* eol=/ delims=" %%a in (format.txt) do (
   echo %%a|findstr /r "[/\:*?^"^<^>^|]" > nul
   if errorlevel 1 (
      set "format=%%a"
   ) else (
      cls
      echo Your format contains illegal characters!
      echo Rename not completed.
      pause
      exit
   )
)


example of format.txt:

Code: Select all

/ Formating tokens = [name],[ext],[mm],[dd],[yy],[yyyy],[hour],[min]
/ Illegal Characters: / \ : * ? " < > |
/ Format:
[name] [[mm]>[dd]].txt


I am trying to error check in format.txt to see if the "format" contains illegal characters and to alert the user. Problem is when escape characters are encountered, I think the "echo %%a" line is exiting on error / creating files with strange names. How could I fix this? or is there another way to do this?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Searching for redirection chars with findstr

#2 Post by dbenham » 22 Jun 2012 21:59

You have got some nasty tricky issues to deal with.

Problem 1: \ and " must be escaped as \\ and \" for FINDSTR. But special characters must be either escaped or quoted for the CMD.EXE parser. You enclose your search string in quotes, so the special characters are OK, except you want to include a quote in your search string. That creates unpaired quotes and everything becomes more complicated. The simple solution is to use two escaped quotes \"\" in your character set expression. It keeps the quotes paired, and doesn't do any harm to have the same character appear twice in the set. Now all the special characters are quoted from the perspective of CMD.EXE, so you don't need to escape them.

Problem 2: You are attempting to pass unescaped special characters through a pipe. The solution for this is very ugly. You can't execute the command directly. Instead you store the line in a variable and execute the command via CMD with /V:ON option to enable delayed expansion. It is critical that delayed expansion be disabled in your parent batch so that the value is not expanded until it gets inside the new CMD session.

Code: Select all

for /f "eol=/ delims=" %%a in (format.txt) do (
   set "test=%%a"
   cmd /v:on /c echo !test!|findstr /r "[/\\:*?\"\"<>|]" >nul
   if errorlevel 1 (
      set "format=%%a"
   ) else (
      cls
      echo Your format contains illegal characters!
      echo Rename not completed.
      pause
      exit
   )
)

The above works, but it is very slow.

I'm not sure what your requirements are. But the following might help you develop a faster and simpler solution:

This code will only match lines that are valid format that don't begin with /

Code: Select all

findstr /r "^[^/\\:*?\"\"<>|]*$" format.txt

This code will only match lines that don't begin with / that contain illegal characters

Code: Select all

findstr /r "^[\\:*?\"\"<>|] ^[^/].*[/\\:*?\"\"<>|]" format.txt

When using findstr, you can use the following syntax to check the results. I much prefer this over checking ERRORLEVEL.

Code: Select all

findstr "search" file >nul && (echo match found) || (echo no match found)



Dave Benham

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Searching for redirection chars with findstr

#3 Post by MrKnowItAllxx » 22 Jun 2012 22:54

Thank you very much for your response, I hadn't known very much about findstr but now I have a much better understanding after deciphering your alternative solutions. :) Also thanks for the errorlevel alternative

I found this solution suited my needs perfectly:

Code: Select all

findstr /r "^[^/\\:*?\"\"<>|]*$" format.txt >nul || (
echo Your format contains illegal characters!
echo Rename not completed.
pause
exit
)
for /f "eol=/ delims=" %%a in (format.txt) do set "format=%%a"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Searching for redirection chars with findstr

#4 Post by dbenham » 23 Jun 2012 08:05

Are you sure that works :?: :?

That code will allow a line with illegal characters to slip through as long as there is at least one line that is valid.

The following will verify that all lines are valid. In other words it aborts if there is at least one illegal line:

Code: Select all

findstr /r "^[\\:*?\"\"<>|] ^[^/].*[/\\:*?\"\"<>|]" format.txt >nul && (
  echo Your format contains illegal characters!
  echo Rename not completed.
  pause
  exit
)
for /f "eol=/ delims=" %%a in (format.txt) do set "format=%%a"

Note that there are two search strings (FINDSTR splits the string at spaces). The first string looks for an illegal character other than / in the first position. The second string looks for an illegal character anywhere after the 1st position when the 1st position is not /.


Dave Benham

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Searching for redirection chars with findstr

#5 Post by MrKnowItAllxx » 23 Jun 2012 08:53

:? Strange.. the original solution seemed to work, but maybe I'm crazy. I switched over to this new solution.

My original requirements were simply that the line did not begin with a '/', and the line did not contain 'illegal characters'.

Thanks again :)

Post Reply