Batch File - Copying contents of text file to clip

2019-09-02 08:49发布

问题:

I'm attempting to write a basic batch file to help with some tasks at work. Current iteration is as follows:

'@echo off
echo 1: Follow-Up Email
echo 2: Things
echo 3: Stuff

set /p "option=Select Option:"

IF %option%==1(
    type "C:\files\test.txt" | clip
    )
ELSE IF %option%==2(    
    type "C:\files\test2.txt" | clip
    )
ELSE IF %option%==3(
    type "C:\files\test3.txt" | clip
    )
ELSE (
    echo Not a valid option.
    )   '

Situation: text.txt contains the string "text123." text2.txt contains the string "test2." etc. My problem is the script simply does nothing. No text is put to the clipboard. What simple thing(s) am I missing?

Thanks much in advance.

回答1:

Your else if syntax is the problem. How about this?

@echo off

:Start
cls
echo 1: Follow-Up Email
echo 2: Things
echo 3: Stuff

set /p "option=Select Option:"

IF "%option%"=="1" type "C:\files\test.txt" | clip & goto :Done
IF "%option%"=="2" type "C:\files\test2.txt" | clip & goto :Done
IF "%option%"=="3" type "C:\files\test3.txt" | clip & goto :Done
echo.Not a valid option.
pause
goto :Start

:Done


回答2:

Your code formatting was fubar.
The spaces before the ( in the if tests were missing,
and the placement of ELSE must be on the same line as the ) and (

It's not as robust as it could be, but it works.

@echo off
echo 1: Follow-Up Email
echo 2: Things
echo 3: Stuff

set /p "option=Select Option:"

IF %option%==1 (
    type "C:\files\test.txt" | clip
    ) ELSE IF %option%==2 (    
    type "C:\files\test2.txt" | clip
    ) ELSE IF %option%==3 (
    type "C:\files\test3.txt" | clip
    ) ELSE (
    echo Not a valid option.
    )