AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1760688
Accepted
J'e
J'e
Asked: 2023-01-03 12:18:10 +0800 CST2023-01-03 12:18:10 +0800 CST 2023-01-03 12:18:10 +0800 CST

如何从 PowerShell 运行 Windows 命令外壳“复制”?

  • 772

我正在尝试使用 PowerShell 连接一堆 1GB 的文件。通常我会在 Windows cmd.exe 中使用copy /b file.txt.0+file.txt.1 file.txt. 但是,我所在的工作站已禁用 cmd.exe。 如何在 PowerShell 中运行相同的命令?当我完全相同地运行它时,出现以下错误,

Copy-Item : A positional parameter cannot be found that accepts argument 'file.txt'.
At line:1 char:1
+ copy /c file.txt.0+file.txt.1 file.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

我也试过这个,但它似乎只是用最后一个输入文件覆盖输出,

copy -Path [file.txt.0, file.txt.1] -Destination file.txt
powershell
  • 2 2 个回答
  • 48 Views

2 个回答

  • Voted
  1. harrymc
    2023-01-03T12:28:40+08:002023-01-03T12:28:40+08:00

    我 在 如何在 PowerShell 中连接两个文本文件Get-Content?Set-Content

    这是完全引用的答案:

    只需使用Get-Content和Set-Contentcmdlet:

    Get-Content inputFile1.txt, inputFile2.txt | Set-Content joinedFile.txt
    

    您也可以使用这种样式连接两个以上的文件。

    如果源文件的名称相似,则可以使用通配符:

    Get-Content inputFile*.txt | Set-Content joinedFile.txt
    

    注 1: PowerShell 5 和更早版本允许分别使用别名catand scforGet-Content和 for 以及更简洁地完成此操作Set-Content。但是,这些别名是有问题的,因为cat在 *nix 系统中sc是系统命令,在 Windows 系统中是系统命令 - 因此不推荐使用它们,事实上sc什至不再定义为 PowerShell Core (v7)。PowerShell 团队一般不建议使用别名。

    注意 2:小心使用通配符 - 如果您尝试输出到inputFiles.txt(或与模式匹配的类似内容),PowerShell 将进入无限循环!(我刚刚测试了这个。)

    注意 3:输出到文件>不保留字符编码!这就是Set-Content推荐使用的原因。


    如果Get-Content和Set-Content对于大文件来说太慢,流是一个很好的选择。

    这是基于 此答案的(未经测试的)脚本:

    $rootPath = "C:\temp"
    $outputPath = "C:\test\somewherenotintemp.csv"
    $streamWriter = [System.IO.StreamWriter]$outputPath
    Get-ChildItem $rootPath -Filter "*.csv" -File  | ForEach-Object{
        $file = $_.BaseName
        [System.IO.File]::ReadAllLines($_.FullName) | 
            ForEach-Object {
                $streamWriter.WriteLine(('{0},"{1}"' -f $_,$file))
            }
    }
    $streamWriter.Close(); $streamWriter.Dispose()
    

    您将在链接的答案中找到更多详细信息。

    • 1
  2. Best Answer
    J'e
    2023-01-04T11:32:00+08:002023-01-04T11:32:00+08:00

    copy在 PowerShell 中的别名Copy-Item与 Dos 命令不同copy。follow 命令非常适合大文件,而且速度非常快。在我的例子中,我的 powershell 实例在连接 8x1GB 文件时使用了大约 42MB 的内存。CPU 使用率也很低,我的磁盘是瓶颈。

    cmd /c copy /b file.txt.0+file.txt.1 file.txt

    • 1

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve