Reputation: 213
I need a batch-file to convert all files in a folder and its subfolders to lowercase. For example:
Here Is StackOverflow.txt
to
here is stackoverflow.txt
A piece of the file name is in the bracket. Is it possible to neglect it and leave it on its previous state? e.g.
Here Is [A WEBSITE CALLED] StackOverflow.txt
to
here is [A WEBSITE CALLED] stackoverflow.txt
Upvotes: 1
Views: 2526
Reputation:
Just adding another option to the answers, this will rename all the files in a folder and its subfolders to lowercase version of the name.
from cmdline:
note, make sure to CD
to the correct dir, before running it:
@for /f "delims=" %i in ('dir /b/l/a-d') do ren "%~fi" "%~i"
or Batch file:
@echo off
for /f "delims=" %%i in ('dir /b/l/a-d') do echo ren "%%~fi" "%%~i"
Upvotes: 1
Reputation: 67216
@echo off
setlocal EnableDelayedExpansion
rem Start the recursive process over the tree
call :processThisDir
goto :EOF
:processThisDir
rem Process all filenames in this folder and separate they in three parts
for /F "tokens=1-3 delims=[]" %%a in ('dir /B /A-D') do (
set "left=%%a" & set "right=%%c"
rem Convert left and right parts to lower case
for %%l 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) do (
set "left=!left:%%l=%%l!"
set "right=!right:%%l=%%l!"
)
rem Rename this file
ren "%%a[%%b]%%c" "!left![%%b]!right!"
)
rem Recursively process the folders in this folder
for /D %%a in (*) do (
cd "%%a"
call :processThisDir
cd ..
)
exit /B
Upvotes: 2
Reputation: 130819
Easily done with JREN.BAT - a hybrid JScript/batch script that renames files via regular expression replacement. JREN.BAT is pure script that runs natively on any Windows machine from XP onward.
To simply convert all file names to lower case:
jren "^" "" /l /s
If you want all text between square brackets to be upper case, and everything else to be lower case, then it is easily done with two commands
jren "^" "" /l /s
jren "[.+?]" "uc($0)" /j /s
If you want to preserve the original case of all text between square brackets, and convert everything else to lower case, then it takes a more complicated regular expression and replacement string.
jren "([^[]*)(\[.*?\])*" "lc($1?$1:'')+($2?$2:'')" /j /s
Since JREN is a batch script, you must use CALL JREN
if you want to use the command within another batch script.
Use jren /?
to get help on all available options.
Upvotes: 4