簡體   English   中英

如何在命令行上將文件(不是內容)復制/剪切到 Windows 中的剪貼板?

[英]How to copy/cut a file (not the contents) to the clipboard in Windows on the command line?

有沒有辦法從命令行將文件復制(或剪切)到 Windows 剪貼板?

特別是使用批處理腳本。 我知道如何將內容復制到剪貼板( type file | clip ),但事實並非如此。 我想要整個文件,就像在 Windows 資源管理器中按Ctrl + C 一樣

好吧,似乎最簡單的方法是創建一個小的 C# 工具,它接受參數並將它們存儲在剪貼板中:

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace File2Clip
{
    public class App
    {
        [STAThread]
        static void Main(string[] args)
        {
            List<string> list = new List<string>();

            string line;
            while(!string.IsNullOrEmpty(line = Console.ReadLine())) list.Add(line);
            foreach (string s in args) list.Add(s);

            StringCollection paths = new StringCollection();
            foreach (string s in list) {
            Console.Write(s);
                paths.Add( 
                    System.IO.Path.IsPathRooted(s) ? 
                      s : 
                      System.IO.Directory.GetCurrentDirectory() + 
                        @"\" + s);
            }
            Clipboard.SetFileDropList(paths);
        }
    }
}

2017 年編輯:這是一個包含源代碼和二進制文件的github 存儲庫

這會將文件的內容放入剪貼板(由clip.exe 完成)。

type \path\to\file|clip

要獲得實際文件,您可能不得不求助於其他一些編程語言,如 VBScript 或 PowerShell 來訪問 Windows API。 當您按 CTRL+C 一個文件時,我並不完全確定 Explorer 將什么放入剪貼板。 我懷疑它使用通知系統來做一些比將文件路徑放在那里更智能的事情。 根據 CTRL+V 的上下文,您會得到一些東西(資源管理器、Word)或什么都沒有(記事本)。

我一直希望在 Emacs 中使用它,因此,受到這個問題、這里的答案以及大量 NIH 綜合症的啟發,我編寫了一個 C 版本,可在

https://github.com/roryyorke/picellif

picellif 還處理通配符(我不清楚 rostok 的 C# 版本是否支持)。

你可以試試瑞士銼刀(SFK):


sfk toclip
 Copy stdin to clipboard as plain text.

    type test.txt | sfk toclip
       Copies the content of ASCII file test.txt into the clipboard.

    sfk list | sfk toclip
       Copies a file listing of the current dir into the clipboard.

sfk fromclip [-wait] [-clear]

 Dump plain text content from the clipboard to the terminal.

   -wait : block until plain text is available.
   -clear: empty the clipboard after reading it.

示例:將反斜杠變成正斜杠。 假設您在Notepad 中打開了以下文本:

foo/bar/systems/alpha1.cpp
foo/bar/systems/alpha2.cpp
foo/bar/systems/beta1.cpp

出於某種原因,您需要像這樣格式的第一行:

foo\bar\systems\alpha1.cpp

那么你可以這樣做:

  1. 使用 SHIFT + CURSOR 鍵標記第一行。
  2. Ctrl + CCtrl + Insert將其復制到剪貼板
  3. 在 Windows 命令行上,運行以下命令(例如,從批處理文件中):

     sfk fromclip +filter -rep x/x\\x +toclip
  4. 返回編輯器,按Ctrl + VShift + Insert,粘貼剪貼板中的結果。

如您所見,該行更改為“foo\\bar\\systems\\alpha1.cpp”。

copymove分別是(部分)復制/粘貼和剪切/粘貼文件的批處理命令。 我們在處理文件時不使用術語粘貼或剪切,但如果我了解您,需要將文件復制到另一個位置並將文件移動到另一個位置。

我也遇到過類似的問題,為此我寫了一個ahk腳本,需要Gdip庫來編譯

CopyToClip.ahk:

fileName := %0%

if( !FileExist(fileName) )
{
    MsgBox, %fileName% not found!
    return
}

;MsgBox, %fileName%
file := FileOpen(fileName, "r")
if( ErrorLevel )
{
    MsgBox, Open %fileName% error!
    return
}
encoding := File.Encoding
fileSize := File.Length


; Read as text?
readAs := "unkonw"
SplitPath, fileName, name, dir, ext, name_no_ext, drive
if( ext == "txt" || ext == "md" || ext == "bat" || ext == "cmd" || ext == "ahk" || ext == "ini" || ext == "sfo")
{
    readAs := "text"
}
else if( ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "xbt" || ext == "img" )
{
    readAs := "image"
}
else if( fileSize > 10240 )
{
    MsgBox, Unsupport file type %ext%
    return
}
else
{
    ToolTip, Unsupport file type %ext%`, but read as text.
    readAs := "text"
}
;MsgBox, %readAs%

if( readAs == "text" )
{
    fileContent := File.Read()
    Clipboard := fileContent
}
else if( readAs == "image" )
{
    pToken := Gdip_Startup()
    Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromFile(fileName))
    Gdip_DisposeImage(pBitmap)
    Gdip_Shutdown(pToken)
    ;file.RawRead(fileContent, fileSize)
}else
{
    MsgBox, Unkonw Error!
    return
}

if( ErrorLevel )
{
    MsgBox, Read %fileName% error!
    return
}

ToolTip, %fileName%`nSize: %fileSize%
Sleep, 400
ExitApp
/*
MsgBox, 
(
%encoding%,
%fileSize%,
-----------------------------
%fileContent%
)
*/

編譯后只需要CopyToClip.exe <yourfilename>將文件內容復制到剪貼板,目前支持文本和圖像

最后,我們可以在右鍵菜單上添加快捷菜單

您需要將D:\\\\Program Files\\\\CopyToClip\\\\CopyToClip.exe為您自己的路徑

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*]

[HKEY_CLASSES_ROOT\*\shell]

[HKEY_CLASSES_ROOT\*\shell\aCopy to Clip]
"Icon"="C:\\Windows\\System32\\imageres.dll,14"
@="Copy to Clip"

[HKEY_CLASSES_ROOT\*\shell\aCopy to Clip\Command]
@="D:\\Program Files\\CopyToClip\\CopyToClip.exe \"%1\""

Windows Powershell 與 Linux 的 cp 幾乎相同

您應該查看Windows Powershell ,您可以使用cp命令。

Windows 中的簡單命令行復制

  1. 通過在cmd鍵入powershell打開 Powershell。
  2. 然后做這個cp path_to_file/folder_to_copy path_to_destination

一些例子

將文件復制到您所在的目錄中!

cp path_to_file .

這里的. 表示當前目錄。

復制到受保護的目錄

如果要將文件復制到受限文件夾中,則需要管理員權限

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM