Reputation: 15
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
Upvotes: 0
Views: 114
Reputation: 841
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.
Upvotes: 1
Reputation: 56180
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)
Upvotes: 1