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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rafi401
Posts: 4
Joined: 16 Oct 2014 18:58

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

#1 Post by rafi401 » 16 Oct 2014 19:17

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 16 Oct 2014 22:50

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

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

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

#3 Post by Yury » 17 Oct 2014 01:33

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.

rafi401
Posts: 4
Joined: 16 Oct 2014 18:58

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

#4 Post by rafi401 » 17 Oct 2014 04:03

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#5 Post by foxidrive » 17 Oct 2014 04:08

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 ~

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

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

#6 Post by Yury » 17 Oct 2014 05:36

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

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#7 Post by Squashman » 17 Oct 2014 06:41

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>

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

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

#8 Post by Yury » 17 Oct 2014 08:28

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


.

rafi401
Posts: 4
Joined: 16 Oct 2014 18:58

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

#9 Post by rafi401 » 18 Oct 2014 01:47

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

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

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

#10 Post by Yury » 18 Oct 2014 02:38

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

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

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

#11 Post by Yury » 18 Oct 2014 02:55

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

rafi401
Posts: 4
Joined: 16 Oct 2014 18:58

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

#12 Post by rafi401 » 18 Oct 2014 03:51

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

Post Reply