Batch File - Copying contents of text file to clip

2019-09-02 08:32发布

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.

2条回答
爷的心禁止访问
2楼-- · 2019-09-02 08:56

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.
    )
查看更多
我命由我不由天
3楼-- · 2019-09-02 09:05

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
查看更多
登录 后发表回答