Help with batch!?!

May 15, 2011 at 18:25:56
Specs: Windows XP
I want the user to type somthing using:

echo Type something:
set /p something=""

And I want a code to remove all spaces. So if I typed:

Hi people! :)

it should be changed to:

Hipeople!:)

I then want a code to put a space every 3 charictors, so it'll then be:

Hip eop le! :)

And finally I want it to set the groups of three to different variables so:

1=Hip
2=eop
3=le!
4=:)

Is there any way to do that?


also it would be helpful if you had the answer to these other questions:

How can I filter out every thing except numbers so This:
what5eve ': 123;65
will be:
512365

How can I read a text file and save everything in it to 1 variable?

Is it possible to show the date and time in these formats:
Sunday, May 15, 2011
and:
9:12 PM

How can I allow someone to type 1 charector and then atomaticly hit enter, not allowing them to put a second charector?

And finnaly;

When I type;
echo %var1%%var2%%var3%
it displays;
Echo if off.

But when I type;
echo %var1% %var2% %var3%
it still displays;
Echo is off.

And whatever else I try I can't get it to echo var1, var2, and var3 in order (I need to make sure there are no spaces). So how do I get them like that?

Thanks for taking the time to read this!
Please tell me if you have the answer to any of these questions.
Thanks,
bbman225

FYI I'm using Windows 7


See More: Help with batch!?!

Report •


#1
May 28, 2011 at 01:27:39
ineteresting.. Commenting just to receive notifications on this post..

Subhash Chandra.
http://www.wintechgeek.com


Report •

#2
May 28, 2011 at 15:20:07
Here's a start, more to follow when time permits. If you are trying to learn batch scripting I recommend the on-line tutorials by Rob Van Der Woude (with whom I have not affiliation).

Untested, leave you to improve on the script:

@echo off
cls
setlocal 

:: User entry....

set /p Something=Type something:
cls

:: Remove spaces from Something

set Something1=%something: =%
Echo.&echo.Environment Variable Something now=%Something1%
pause > nul


:: Insert space every three chars

set something=%something1%~
set pos=0
set space= 
:start
call set chr=%%Something:~%pos%,1%%
     if "%chr%"=="~" goto finis
     set /a this="%pos%" %% 3
     if %this% equ 0 set Mainvar=%Mainvar%%space%
     set Mainvar=%Mainvar%%chr%
     set /a pos+=1
     goto start

:finis
set mainvar=%mainvar:~1%
echo.&echo.Environment Variable Something now=%Mainvar%
pause > nul


:: Create Environmnt Variables
:: Note that this section is written to use the input Hi People! :)
:: It may or may not be successful for a string which is shorter/longer

for /f "tokens=1-3*" %%1 in ("%Mainvar%") do (
    set var1=%%1&set var2=%%2&set var3=%%3&set var4=%%4%
)

echo.&echo.Var1=%var1%  Var2=%var2%  Var3=%var3%  var4=%var4%



Please come back & tell us if your problem is resolved.


Report •

#3
May 29, 2011 at 02:49:48
Final submission - leave you to test on Win 7.

@echo off
cls
setlocal enabledelayedexpansion 

:: Extract digits from string

set a=WHAT5EVE': 123;6             &:: Original string value
set a=%a%~                         &:: Add control char 

set pos=0
:start
call set chr=%%a:~%pos%,1%%
     if "%chr%"=="~" goto finis
     if "%chr%" lss "0" set /a pos+=1& goto start
     if "%chr%" gtr "9" set /a pos+=1& goto start
     set numbers=%numbers%%chr%
     set /a pos+=1
     goto start

:finis
echo Numbers extracted=%numbers%
echo ***
pause > nul
cls

:: Set textfile lines to variable

for /f "tokens=*" %%1 in (textfile.txt) do (
    rem set var=%%1
    set othervar=!othervar!%%1 
)

echo.&echo.All text lines in variable=%othervar%
echo ***
pause > nul
cls

echo.&echo.Question: "Is it possible to show the date and time in these formats:
echo Sunday, May 15, 2011
echo and:
echo 9:12 PM"

echo.&echo.Reply: "Probably but it much easier to set the date and time formats in
echo Control Panel^>Regional and Language Options".
echo ***
pause > nul
cls

echo.&echo.Question: "How can I allow someone to type 1 charector and then
echo atomaticly hit enter, not allowing them to put a second charector?"

echo.&echo.Reply: "Try the Choice command"
echo ***
pause > nul
cls

echo.&echo.The command line "echo %%var1%%" will return "Echo is off" if the
echo Environment Variable var1 is not defined (Set).
echo ***
pause > nul
cls

echo.&echo.Hope this helps.


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
June 1, 2011 at 19:24:04
Tanks for your help!

When I run the file it displays:

Numbers extracted=51236
***

then

All text lines in variable=textfile.txt
***

What dose that mean?

Also I don't know how to use the choice command.


Report •

Ask Question