根据用户输入切换文件的批处理脚本
Batch Script to switch files depending on userinput
它应该如何 work/What 它应该如何:
在 C:\Users\RandomUser/Game 我想替换 C:\Users\RandomUser/Game/ConsoleVariables.ini 的文件。
为此,用户应准备一个包含不同 ini 文件的文件夹,该路径将硬编码在 .bat 脚本中。
在 运行 脚本之后,它将计算在硬编码路径中找到了多少个 .ini 文件(有效)并在 cmd 中显示它们,文件后面有一个从 1 到 n 的数字,以便用户看到按什么数字来选择根据文件。 CMD-Screenshot
如果用户输入一个数字(验证检查会很好)游戏位置的原始 .ini 文件应被删除并替换为他选择的 .ini 文件,复制的 ini 文件应重命名为ConsoleVariables.ini(文件将被复制,因此它不会在文件夹中消失,因此可以重新完成)。
我的工作:
1)循环遍历所有ini文件,并在后面显示1到n的数字
2) 我知道如何 copy/paste 文件
缺少什么:
1)获取用户输入(数字)并根据输入复制相应的文件(不确定如何将用户输入 loop/map 到实际的 ini 文件,因此 1 实际上绑定到第一个 .ini 文件,依此类推)
2) 将复制的文件重命名为 ConsoleVariables.ini 但不要更改原始文件的名称
@echo off
set iniFolder=C:\Users\RandomUser\Inis
set iniDestinationFolder=C:\Users\RandomUser/Game
setlocal enableextensions
setlocal ENABLEDELAYEDEXPANSION
set count = 0
set /a c=0
FOR %%i in (%iniFolder%\*.txt) DO set /a count+=1 & set /a c=c+1 & echo %%i !c!
echo *****%count% ini files found*****
echo press the according number to replace your ini file (1 - %count%)
endlocal
::logic missing here
set /P id=Enter number:
If id==1 goto accepted
:accepted
del %iniDestinationFolder%\Test.txt
xcopy %iniFolder%\Test2.txt %iniDestinationFolder%
pause
注意: 在上面的示例中,我使用 .txt 文件而不是 .ini 文件进行测试。
感谢您的帮助!
假设你目录下的ini文件少于10个,我们可以使用choice代替:
@echo off
set "iniFolder=%userprofile%\Inis"
set "iniDestinationFolder=%userprofile%\Game"
setlocal enabledelayedexpansion
set cnt=0
for %%i in ("%inifolder%\*.txt") DO (
set /a cnt+=1
set "file!cnt!=%%~nxi"
set chc=!chc!!cnt!
call echo !cnt!. %%file!cnt!%%
)
choice /c !chc! /m "Select a file"
copy /Y "%iniFolder%\!file%errorlevel%!" "%iniDestinationFolder%\ConsoleVariables.ini"
您可以使用如下代码所示的数组轻松完成此操作:
@echo off
set "iniFolder=%userprofile%\Desktop\Inis"
set "iniDestinationFolder=%userprofile%\Desktop\Game"
setlocal ENABLEDELAYEDEXPANSION
set count=0
REM Just replace the variable "Ext=txt" by "Ext=ini" after testing it with text files !
Set "Ext=txt"
echo(
REM In this loop (FOR .. DO) ==> We fill the list files into an array
FOR %%f in ("%iniFolder%\*.%Ext%") DO (
SET /a "Count+=1"
set "list[!Count!]=%%~nxf"
set "listpath[!Count!]=%%~dpFf"
)
REM In this loop (FOR..DO) ==> Display array elements in order to show the list files found
For /L %%i in (1,1,%Count%) Do (
echo %%i-!list[%%i]!
)
echo(
echo *****%count% %Ext% files found*****
Set /a "First=%count%-%count% + 1"
echo press the according number to replace your %Ext% file (%First% - %count%)
Set /p "Input="
For /L %%i in (1,1,%Count%) Do (
If [%INPUT%]==[%%i] (
xcopy /I /F "!listpath[%%i]!" "%iniDestinationFolder%\"
)
)
pause & exit
它应该如何 work/What 它应该如何:
在 C:\Users\RandomUser/Game 我想替换 C:\Users\RandomUser/Game/ConsoleVariables.ini 的文件。 为此,用户应准备一个包含不同 ini 文件的文件夹,该路径将硬编码在 .bat 脚本中。 在 运行 脚本之后,它将计算在硬编码路径中找到了多少个 .ini 文件(有效)并在 cmd 中显示它们,文件后面有一个从 1 到 n 的数字,以便用户看到按什么数字来选择根据文件。 CMD-Screenshot
如果用户输入一个数字(验证检查会很好)游戏位置的原始 .ini 文件应被删除并替换为他选择的 .ini 文件,复制的 ini 文件应重命名为ConsoleVariables.ini(文件将被复制,因此它不会在文件夹中消失,因此可以重新完成)。
我的工作: 1)循环遍历所有ini文件,并在后面显示1到n的数字 2) 我知道如何 copy/paste 文件
缺少什么: 1)获取用户输入(数字)并根据输入复制相应的文件(不确定如何将用户输入 loop/map 到实际的 ini 文件,因此 1 实际上绑定到第一个 .ini 文件,依此类推) 2) 将复制的文件重命名为 ConsoleVariables.ini 但不要更改原始文件的名称
@echo off
set iniFolder=C:\Users\RandomUser\Inis
set iniDestinationFolder=C:\Users\RandomUser/Game
setlocal enableextensions
setlocal ENABLEDELAYEDEXPANSION
set count = 0
set /a c=0
FOR %%i in (%iniFolder%\*.txt) DO set /a count+=1 & set /a c=c+1 & echo %%i !c!
echo *****%count% ini files found*****
echo press the according number to replace your ini file (1 - %count%)
endlocal
::logic missing here
set /P id=Enter number:
If id==1 goto accepted
:accepted
del %iniDestinationFolder%\Test.txt
xcopy %iniFolder%\Test2.txt %iniDestinationFolder%
pause
注意: 在上面的示例中,我使用 .txt 文件而不是 .ini 文件进行测试。 感谢您的帮助!
假设你目录下的ini文件少于10个,我们可以使用choice代替:
@echo off
set "iniFolder=%userprofile%\Inis"
set "iniDestinationFolder=%userprofile%\Game"
setlocal enabledelayedexpansion
set cnt=0
for %%i in ("%inifolder%\*.txt") DO (
set /a cnt+=1
set "file!cnt!=%%~nxi"
set chc=!chc!!cnt!
call echo !cnt!. %%file!cnt!%%
)
choice /c !chc! /m "Select a file"
copy /Y "%iniFolder%\!file%errorlevel%!" "%iniDestinationFolder%\ConsoleVariables.ini"
您可以使用如下代码所示的数组轻松完成此操作:
@echo off
set "iniFolder=%userprofile%\Desktop\Inis"
set "iniDestinationFolder=%userprofile%\Desktop\Game"
setlocal ENABLEDELAYEDEXPANSION
set count=0
REM Just replace the variable "Ext=txt" by "Ext=ini" after testing it with text files !
Set "Ext=txt"
echo(
REM In this loop (FOR .. DO) ==> We fill the list files into an array
FOR %%f in ("%iniFolder%\*.%Ext%") DO (
SET /a "Count+=1"
set "list[!Count!]=%%~nxf"
set "listpath[!Count!]=%%~dpFf"
)
REM In this loop (FOR..DO) ==> Display array elements in order to show the list files found
For /L %%i in (1,1,%Count%) Do (
echo %%i-!list[%%i]!
)
echo(
echo *****%count% %Ext% files found*****
Set /a "First=%count%-%count% + 1"
echo press the according number to replace your %Ext% file (%First% - %count%)
Set /p "Input="
For /L %%i in (1,1,%Count%) Do (
If [%INPUT%]==[%%i] (
xcopy /I /F "!listpath[%%i]!" "%iniDestinationFolder%\"
)
)
pause & exit