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
