1

I have a batch file that gets run by the user typing:

usercompile filename

usercompile is a batch file that does this:

copy  /y %1.txt lib\incoming_file.txt

and then starts the compiler:

compiler.exe

The compiler has the "incoming_file" name hard-coded into linked source (this can't be chaged), so the current method is simply to copy the user file in and rename it to the known name and run the compiler.

I'd like to present the user with a list of files that are generated when a batch file is run, then the batch file would copy the selected file in, rename it (just like is done now).

So it would look like this:

Please choose a file to compile:
1) matthews_build
2) marks_build
3) lukes_build

and then the user would type 1 or 2 or 3 (in this case) and press enter. The batch file would copy that file to the known file name and launch the compiler. The one good thing is that the files that need to be listed all have a unique extension (.jal).

Any ideas?

Dávid Laczkó
  • 1,091
  • 2
  • 6
  • 25
  • Welcome to Stack Overflow. Please post the relevant parts of your script so that it's easier for others to understand the issue and offer a solution. – ba_ul Nov 25 '18 at 20:37
  • 1
    Better? Got rid of all the unnecessary info. – Jonathan Oswald Nov 25 '18 at 21:11
  • I don't know much about Windows cmd. But aren't you essentially looking for what's here? https://stackoverflow.com/questions/1223721/in-windows-cmd-how-do-i-prompt-for-user-input-and-use-the-result-in-another-com – ba_ul Nov 26 '18 at 04:58
  • Sort of, but that doesn’t list all files with a given extension or copy those files based upon user input. That only shows that you can indeed make a menu with user input. – Jonathan Oswald Nov 26 '18 at 10:51

2 Answers2

1

I changed my approach and consider my previous answer a bad practice: re-listing the files with a second dir command unnecessarily reads the disk again, not to mention the rare but possible case if a file is added/removed between the 2 dir's and makes the whole thing unreliable.
Based on this brilliant solution I did a possible implementation with dynamic array:

@echo off

set /a counter=0

setlocal enabledelayedexpansion
FOR /f "delims=|" %%i IN ('dir /b /on "yourpath*.jal"') DO (
    set /a counter+=1
    rem echo !counter!^) %%~ni
    set FileList[!counter!]=%%~ni & rem This is an array element, a dinamically created variable
)

rem Iterate through variables:
FOR /l %%i IN (1,1,!counter!) DO (
    echo %%i^) !FileList[%%i]!
)

set /p option="Choose an option: "
echo !FileList[%option%]!

endlocal

This makes the file list available for any number of following commands.

Dávid Laczkó
  • 1,091
  • 2
  • 6
  • 25
0

One possible solution is to list all .jal files and give them an option number, store the result, and based on user input, look up the file based on the option number. As I know no way of storing such a result in memory (no array/hash table data type), only in a file, if a file can not be used, then the listing should be repeated in a deterministic way so that if we re-assign the option numbers, we get the same result. We can do it ensuring alphabetical ordering.
Here is one implementation:
BLOCK 1

setlocal enabledelayedexpansion

FOR /f "delims=|" %%i IN ('dir /b /on "yourpath\*.jal"') DO (
    set /a counter+=1
    echo !counter!^) %%~ni
)

endlocal

The nested dir command ensures alphabetical ordering (reference.)
A remark why I put a pipe (|) as a delimiter: if you don't define a delimiter, the default space will be used. If your file name contains space then it would be truncated. So I picked a character that is not valid in file names ensuring the whole file name is returned.

Now if you get a number from the user by this:

set /p option=Choose your option: 

after this command (evaluating and possibly re-requesting the input) to do a lookup for the file you can repeat BLOCK 1 but replace the echo line with examining the option like this:

if !counter! == %option%

and put those commands in the if block to do whatever you want to do with the file (for debugging, put back the echo command).

Dávid Laczkó
  • 1,091
  • 2
  • 6
  • 25
  • Thanks! So far the list builds, the prompt accepts a choice, but I can't seem to get the choice number to actually do anything. Here's what I have: `@echo off setlocal enabledelayedexpansion FOR /f "delims=|" %%i IN ('dir /b /on "*.jal"') DO ( set /a counter+=1 echo !counter!^) %%~ni ) endlocal set /p option=Choose your option: FOR /f "delims=|" %%i IN ('dir /b /on "*.jal"') DO ( set /a counter+=1 if !counter! == goto action ) goto failed :action echo here we go echo counter :failed echo didn't quite get it pause` – Jonathan Oswald Nov 26 '18 at 13:50
  • Sorry, I'm struggling to format the code on this page :/ – Jonathan Oswald Nov 26 '18 at 13:50
  • if !counter! == goto action is not OK. if !counter! == %option% goto action – Dávid Laczkó Nov 26 '18 at 13:52
  • Still doesn't catch it, now using if !counter! == %option% goto action - still doesn't see the number. Always goes to the fail section – Jonathan Oswald Nov 26 '18 at 14:19
  • Went so far as to echo %option% and it does indeed have the number stored correctly, but it never seems to be able to match it up to one of the file names. Thanks for your patience... – Jonathan Oswald Nov 26 '18 at 14:22
  • Your goto command is not OK. The syntax requires a colon before the label name. But try to simply echo %%~ni before you proceed with more complicated operations like goto's. – Dávid Laczkó Nov 26 '18 at 14:23
  • Why did you remove setlocal enabledelayedexpansion - endlocal from around the second FOR? That is not going to work. You need to delay variable expansion so that you can use the actual runtime value of variables. – Dávid Laczkó Nov 26 '18 at 14:40
  • @DávidLaczkó: `goto` does *not* require the colon. It's optional. Personally I like to have it for readability, others don't - for the same reason... – Stephan Nov 26 '18 at 14:53
  • @DávidLaczkó All done works great - thank you so much. – Jonathan Oswald Nov 26 '18 at 18:35
  • I think this anwer should not be the accepted one - maybe I should even delete it, please consider my other one. – Dávid Laczkó Nov 26 '18 at 20:00