从文本创建文件和文件夹

马库斯

我有一个关于如何从文本创建文件的问题,例如,我有$source = "C:\folder1\folder11\file1.txt"目的地是目的地$destination = "C:\folder2\folder22\file2.txt"的文件和文件夹尚不存在。

我使用这个命令powershell:

$source = "C:\folder1\folder11\file1.txt"
$destination = "C:\folder2\folder22\file2.txt"

Copy-Item $source -Destination  (New-Item -ItemType File -Path $destination -Force) -Force

此命令创建文件:file2.txt和文件夹:C:\folder2\folder22\但有错误:

找不到路径的一部分

你知道我该如何解决这个错误吗?

拉纳迪普·杜塔

如果源是目录,-recurse 开关将创建目标文件夹层次结构。

当源是一个文件时,Copy-Item期望目标是一个已经存在的文件或目录。

所以,做这样的事情:

$source = "C:\folder\String.txt"
$destination = "C:\folder2\folder22\file2.txt"

if(!(Test-Path $destination))
{
New-Item (Split-Path -Path $destination) -ItemType Directory -Force
}
Copy-Item -Path $source -Destination $destination -Force

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章