How to remove the : and * special characters from strings?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mwt1974
Posts: 1
Joined: 21 Nov 2011 06:17

How to remove the : and * special characters from strings?

#1 Post by mwt1974 » 21 Nov 2011 07:04

Hi.

I have been tasked with creating a simple batch file to pass URLs read from text file to a screen grabbing program and use the URL for the resulting jpg file name. The batch script will be required to run on XP, Vista and Windows 7.

The script uses a FOR loop and can replace all the special characters using string replacement except the : and * from the test URL. I also seem to be limited to 228 character file names instead of the maximum 255 before webscreencapture.exe falls over.

I would be most great full for any ideas on how I can replace the : and * characters. My code is listed below. The made up test url = "http://www.google.co.uk/search?source=ig&hl=en&rlz=&=&q=car+|+*+:+"+?+<+>+"

@echo off
(SET str=)
(SET BatchID=)
CLS
echo.
echo.

:: SET /P prompts for input and sets the variable
:: to whatever the user types

SET /P BatchID=Please type the Batch ID and press Enter:

MKDIR C:\Compliance_checks\%BatchID%

:: For loop won't run correctly without EnableDelayedExpansion

Setlocal EnableDelayedExpansion

:: strip out special characters and replace with underscore and
:: pass URL and jpg file name to webscreencapture.exe

for /f %%i in (C:\Compliance_checks\url_lst.txt) do (

SET str=%%i
SET str=!str:http://=! Trim http:// from start of string
SET str=!str:~0,228! Truncate URL string to 228 characters long

The next 4 special characters can be replaced without the need to prefix the ^ escape character

SET str=!str:/=_!
SET str=!str:\=_!
SET str=!str:?=_!
SET str=!str:"=_!

The next 3 special characters require the ^ escape character to be prefixed to be replaced in the string.

SET str=!str:^<=_!
SET str=!str:^>=_!
SET str=!str:^|=_!

The next 2 special characters can not be replaced even with the ^ escape character prefix.
SET str=!str:^:=_!
SET str=!str:^*=_!

C:\Compliance_checks\webscreencapture.exe %%i C:\Compliance_checks\%BatchID%\!str!.jpg
)
ENDLOCAL :?:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to remove the : and * special characters from strin

#2 Post by Ed Dyreen » 21 Nov 2011 07:51

'
From an older library:

Code: Select all

::    -The '*' symbol must be noted as '^*' otherwise;
::    ;. for won't detect it as an argument !
::    ;. Can be avoided by loopin however we won't as this is too much of a performance hit !

set "star=!star:^*=#____CMD_ESC_SYM[Star____]____#!"
As the library documentation indicates, it is possible to process the '*' but it requires looping over the input arguments !
I believe it's better to avoid, you are about to waste valuable CPU cycles.

This will help: How to replace "=","*", ":" in a variable :arrow:
http://www.dostips.com/forum/viewtopic.php?f=3&t=1485

Post Reply