Trying to make a menu in a windows command prompt
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 libincoming_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?
cmd command-prompt
add a comment |
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 libincoming_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?
cmd command-prompt
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? stackoverflow.com/questions/1223721/…
– ba_ul
Nov 26 '18 at 4: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
add a comment |
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 libincoming_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?
cmd command-prompt
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 libincoming_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?
cmd command-prompt
cmd command-prompt
edited Nov 26 '18 at 13:18
Dávid Laczkó
429128
429128
asked Nov 25 '18 at 19:02
Jonathan OswaldJonathan Oswald
32
32
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? stackoverflow.com/questions/1223721/…
– ba_ul
Nov 26 '18 at 4: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
add a comment |
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? stackoverflow.com/questions/1223721/…
– ba_ul
Nov 26 '18 at 4: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
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
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
1
Better? Got rid of all the unnecessary info.
– Jonathan Oswald
Nov 25 '18 at 21:11
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? stackoverflow.com/questions/1223721/…
– ba_ul
Nov 26 '18 at 4:58
I don't know much about Windows cmd. But aren't you essentially looking for what's here? stackoverflow.com/questions/1223721/…
– ba_ul
Nov 26 '18 at 4: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
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
add a comment |
2 Answers
2
active
oldest
votes
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).
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
|
show 6 more comments
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470875%2ftrying-to-make-a-menu-in-a-windows-command-prompt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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).
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
|
show 6 more comments
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).
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
|
show 6 more comments
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).
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).
edited Nov 26 '18 at 13:42
answered Nov 26 '18 at 11:57
Dávid LaczkóDávid Laczkó
429128
429128
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
|
show 6 more comments
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
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
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
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
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
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
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
|
show 6 more comments
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 26 '18 at 19:58
Dávid LaczkóDávid Laczkó
429128
429128
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470875%2ftrying-to-make-a-menu-in-a-windows-command-prompt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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? stackoverflow.com/questions/1223721/…
– ba_ul
Nov 26 '18 at 4: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