繁体   English   中英

如何组合 Windows 路径和通配符?

[英]How can I combine Windows paths and wildcards?

我的剪贴板中经常有一个 Windows 绝对路径(例如C:\\test ),我想将它粘贴到git-bash 如果我将路径放在单引号中,这通常有效:

$ ls 'C:\test'
bar.txt   foo.txt  baz.exe  dat.exe

我的问题是当我尝试向路径添加通配符时:

$ ls 'C:\test\*.txt'
ls: cannot access 'C:\test\*.txt': No such file or directory

$ ls 'C:\test\'*.txt
ls: cannot access 'C:\test\*.txt': No such file or directory

我知道如果我将路径转换为 ​​Unix 样式,即/c/test/*.txt ,它会起作用,但这不是很方便,因为我已经在剪贴板中有 Windows 路径。

有任何想法吗?

我最终用 C# 编写了一个控制台应用程序,它在 Windows 和git-bash之间切换剪贴板中的路径。 我只是在剪贴板中找到一个路径,运行这个应用程序,它会更新剪贴板以具有相反的样式路径。 那我就贴吧。

using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SwitchPath
{
    internal static class Program
    {
        private const RegexOptions Options =
            RegexOptions.IgnoreCase | RegexOptions.CultureInvariant |
            RegexOptions.Multiline;

        private static readonly Regex WinPath = new Regex(
            @"^([a-z]):\\(.+)$", Options);

        private static readonly Regex UnixPath = new Regex(
            @"^/([a-z])/(.+)$", Options);

        [STAThread]
        private static void Main()
        {
            var path = Clipboard.GetText().Trim();
            string drive, rest;

            var m = WinPath.Match(path);
            if (m.Success)
            {
                drive = m.Groups[1].Value.ToLowerInvariant();
                rest = m.Groups[2].Value.Trim().Replace(@"\", "/");
                path = $"/{drive}/{rest}";
                Clipboard.SetText(path);
                return;
            }

            m = UnixPath.Match(path);
            if (m.Success)
            {
                drive = m.Groups[1].Value.ToUpperInvariant();
                rest = m.Groups[2].Value.Trim().Replace("/", @"\");
                path = $@"{drive}:\{rest}";
                Clipboard.SetText(path);
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM