简体   繁体   中英

Recursively change file extensions to lower case

I have a game that I play and mod a lot, and a lot of the files in the game have file extensions that are in all caps, which bothers me quite a bit. I'm trying to change them all to be lowercase, but there are numerous folders in the game files, so I'm having to be very repetitive. Right now, I'm working with this:

cd\program files (x86)\Activision\X-Men Legends 2\Actors
start ren *.IGB *.igb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\
start ren *.XMLB *.xmlb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\act0\tutorial\tutorial1
start ren *.XMLB *.xmlb

and so on for each and every folder in the game files. I have a very long .bat file where I just have line after line of this but with a different destination folder. Is there a way to streamline this process so I don't have to manually type out each folder name? Also, is there a line that I could add at the beginning to automatically run as an administrator, so I don't have to make sure to run the .bat file as an administrator each time?

I'm not looking for anything complicated, and I'm very inexperienced with coding other than the small amount of stuff I've been able to search up.

Instead of doing it for each folder, use a for /R loop which loops through all subfolders. I would suggest the following code:

@echo off

:prompt
set /p "extensions=What are the up-case extensions you want to convert to lower-case?: "
if not defined extensions (cls & goto:prompt) else (goto:loop)

:loop
for %%A IN (%extensions%) do (
    for /R "custom_folder" %%B IN (*.%%A) do (
        ren "%%~fB" "%%~nB.%%A"
    )
)

Take a look on this on how to run this batch file as admin. Create another batch file and add the code specified in the accepted answer.

Note: As Stephan pointed out in the comments, you can use %ProgramFiles(x86)% environment variable which is the same thing .

@echo off
setlocal

rem Check if admin.
2>nul >nul net session || goto :runasadmin

rem Start in script directory.
pushd "%~dp0" || (
    >&2 echo Failed to change directory to "%~dp0".
    pause
    exit /b 1
)

rem Ask for directory to change to, else use the script directory if undefined.
set "dirpath=%~dp0"
set /p "dirpath=Dir path: "

rem Expand any environmental variables used in input.
call set "dirpath=%dirpath%"

rem Start in the input directory.
pushd "%dirpath%" || (
    >&2 echo Failed to change directory to "%dirpath%".
    pause
    exit /b 1
)

rem Ask for file extensions.
echo File extensions to convert to lowercase, input lowercase.
echo   i.e. doc txt

set "fileext="
set /p "fileext=File extension(s): "

if not defined fileext (
    >&2 echo Failed to input file extension.
    pause
    exit /b 1
)

rem Display current settings.
echo dirpath: %dirpath%
echo fileext: %fileext%
pause

rem Do recursive renaming.
for %%A in (%fileext%) do for /r %%B in (*.%%A) do ren "%%~B" "%%~nB.%%A"

rem Restore to previous working directory.
popd

echo Task done.
pause
exit /b 0

:runasadmin
rem Make temporary random directory.
set "tmpdir=%temp%\%random%"

mkdir "%tmpdir%" || (
    >&2 echo Failed to create temporary directory.
    exit /b 1
)

rem Make VBS file to run cmd.exe as admin.
(
    echo Set UAC = CreateObject^("Shell.Application"^)
    echo UAC.ShellExecute "cmd.exe", "/c ""%~f0""", "", "runas", 1
) > "%tmpdir%\getadmin.vbs"

"%tmpdir%\getadmin.vbs"

rem Remove temporary random directory.
rd /s /q "%tmpdir%"
exit /b

This script is expected to start from double-click.

It will restart the script as admin if not already admin.

It will prompt to get information such as directory to change to and get file extensions ie doc txt ( not *.doc *.txt ). If you enter ie %cd% as the directory input, it will be expanded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM