• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3740
  • Last Modified:

VBScript: Convert to lower case

Hi there,

I need a VBScript that will be called within a batch file as [cscript convert.vbs "BIG.is @s HUGE as_LARGE." >MyOutputFile.txt] that will convert all upercase caracters to lower case caracters.

I my example above, the expected output is: big.is @s huge as_large.

Thanks for your help,
Rene
0
ReneGe
Asked:
ReneGe
  • 7
  • 5
  • 4
  • +3
5 Solutions
 
rockiroadsCommented:
something like this perhaps

check for args, if 1 then convert to lowercase and output it



args = WScript.Arguments.Count

If args = 1 then
  arg = WScript.Arguments(0)
  wscript.echo lcase(arg)
end If



0
 
Om PrakashCommented:
Create the file called "convert.vbs" and add the following code
If (WScript.Arguments.Count > 0) Then
	dim filesys, filetxt, getname, path
	Set filesys = CreateObject("Scripting.FileSystemObject")
	Set filetxt = filesys.CreateTextFile("e:\somefile.txt", True)
	filetxt.WriteLine(lcase( WScript.Arguments(0)))
	filetxt.Close
End If

Open in new window

0
 
rockiroadsCommented:
cscript convert.vbs will produce a logo so pass in //nologo in order to remove it
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
baffy2000Commented:
Or you could use another batch file, instead of VBScript? The following might help:

@echo off
echo>%1
dir /b/l %1>lower.tmp
set /p result=<lower.tmp
echo %result%

Stick this in a batch file, say, "lower.bat", and just call it thus:

lower "BIG.is @s HUGE as_LARGE."

Note, this will not work with the following characters: \ / : * ? " < > |

But if that's ok, then this will probably be quicker than a VBScript.
0
 
baffy2000Commented:
Hmm. Just noticed that leaves some files lying around. Try this instead.

@echo off
echo>%1
dir /b/l %1>lower.tmp
set /p result=<lower.tmp
del lower.tmp
del %1
echo %result%
0
 
Daz_1234Commented:
Hi Rene,

The code below actually generates a small VBScript on the fly to do the conversion so you do not need to call a separate script from your batch file.  You can just insert the code below into your existing batch script and whenever you need to do a conversion, just use the command:

Call :ConvertLower "%INPSTR%"

... then the lower case equivalent will be stored in %LRESULT%

The code below is actually self contained so to test it you can just paste it all into a .cmd file.

Hope this helps.
Daz

(note: you don't say which OS you are using, I tested  this working on XP SP3)
Set INPSTR=BIG.is @s HUGE as_LARGE.

@Echo Input = %INPSTR%

Call :ConvertLower "%INPSTR%"

@Echo Output = %LRESULT%


Pause

Exit

:: Subroutine

:ConvertLower %1
    Set TS=%temp%\2Lwr.vbs
    Echo Set fso = CreateObject("Scripting.FileSystemObject") > %TS%
    Echo Set ts = fso.OpenTextFile("%temp%\lcase.txt", 2, True) >> %TS%
    Echo ts.WriteLine LCase("%~1") >> %TS%
    Echo ts.Close >> %TS%
    WScript //B "%TS%"
    Set /P LRESULT=<"%temp%\lcase.txt"
Goto :EOF

Open in new window

0
 
Bill PrewCommented:
If you want a way to do it right in a bat file you can get one here:

http://www.dostips.com/DtCodeCmdLib.php#Function.toLower

My adaptations of this that I use are in the attached code.

~bp
@echo off
set Test=ABCDEFGHIJ KLMnop
echo %Test%
call :UCase Test
echo %Test%
call :LCase Test
echo %Test%
exit /b
 
:UCase VariableName
  for %%I in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do call set "%1=%%%1:%%~I%%"
  exit /b
 
:LCase VariableName
  for %%I in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") do call set "%1=%%%1:%%~I%%"
  exit /b

Open in new window

0
 
Daz_1234Commented:
Hi Rene,

I recommend that you use Bill's version - it is much neater than my way, and you never leave the batch command script environment at all.

Regards,
Darren.



==> Bill,  Very nice.  
0
 
Bill PrewCommented:
==> Daz_1234

Thanks!

~bp
0
 
ReneGeAuthor Commented:
Wow, that was impressive guys.

==> Hey Darren
Worked without a sweat and was very educative. Thanks

==> Hey bp
Worked without a sweat and was very educative. Thanks
Also thaks for the ref.

==> rockiroads
Worked without a sweat and was very educative. Thanks

==> om_prakash_p
By running your script, I get a run time error (4,2): Disk not ready. Thanks

==> baffy2000
- I need to convert text to lower case, not file names. Also, your script did not work. However, I feel that I am going to learn something here
- I am puzzled about you command line [set /p result=<lower.tmp]. Would you mind explaining it to me?
-Assuming that variables are defined, would [dir /b /l %~1>%result%] accomplish what you intended to do? Or I did not understand it well?
-Thanks

--------------------------------------------------------------------------

I have more then i need, so I'll close this thread now. And attribute the points considering the level or pertinant contribution.

Feel free to comment further.


Thanks guys,

YOU ROCK !!!!!

Cheers,
Rene
0
 
ReneGeAuthor Commented:
You were all fantastic. I just hope I split the points fairly.

Cheers,
Rene
0
 
Bill PrewCommented:
Thanks Rene.

~bp
0
 
Daz_1234Commented:
You're welcome Rene, thank you.

0
 
ReneGeAuthor Commented:
==> bp,

Would you mind explaining [call set "%1=%%%1:%%~I%%"] in detail to me?

Thanks,
Rene
0
 
baffy2000Commented:
>==> baffy2000
>- I need to convert text to lower case, not file names. Also, your script did not work. However, I feel >that I am going to learn something here
>- I am puzzled about you command line [set /p result=<lower.tmp]. Would you mind explaining it to me?
>-Assuming that variables are defined, would [dir /b /l %~1>%result%] accomplish what you intended to >do? Or I did not understand it well?
>-Thanks

Since you ask, this does convert text to lower case, not file names. Not sure why it didn't work for you, since it works for me. Before I go any further, Bill's solution is better - it's more elegant, and works in all situations, whereas mine, as I explained, does not.

For completeness, my one works as follows, and is based on the fact that the "dir" command has a /L switch which outputs in lower case.

The explanation is as follows, assuming you issue the command,

lower HELLO

@echo off
:=====Turn off echoing
echo>%1
:=====Creates an empty file called, in this case, "HELLO"
dir /b/l %1>lower.tmp
:=====Runs the dir command to list files called "HELLO" in the current directory, outputting in lower case, and output the results to lower.tmp. You will now have another file in your current directory, called lower.tmp, with the contents "hello"
set /p result=<lower.tmp
:=====Set the variable result to be the contents of lower.tmp (which is "hello")
del lower.tmp
del %1
:=====delete the two files we've created
echo %result%
:=====Output the variable %result% (which equals "hello")

So, it's a bit of a hack, but a lot of things in batchworld are. Still, Bill's solution is better.

In response to the question you just asked Rene, I would suggest you type the following three commands, one by one, into a command prompt, and see if that answers your question...

set tempvar=Here are some XXXXX's
echo %tempvar%
echo %tempvar:X=A%
0
 
baffy2000Commented:
edit: Sorry, I meant the question you just asked Bill....!

:-)
0
 
ReneGeAuthor Commented:
==>Baffy

I closed the thread to rapidly. I should have waited for your reply.  One thousand sorries...

Your script is very creative.  Thanks for taking the time for explaining it to me.

Thanks for answering my question to bp.

You'r a superstar !!

Cheers,
Rene
0
 
ReneGeAuthor Commented:
I finally got it!!

Thanks Baffy
-Example.png
0
 
Bill PrewCommented:
Okay, I'm gonna assume you're cool with the fancy logic there unless you say otherwise, glad it makes more sense now.

~bp
0
 
ReneGeAuthor Commented:
Yea... I'm cool and happy !!

Thanks bp
0
 
baffy2000Commented:
I feel all warm inside.
0
 
ReneGeAuthor Commented:
lol
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 5
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now