Clean char / and & in a string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Clean char / and & in a string

#1 Post by darioit » 19 Aug 2014 01:03

Hello everybody,
I find a new issue that need your help, here my question:

I read a string formed by letters and symbol like / and &

This string is fixed long 50 chars

My goal is to clean "/" and "&" before use this string to create a new folders

This is input file:

Code: Select all

Best Gadget inc./                       0123456789
Sanford & son C. SAS                    0123456789


This is 1st script

Code: Select all

b.bat input.txt


and this is a b.bat script

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (%1) do set "RECORD=%%a" &call :Proc %1

:Proc

set "ANAGR=!RECORD:~0,40!"
echo "original:"%ANAGR%

rem clean / chars
set ANAGR=%ANAGR:/ =%
echo "Clean 1  :"%ANAGR%

rem clean & chars
set ANAGR=%ANAGR:& =%
echo "Clean 2  :"%ANAGR%

mkdir "%ANAGR%"

pause
goto:eof


and this is a result

Code: Select all

C:\>b.bat input.txt
"original :"Best Gadget inc./
"Clean 1  :"Best Gadget inc.
"Clean 2  :"Best Gadget inc.
Press any key to continue . . .
"original :"Sanford
'son' is not recognized as an internal or external command,
operable program or batch file.
'son' is not recognized as an internal or external command,
operable program or batch file.
"Clean 1  :"Sanford
"Clean 2  :"Sanford
Press any key to continue . . .
"original :"Sanford
'son' is not recognized as an internal or external command,
operable program or batch file.
'son' is not recognized as an internal or external command,
operable program or batch file.
"Clean 1  :"Sanford
"Clean 2  :"Sanford
A subdirectory or file Sanford  already exists.
Press any key to continue . . .


Slash "/" is clean correct but with "&" I get some errors, what's wrong?

Thanks and Best Regards

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Clean char / and & in a string

#2 Post by jeb » 19 Aug 2014 01:18

Hi dariot,

you enabled the delayed expansion, but you don't use it, this would solve your problems.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (%1) do set "RECORD=%%a" &call :Proc
EXIT /B

:Proc

set "ANAGR=!RECORD:~0,40!"
echo "original:"!ANAGR!

rem clean / chars
set "ANAGR=!ANAGR:/ =!"
echo "Clean 1  :"%ANAGR%

rem clean & chars
set "ANAGR=!ANAGR:& =!"
echo "Clean 2  :"!ANAGR!

mkdir "!ANAGR!"

pause
goto:eof

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Clean char / and & in a string

#3 Post by darioit » 19 Aug 2014 01:57

oh thanks I use "!" in all variable of this batch, but here I get a template for this site and I forget to change % in ! lol

Post Reply