将单个文件复制到不存在的文件夹 [英] Copy a single file to a non-existing folder

查看:73
本文介绍了将单个文件复制到不存在的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很容易将多个文件复制到一个不存在的文件夹并让其创建:

It's easy to copy multiple files to a folder that doesn't exists and let it be created:

Copy-Item C:\Temp\aa C:\Temp2\DoesNotExist

上面的命令将创建文件夹DoesNotExist.这就是我所追求的.

The command above will create the folder DoesNotExist. That's what I'm after.

但是当源只是单个文件时,PowerShell 语法是什么?

But what is the PowerShell syntax to the same when the source is only a single file?

Copy-Item C:\Temp\test.txt  C:\Temp2\DoesNotExist

我尝试了 C:\Temp2\DoesNotExist\(带有斜杠),但 Powershell 显示文件名、目录名或卷标语法不正确."并拒绝复制单个文件.

I tried C:\Temp2\DoesNotExist\ (with the trailing slash), but Powershell says "The filename, directory name, or volume label syntax is incorrect." and refuses to copy the single file.

推荐答案

如果您正在寻找一个单一的班轮解决方案,您可以这样做.

If you're looking for a one liner solution, you can do this.

copy "C:\test2.txt"  -Destination (New-Item "C:\Temp2\DoesNotExist\" -Type container -force) -Container -force

这篇关于将单个文件复制到不存在的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆