Page 1 of 1

Newbie trouble: Returning a subroutine's value

Posted: 29 Oct 2009 12:35
by noprogrammer
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.

For the sake of completeness

Posted: 30 Oct 2009 11:45
by noprogrammer
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?

Posted: 02 Nov 2009 11:04
by avery_larry
The line you need is:

call :tocamelcase s

Posted: 02 Nov 2009 12:27
by noprogrammer
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?

Posted: 02 Nov 2009 14:20
by avery_larry
at the beginning of the script, add:

setlocal enabledelayedexpansion

Then you can do this:

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

Posted: 03 Nov 2009 07:47
by noprogrammer
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