2

I am writing a batch script for a friend, it needs one input which is a website url. They're not the most super of users, so I want to make it as dirt simple as possible; I want them to copy the url into their clipboard and just click the script.bat file and be good.

.\executable.exe InsertClipboardHere -a --million "arguments"

Is there a way to access the contents of the clipboard from a batch script, without any "advanced" utilities like windows powershell or external programs?

  • 3
    No way in cmd natively. Need powershell or 3rd party tool unfortunately. You can write to the clipboard with clip.exe but not pull from it – Narzard Aug 12 at 4:24
0

Try:

.\executable.exe paste -a --million "arguments"

Try: Example 1 Example 2

Command Variations: paste | command

Paste the contents of the Windows Clipboard into the input of the specified command command.

paste | command | clip

Paste the contents of the Windows Clipboard into the input of the specified command command then copy its output to the Windows Clipboard.

For example, paste | sort | clip sorts the contents of the Windows Clipboard. Requires: clip

paste > filename

paste > example.txt

Pastes the contents of the Windows Clipboard into the specified filename file.

paste | sort > filename

Sorts the contents of the Windows Clipboard and saves sorted output to the specified filename file.

0

Obs.: - This answer is intended for users who want something like this and also no longer want to download string tools.


1) I used this solution in this kind of work, but obviously this is not simple either, besides the use of external tools available in the system.

2) Basically, use C# code to do the to paste at run time.


The @Narzard comment is 100% correct, I spent a lot of time trying the same as you ask, but I found no solution to do it as simply as possible ... this is the solution I decided to implement in my Bat code:


@echo off & setlocal EnableDelayedExpansion

set "_out=%temp%\Paste.cs" & set "_csc="%windir%\Microsoft.NET" "csc.exe""
set _arg=/target:exe /out:"!_out:~0,-2!exe" "!_out!" /platform:anycpu & > "!_out!" (
echo/using System^;using System^.Windows^.Forms^;using System^.Threading^;namespace Huddled^{
echo/public class Paste^{[STAThread]static void Main^(^)^{foreach^(string line in Clipboard.GetText^(^).Split^(
echo/new string[]^{"\r\n","\n"^},StringSplitOptions.None^)^)Console.WriteLine^(line^)^;^}^}^}) 

for /f %%i in ('%__appdir__%\where /r !_csc!')do "%%~i" !_arg! >nul && del /q /f "!_out!" & goto :_next

:_next
for /f tokens^=* %%i in ('"!_out:~0,-2!exe"')do .\executable.exe "%%~i" -a --million "arguments"
del /q /f "!_out:~0,-2!exe" 2>nul & rem: continue from here!

C# without escaping characters tested in .NET Framework x86/x64:

2.0.50727

3.5

4.0.30319

using System;
using System.Windows.Forms;
using System.Threading;

namespace Huddled

{
  public class Paste 
    {
      [STAThread] static void Main() 
        {
          foreach(string line in Clipboard.GetText().Split(new string[]{"\r\n","\n"},StringSplitOptions.None))Console.WriteLine(line);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.