Page 1 of 1

changing all the file names' case in a directory to UPPER

Posted: 16 Oct 2014 19:17
by rafi401
Hi All,
I am new to this forum.
I have many files in a directory with different names. I want to change the file names to upper case.
I am able to change them using command prompt and excel as of now.
I am wondering whether we can do this process automatically by using any batch file.
Can anyone please help me out in changing the case of all file names(lower to upper) in the directory using dos batch file.

Sample fle name: "abcd_123.txt"

Thanks in advance
Rafi

Re: changing all the file names' case in a directory to UPPE

Posted: 16 Oct 2014 22:50
by Squashman
We have a function in the Library to upper case a string.
You could basically parse all the file names in a directory with the FOR command, send the file name to the uppercase routine and then do a rename.
http://www.dostips.com/DtTipsStringOper ... on.toUpper

Re: changing all the file names' case in a directory to UPPE

Posted: 17 Oct 2014 01:33
by Yury

Code: Select all

@echo off

set "dir=D:\Test"

chcp 1252>nul
for /f "tokens=1* delims=-" %%i in ('for /f "delims=" %%k in ^('"dir /a-d/b "%dir%" 2>nul"'^) do @find "" "%%k" 2^>^&1') do (
 if not "%%j"=="" (
  for /f "delims=" %%l in ('echo "%%j"^| find /v """"""') do for /f "tokens=*" %%m in (%%l) do echo %%m
  ) else (
  for /f "delims=" %%l in ('echo "%%i"^| find /v """"""') do for /f "tokens=*" %%m in (%%l) do echo %%m
  )
 )

pause>nul
exit /b



This code is universal only for files with ASCII names.

Re: changing all the file names' case in a directory to UPPE

Posted: 17 Oct 2014 04:03
by rafi401
Thanks Yury.

I am able to see all the file names of a directory in Upper cases now. :)

However, can you please tell me how to rename the files present in the directory from Lower Case to UPPER CASE.

Thanks Again,
Rafi

Re: changing all the file names' case in a directory to UPPE

Posted: 17 Oct 2014 04:08
by foxidrive
Here's another way for the current folder - it works here in Windows 8.1

Test it on a folder with only test files inside it.

Code: Select all

@echo off
break>~
for /f "delims=" %%a in ('dir /b /a-d ') do for /f "tokens=4,*" %%b in ('fc /b "~" "%%a"^|find /v ":"') do ren "%%a" "%%c"
del ~

Re: changing all the file names' case in a directory to UPPE

Posted: 17 Oct 2014 05:36
by Yury
rafi401 wrote:However, can you please tell me how to rename the files present in the directory from Lower Case to UPPER CASE.



Code: Select all

@echo off

set "dir=D:\Test"

chcp 1252>nul
pushd "%dir%"
for /f "tokens=1* delims=-" %%i in ('for /f "delims=" %%k in ^('"dir /a-d/b "%dir%" 2>nul| find /v "?""'^) do @find "" "%%k" 2^>^&1') do (
 if not "%%j"=="" (
  for /f "tokens=*" %%l in ("%%j") do ren "%%l" "%%l"
  ) else (
  for /f "tokens=*" %%l in ("%%i") do ren "%%l" "%%l"
  )
 )
popd

exit /b

Re: changing all the file names' case in a directory to UPPE

Posted: 17 Oct 2014 06:41
by Squashman

Code: Select all

@echo off &setlocal enabledelayedexpansion
set "basefolder=H:\Uppertest\"
pushd "%basefolder%"

REM just doing this to show the filenames
echo File names before Upper Casing
DIR /b

FOR %%G IN (*) DO (
   set "str=%%~G"
   CALL :toUpper str
   ren "%%~G" "!str!"
)
echo File Names after upper casing
DIR /b
GOTO :EOF

:toUpper str -- converts lowercase character to uppercase
::           -- str [in,out] - valref of string variable to be converted
:$created 20060101 :$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%%
)
EXIT /b

Output

Code: Select all

C:\BatchFiles\UpperCase>UpperNames.bat
File names before Upper Casing
alllower.txt
CamelCase.txt
File Names after upper casing
ALLLOWER.TXT
CAMELCASE.TXT

C:\BatchFiles\UpperCase>

Re: changing all the file names' case in a directory to UPPE

Posted: 17 Oct 2014 08:28
by Yury
I have perfected and shortened my code. Now it does not depend on the code page and ASCII symbols in file names:


Code: Select all

@echo off

set "dir=D:\Test"

pushd "%dir%"
for /f "tokens=* delims=- " %%i in ('for /f "delims=" %%j in ^('"dir /a-d/b "%dir%" 2>nul"'^) do @find "" "%%j"') do ren "%%i" "%%i"
popd

exit /b


.

Re: changing all the file names' case in a directory to UPPE

Posted: 18 Oct 2014 01:47
by rafi401
Thanks a million all.. :)
The files in the directory can be renamed to upper case. :)
Is there any option to set/fetch the directory in the script dynamically?

Also i am trying to change the files's case to lower.. but with no luck :(
Can you please help me out.
Thanks Much,
Rafi

Re: changing all the file names' case in a directory to UPPE

Posted: 18 Oct 2014 02:38
by Yury
rafi401 wrote:Is there any option to set/fetch the directory in the script dynamically?



Code: Select all

@echo off

echo Enter the directory.
echo.
set /p "dir="

pushd "%dir%"
for /f "tokens=* delims=- " %%i in ('for /f "delims=" %%j in ^('"dir /a-d/b 2>nul"'^) do @find "" "%%j"') do ren "%%i" "%%i"
popd

exit /b

Re: changing all the file names' case in a directory to UPPE

Posted: 18 Oct 2014 02:55
by Yury
rafi401 wrote:Also i am trying to change the files's case to lower.. but with no luck



Code: Select all

@echo off

echo Enter the directory.
echo.
set /p "dir="

pushd "%dir%"
for /f "delims=" %%i in ('"dir /a-d/b/l 2>nul"') do ren "%%i" "%%i"
popd

exit /b

Re: changing all the file names' case in a directory to UPPE

Posted: 18 Oct 2014 03:51
by rafi401
Thanks a lot Yury :)
i am able to integrate the case change and was able to see my expected output.

Kudos to you.. :)

Code: Select all

@ECHO off
:again
echo Enter valid entry from the below options
SET /P OPT=Which option would you like? 1) Lower 2) Upper 3) Exit   

IF %OPT%==1 GOTO :Lower
IF %OPT%==2 GOTO :Upper
IF %OPT%==3 GOTO :Exit
GOTO :again

:Lower
REM "@echo off

echo Enter the directory.
echo.
set /p "dir="

pushd "%dir%"
for /f "delims=" %%i in ('"dir /a-d/b/l 2>nul"') do ren "%%i" "%%i"
popd

exit /b"

:Upper
REM "@echo off

echo Enter the directory.
echo.
set /p "dir="

pushd "%dir%"
for /f "tokens=* delims=- " %%i in ('for /f "delims=" %%j in ^('"dir /a-d/b 2>nul"'^) do @find "" "%%j"') do ren "%%i" "%%i"
popd

exit /b"


:Exit
Exit