0

i would like to trim file names in batch for eg: From "This IsGood 1.1.png" To "tig11.png"

The condition is that the script should get all capitalized letter from the file name,convert them into small letters and then form the trimmed file name without any spaces in it...

Please me help accomplishing this task.. Thanks!

gocool
  • 3
  • 1
  • You could use a `FOR /L` loop and examine each character, or where you failed? – jeb Apr 14 '12 at 12:35
  • And if the existing name is composed of nothing but lower case letters you get... ? – dbenham Apr 14 '12 at 12:36
  • And if JohnDoe.txt and JoyDivision.txt exist in the same folder you get... ? – dbenham Apr 14 '12 at 13:01
  • @dbenham 1.If all are lowercase the bat script executes but it does nothing to files.2.The file is overwritten or a number can be added i.e. JD.txt or JD_1.txt – gocool Apr 14 '12 at 13:26
  • so, the file is overwritten OR a number is added? show us what you tried and what problems you found. – PA. Apr 14 '12 at 16:39

1 Answers1

1

you may begin with a simple iteration on the filename

set str=This IsGood 1.1
:again
set chr=%str:~0,1%
set str=%str:~1%
if not  "%chr%"=="" echo %chr% 
if not "%str%"=="" goto :again

read HELP SET, HELP IF and HELP GOTO

Now, if understood, proceed by changing the echo command into a call...

 if not  "%chr%"=="" call :checkchar %chr% 
 ...
 :checkchar 
 echo %1
 goto :eof

read HELP CALL

then implement the actual code for checking if the character is uppercase or not

:checkchar 
for %%c in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9) do if .%1==.%%c echo %%1
goto :eof

read HELP FOR

change now this new echo command into a new call to a function to set the variable to lowercase

now concatenate the result into a, say, fn variable that you will need to initialize to blanks.

and wrap all with the appropiate filename handling, extracting just the filename (see the ~n option in the syntax of handling parameters) and then composing back the full path with the new name (using the ~d ~p ~x options)

@echo off
set fn=
call :upcaseonly %~n1
echo %~dp1%fn%%~x1
goto :eof

:upcaseonly
set str=%*
:again
set chr=%str:~0,1%
set str=%str:~1%
if not  "%chr%"=="" call :checkchar %chr% 
if not "%str%"=="" goto :again
goto :eof

:checkchar
for %%c in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9) do if .%1==.%%c set fn=%fn%%1
goto :eof 

use this as a starting point. you will have to add your own logic for renaming the file and handling if the file already exists or if the file cannot be renamed ...

PA.
  • 28,486
  • 9
  • 71
  • 95