Search code examples
windowsbatch-filexcopy

Batch Script to switch files depending on userinput


How it should work/What it should do:

At C:\Users\RandomUser/Game I want to replace the file at C:\Users\RandomUser/Game/ConsoleVariables.ini. In order to do so the user should prepare a folder with his different ini files and that path will be hardcoded in the .bat script. After running the script it will count how many .ini files were found in the hardcoded path (works) and display them in cmd with a number from 1 to n behind the files so the user sees what number to press to choose the according file. CMD-Screenshot

If the user enters a number (validation check would be nice) the original .ini file at the game location should be deleted and be replaced by the .ini file which he chose and the copied ini file should be renamed to ConsoleVariables.ini (the file will be copied so it won't disappear in the folder so it can be re done).

What I have working: 1) loop through all ini files and display them with the number from 1 to n behind them 2) I know how to copy/paste files

What's missing: 1) Get userinput(number) and copy the according file depending on the input(Not sure how to loop/map the userinput to the actual ini files so 1 is actually bound to the first .ini file and so on) 2) rename copied file to ConsoleVariables.ini but don't change the name from the original file

@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

Note: In the example above im using .txt files instead of .ini files for testing purposes. Any help is appreciated!


Solution

  • You can do it easily by using an array like this code below :

    @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