Batch File - Copying contents of text file to clipboard

与世无争的帅哥 提交于 2019-12-13 21:04:15

问题


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.
    )


来源:https://stackoverflow.com/questions/19437749/batch-file-copying-contents-of-text-file-to-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!