-1

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
E. Reed
  • 47
  • 6
  • See my answer here about admin. https://stackoverflow.com/questions/47895544/mkdir-in-batch-file-as-admin/47896026#47896026 – catcat Jan 26 '19 at 06:33
  • 2
    `for /r "%ProgramFiles(x86)%\Acivision\X-Men Legends 2\" %%a in (*.IGB) do ren "%%a" "%%~na.igb"`. Another line for `XMLB` and you're done. See `for /?` for more info (and visit [SS64](https://ss64.com/nt/)) – Stephan Jan 26 '19 at 08:37
  • @double-beep none of the answers have worked for me – E. Reed Feb 12 '19 at 20:14

2 Answers2

2

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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • When I run the batch file with that command in it, command prompt just opens up and instantly closes, and no file extensions are changed – E. Reed Jan 26 '19 at 22:39
  • @E.Reed run it from an open command prompt (not via double-click). The window will stay open, so you can read any error messages. – Stephan Jan 27 '19 at 08:27
  • I did replace .EXT and .ext with the necessary extensions. I’ll try from simply a command prompt window to see what the error is – E. Reed Jan 27 '19 at 16:21
0
@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 i.e. doc txt (not *.doc *.txt). If you enter i.e. %cd% as the directory input, it will be expanded.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • @double-beep The start-as-admin appears to be an optional request question. Required is not how I would claim it to be. What "*should be*" is questionable in of itself, which could be based on opinion. – michael_heath Jan 27 '19 at 11:50
  • @double-beep If it were too broad, I may have not been able to answer, yet I did. Refer last line in my last comment about "*should be*". – michael_heath Jan 27 '19 at 12:02
  • @double-beep That is your opinion. The OP decides and the OP can learn from the answer. Calling an answer "ugly" in the same topic as your answer or any other negative opinion could be considered causing harm. – michael_heath Jan 27 '19 at 12:25