递归地将文件扩展名更改为小写

时间:2019-01-26 05:51:01

标签: windows batch-file command-prompt

我有一个经常玩和修改的游戏,并且游戏中的许多文件都有大写的文件扩展名,这让我很困扰。我试图将它们全部更改为小写字母,但游戏文件中有许多文件夹,因此我必须非常重复。现在,我正在处理此问题:

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

,对于游戏文件中的每个文件夹,依此类推。我有一个很长的.bat文件,其中只有一行,但是有不同的目标文件夹。有没有一种方法可以简化此过程,所以我不必手动键入每个文件夹的名称?另外,我是否可以在开头添加一行以管理员身份自动运行,所以不必确保每次都以管理员身份运行.bat文件?

我并没有寻找任何复杂的东西,除了我已经能够搜索的少量内容之外,我对编码没有任何经验。

2 个答案:

答案 0 :(得分:2)

使用for /R循环来遍历所有子文件夹,而不是对每个文件夹都这样做。我建议以下代码:

@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"
    )
)

看看this如何以管理员身份运行此批处理文件。创建另一个批处理文件,并添加接受的答案中指定的代码。

注意:正如Stephan在评论中所指出的,您可以使用%ProgramFiles(x86)%环境变量,它是同一件事

答案 1 :(得分: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

此脚本应从双击开始。

如果尚未使用admin,它将以admin身份重新启动脚本。

它将提示您获取要更改的目录之类的信息并获取文件扩展名,即doc txt不是*.doc *.txt )。如果输入%cd%作为目录输入,它将被展开。