Solved

VBScript: Convert to lower case

Posted on 2010-09-07
22
3,230 Views
Last Modified: 2012-05-10
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
Comment
Question by:ReneGe
  • 7
  • 5
  • 4
  • +3
22 Comments
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 100 total points
ID: 33624483
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
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 25 total points
ID: 33624491
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33624492
cscript convert.vbs will produce a logo so pass in //nologo in order to remove it
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 3

Expert Comment

by:baffy2000
ID: 33625009
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
 
LVL 3

Assisted Solution

by:baffy2000
baffy2000 earned 25 total points
ID: 33625042
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
 
LVL 13

Assisted Solution

by:Daz_1234
Daz_1234 earned 175 total points
ID: 33625386
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
 
LVL 53

Accepted Solution

by:
Bill Prew earned 175 total points
ID: 33625918
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
 
LVL 13

Expert Comment

by:Daz_1234
ID: 33625950
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33626133
==> Daz_1234

Thanks!

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33626985
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
 
LVL 10

Author Closing Comment

by:ReneGe
ID: 33627104
You were all fantastic. I just hope I split the points fairly.

Cheers,
Rene
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33627161
Thanks Rene.

~bp
0
 
LVL 13

Expert Comment

by:Daz_1234
ID: 33627179
You're welcome Rene, thank you.

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33627185
==> bp,

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

Thanks,
Rene
0
 
LVL 3

Expert Comment

by:baffy2000
ID: 33627395
>==> 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
 
LVL 3

Expert Comment

by:baffy2000
ID: 33627403
edit: Sorry, I meant the question you just asked Bill....!

:-)
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33627615
==>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
 
LVL 10

Author Comment

by:ReneGe
ID: 33627950
I finally got it!!

Thanks Baffy
-Example.png
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33628029
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
 
LVL 10

Author Comment

by:ReneGe
ID: 33628140
Yea... I'm cool and happy !!

Thanks bp
0
 
LVL 3

Expert Comment

by:baffy2000
ID: 33628311
I feel all warm inside.
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33628565
lol
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now