Newbie trouble: Returning a subroutine's value

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Newbie trouble: Returning a subroutine's value

#1 Post by noprogrammer » 29 Oct 2009 12:35

Hi,

my batch skills are somewhat rusty and I've been fumbling around for hours finding out how to glue those parts together, but no way.
(Checking DtTutoFunctions.php, didn't get me further.)

I'd like to use the function :toCamelCase combined with a simple For loop to rename files with uppercase filenames.
Before: ANY FILE.DAT, OTHER_ONE ...
After: AnyFile.dat, Otherone ...

So basically, what I want to do is referencing :toCamelCase from within a For loop which iterates through all matching directory entries:

Code: Select all

@echo off
For /f "tokens=*" %%i in ('dir /a:-d /b %MyDir% ^| findstr /v "[abcdefghijklmnopqrstuvwxyz]"') do (
   set "s=%%~i"
   REM Now what's missing is some sort of statement
   REM that allows saving the return value of :toCamelCase
   REM in a new variable for further use:
   ren "%%~i" %ret_val
)

:toCamelCase str -- converts a string to camel case
::               -- str [in,out] - valref of string variable to be converted
[...]

Now what's the missing line in the above code that will do this?
I have no clue how to get the return value of :toCamelCase into a variable (this example seems to work, though).

Any enlightening help will be appreciated! Thanks in advance.

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

For the sake of completeness

#2 Post by noprogrammer » 30 Oct 2009 11:45

In the code sample above the files to process might just lie in the current folder, so the file's path is not needed.

Could anybody tell me what's happening in line 13 and particularly in line 21 of :toCamelCase?
Is :toCamelCase suitable for the renaming task? And if not, how do I alter the function to meet my needs?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#3 Post by avery_larry » 02 Nov 2009 11:04

The line you need is:

call :tocamelcase s

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

#4 Post by noprogrammer » 02 Nov 2009 12:27

Hi avery_larry,

avery_larry wrote:The line you need is:

call :tocamelcase s


Unfortunately, that's merely the first step.
Executing this command does apply camel case to the content of s – but I need to pass it on to ren:

Code: Select all

ren "%%~i" %s
(This doesn't work – yet.)

I'm not sure about the proper syntax, but something like:
new_var=`call :tocamelcase s`
The backticks signifying a preceding (:tocamelcase) execution to assign the correct value to new_var.
If this is feasible, a succeeding ren will work.

Any idea?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#5 Post by avery_larry » 02 Nov 2009 14:20

at the beginning of the script, add:

setlocal enabledelayedexpansion

Then you can do this:

ren "%%~i" "!s!"

noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

#6 Post by noprogrammer » 03 Nov 2009 07:47

That's it, thanks a bunch, avery_larry! Delayed expansion, right.
First tests showed that it works for directories with approx. two dozens files. Don't know the upper limit, though.

However, I noticed, that the For loop keeps skipping the first matching entry.
Echoing the matching filenames, this first entry is printed as "ANYFILE.LOG" (tested with various file types), throwing an error ("cannot be found") for the rename task.

Apart from this, the batch now seems to work as expected.
This is the entire script:

Code: Select all

@echo off
Setlocal EnableExtensions
Setlocal EnableDelayedExpansion

REM [...]
pushd %SomeDir%
attrib -a -r

For /f "tokens=*" %%i in ('dir /a:-d /b ^| findstr /v "[abcdefghijklmnopqrstuvwxyz]"') do (
    set "s=%%~i"
    call:toCamelCase s
    ren "%%~i" "!s!"
)

popd

:toCamelCase str -- converts a string to camel case
::               -- str [in,out] - valref of string variable to be converted
:$created 20080219 :$changed 20080219 :$categories StringManipulation
:$source http://www.dostips.com
If not defined %~1 exit/b
For %%a 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:%%~a%%"
)
call set "%~1= %%%~1%%"
For %%a 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:%%~a%%"
)
call set "%~1=%%%~1: =%%"
exit/b

Post Reply