【问题标题】:Batch Script to switch files depending on userinput根据用户输入切换文件的批处理脚本
【发布时间】:2019-11-28 07:27:52
【问题描述】:

它应该如何工作/应该做什么:

在 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) 我知道如何复制/粘贴文件

缺少什么: 1)获取用户输入(数字)并根据输入复制相应的文件(不确定如何将用户输入循环/映射到实际的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 文件进行测试。 任何帮助表示赞赏!

【问题讨论】:

  • set count = 0 将名为 count + space 的变量设置为像 space + 0... 这样的值

标签: windows batch-file xcopy


【解决方案1】:

您可以使用如下代码中的数组轻松完成:

@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

【讨论】:

  • @Julian 欢迎来到 Stack Overflow。请通过 Stack Overflow (SO) tour 了解如何通过单击答案左侧的灰色复选标记符号来接受有帮助的答案,从而对 SO 表示感谢。另请参阅How does accepting an answer work? 没有接受答案的问题即使有很好的答案也会被评为未回答的问题。
  • 粘贴时如何重命名正在复制到 ConsoleVariables.ini 的文件?
  • @Julian 把最后一行换成xcopy /I /F /Y "!listpath[%%i]!" "%iniDestinationFolder%\ConsoleVariables.ini"
  • @Julian,使用copy 而不是xcopy 并附加目标名称:copy /Y "!listpath[%%i]!" "!iniDestinationFolder!\ConsoleVariables.ini"...
  • Set /a "First=%count%-%count% + 1" 行是什么?它只是将变量First 设置为1 的值...我会将set /P "Input=" 替换为set "Input=0" & set /P Input="" && findstr "^[0123456789][0123456789]*$" > nul || goto :Repeat 并将标签:Repeat 放在该行之前,以便进行某种验证...
【解决方案2】:

假设您的目录中的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"

【讨论】:

  • 感谢您的回答。现在它不会为我删除/复制任何文件。启动 cmd 屏幕也不显示实际文件(见屏幕截图)。 imgur.com/2oqDXJA
  • 不知道为什么,但是在执行批处理文件后,cmd提示符会立即再次关闭。
  • 在批处理文件底部添加暂停以查看原因
  • 那么您的文件夹名称不正确。确保更新 inifolderinidestinationfolder
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 2020-01-31
相关资源
最近更新 更多