Page 1 of 1

Clean char / and & in a string

Posted: 19 Aug 2014 01:03
by darioit
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

Re: Clean char / and & in a string

Posted: 19 Aug 2014 01:18
by jeb
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

Re: Clean char / and & in a string

Posted: 19 Aug 2014 01:57
by darioit
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