0

I've got a batch file that copies some files based on user input and I want to simplify it. If a user types the letter A in the prompt, it copies A.txt from C:\ to E:\ Essentially, it goes like this:

set /p letter="Enter a letter of the alphabet"
if /i [%letter%]==[A] GOTO LetterA 
if /i [%letter%]==[B] GOTO LetterB 
if /i [%letter%]==[C] GOTO LetterC 
:LetterA 
robocopy C:\ E:\ A.txt 
:LetterB 
robocopy C:\ E:\ B.txt 
:LetterC 
robocopy C:\ E:\ C.txt 
:end

Would I have to type out/copy each line for every letter of the alphabet, for both the IF statement and the goto command it calls? Can I accomplish this easier with a for loop or if statement and basically say..

For %letter% 
robocopy C:\ E:\ %letter%.txt

Or like an IF statement, referring to a list.txt file.. whereas list.txt has all letters of alphabet in it..

IF %letter% in (C:\list.txt)
then robocopy C:\ E:\ %letter%.txt
brad barnes
  • 15
  • 1
  • 5
  • Edited to correct syntax, I have brackets on mine instead of quotes. The script I have is similar to this, and works, I just typed that up quickly as an example. I'm really just curious if I can accomplish the same goal with less code, rather than having to type out a line for every letter. – brad barnes Feb 19 '20 at 14:20
  • 2
    You were comparing two completely different things, `if /i %letter%=="A"` could never be correct, because only one side of the comparison had doublequotes. However, you've now edited your question to correct the syntax, but decided to use a poor implementation of the above, `if /i [%letter%]==[A]`. As you're using `Set /P` for user input, there is no control over that input, for that reason you should stick with the doublequotes instead, `if /i "%letter%"=="A"`, as it will protect most invalid characters within the input content. – Compo Feb 19 '20 at 14:26
  • Can you please explain your real task, because my assumption is that this isn't about creating an output file named with the input, but to select a mounted volume's drive letter for the `robocopy` command. – Compo Feb 19 '20 at 14:43
  • Script uses diskpart to assign volume letters, then allows the user to input text. It then checks the text against a list, say fruit [banana, apple, orange], if the user inputs a fruit from the list, it will copy the file corresponding to the fruit they entered, from the shared drive to the USB drive. if %userinput% is apple, it sees apple in the list, and copies %userinput%.exe from \\shared drive\folder to USB drive E:\ – brad barnes Feb 19 '20 at 14:58
  • 1
    It seems as if your question is irrelevant to your actual task then. I would suggest that you provide a more realistic example of your script, with the actual task, as a new question, then at least we could help you to structure it more appropriately and efficiently, trying to put together many modified generic examples, to create a fully functioning script is usually not the best way to go about achieving most tasks. Also can you guarantee that the USB strorage device will always be assigned to `E:` and that the network drive is available with appropriate permissions for your specific users. – Compo Feb 19 '20 at 15:09

2 Answers2

1

Basically you look for a way to ensure, the input is within a predefined set. You can do that like:

@echo off
setlocal
:loop
set /p "letter=Enter a letter of the alphabet: "
echo %letter%|findstr /ix "a b c d e f g h i j k l m n o p q r s t u v w x y z" || goto :loop
ECHO robocopy C:\ E:\ %letter%.txt

(remove the uppercase ECHO after testing to actually enable the robocopy command)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Stephan, are there any caveats to shortening it to this: `findstr /IRX "^[a-z]$"`? – Squashman Feb 19 '20 at 14:33
  • The biggest issue with that example is not with the `findstr` match, _which could be modified_, it's with the `echo`. If the end user enters a poison character, because there's no control over their input, it may fail at that stage. – Compo Feb 19 '20 at 14:37
  • @Squashman: I [don't fully trust findstr](https://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman), and inded, your's also accepts German Umlauts (and probably some other non-english characters too) – Stephan Feb 19 '20 at 14:52
  • Thanks. This is what I was after. – brad barnes Feb 19 '20 at 16:45
  • @brad: you can also check against a file (might make sense after reading your comment about "fruits") with `findstr /ixg:list.txt` – Stephan Feb 19 '20 at 21:31
  • See [Why does findstr not handle case properly in some circumstances?](https://stackoverflow.com/a/8767815/12861751) – ScriptKidd Feb 22 '20 at 13:33
1

A simple answer:

@echo off
set /p "letter=Enter a letter of the alphabet: "
for %%x in (a b c d e f g h i j k l m n o p q r s t u w x y z) do if /i "%letter%" equ "%%x" robocopy C:\ E:\ %%x.txt

However, it can't handle poison characters, but much safer than findstr.

Stephan
  • 53,940
  • 10
  • 58
  • 91
ScriptKidd
  • 803
  • 1
  • 5
  • 19