Powershell 2 复制项目,如果不存在则创建文件夹
$from = "\\something\1 XLS\2010_04_22\*"
$to = "c:\out\1 XLS\2010_04_22\"
copy-item $from $to -Recurse
如果 c:\out\1 XLS\2010_04_22\
确实存在,则此方法有效。如果 c:\out\1 XLS\2010_04_22\
不存在,是否可以使用单个命令来创建它?
$from = "\\something\1 XLS\2010_04_22\*"
$to = "c:\out\1 XLS\2010_04_22\"
copy-item $from $to -Recurse
This works if c:\out\1 XLS\2010_04_22\
does exist . Is it possible with a single command to create c:\out\1 XLS\2010_04_22\
if it doesn't exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在 PowerShell 2.0 中,仍然无法使用 Copy-Item cmdlet 来创建目标文件夹,您将需要如下代码:
如果您在 Copy-Item 中使用 -Recurse ,它将创建源结构的所有子文件夹在目标中,但即使使用 -Force,它也不会创建实际的目标文件夹。
In PowerShell 2.0, it is still not possible to get the Copy-Item cmdlet to create the destination folder, you'll need code like this:
If you use -Recurse in the Copy-Item it will create all the subfolders of the source structure in the destination but it won't create the actual destination folder, even with -Force.
是的,添加
-Force
参数。Yes, add the
-Force
parameter.在 PowerShell 3 及更高版本中,我将 Copy-Item 与 New-Item 一起使用。
我还没有在版本2中尝试过。
In PowerShell 3 and above I use the Copy-Item with New-Item.
I haven't tried it in ver 2.
这是一个对我有用的例子。我在一个文本文件中列出了大约 500 个特定文件,这些文件包含在大约 100 个不同的文件夹中,我应该将其复制到备份位置,以备以后需要这些文件时使用。该文本文件包含完整路径和文件名,每行一个。就我而言,我想从每个文件名中删除驱动器盘符和第一个子文件夹名称。我想将所有这些文件复制到我指定的另一个根目标文件夹下的类似文件夹结构中。我希望其他用户觉得这有帮助。
Here's an example that worked for me. I had a list of about 500 specific files in a text file, contained in about 100 different folders, that I was supposed to copy over to a backup location in case those files were needed later. The text file contained full path and file name, one per line. In my case, I wanted to strip off the Drive letter and first sub-folder name from each file name. I wanted to copy all these files to a similar folder structure under another root destination folder I specified. I hope other users find this helpful.
我最喜欢的是使用 .Net [IO.DirectoryInfo] 类,它负责一些逻辑。我实际上用它来应对很多类似的脚本挑战。它有一个 .Create() 方法,可以创建不存在的目录,即使存在也不会出现错误。
由于这仍然是一个两步问题,因此我使用 foreach 别名来保持简单。对于单个文件:
就多文件/目录匹配而言,我会使用 RoboCopy 而不是 xcopy。从 from 中删除“*”,然后使用:
您仍然可以添加 /r(递归)、/e(递归,包括空),还有 50 个其他有用的开关。
编辑:回顾一下,它很简洁,但如果您不经常使用该代码,则可读性不是很好。通常我将它分成两部分,如下所示:
此外,DirectoryInfo 是 FileInfo 的 Parent 属性的类型,因此如果您的 $to是一个文件,你可以一起使用它们:
My favorite is to use the .Net [IO.DirectoryInfo] class, which takes care of some of the logic. I actually use this for a lot of similar scripting challenges. It has a .Create() method that creates directories that don't exist, without errors if they do.
Since this is still a two step problem, I use the foreach alias to keep it simple. For single files:
As far as your multi file/directory match, I would use RoboCopy over xcopy. Remove the "*" from your from and just use:
You can still add the /r (Recurse), /e (Recurse including Empty), and there are 50 other useful switches.
Edit: Looking back at this it is terse, but not very readable if you are not using the code often. Usually I have it split into two, like so:
Also, DirectoryInfo is the type of the Parent property of FileInfo, so if your $to is a file, you can use them together:
我在这里被绊倒了两次,最后一次是一个独特的情况,即使我放弃使用
copy-item
我想发布我使用的解决方案。只有具有完整路径的文件列表,并且在大多数情况下,文件没有扩展名。
-Recurse -Force
选项对我不起作用,所以我放弃了copy-item
功能,并使用 xcopy 回到如下所示的功能,因为我仍然想保留它衬垫。最初我与 Robocopy 联系在一起,但它显然是在寻找文件扩展名,并且由于我的许多文件没有扩展名,因此它认为它是一个目录。希望它能帮助某人。
I have stumbled here twice, and this last time was a unique situation and even though I ditch using
copy-item
I wanted to post the solution I used.Had a list of nothing but files with the full path and in majority of the case the files have no extensions. the
-Recurse -Force
option would not work for me so I ditchedcopy-item
function and fell back to something like below using xcopy as I still wanted to keep it a one liner. Initially I tied with Robocopy but it is apparently looking for a file extension and since many of mine had no extension it considered it a directory.Hope it helps someone.
我修改了 @FrinkTheBrave 的答案来创建子目录,并解析相对路径:
I modified @FrinkTheBrave 's answer to also create sub-directories, and resolve relative paths:
我以前用过这个:
new-item -path 'd:\path\to\destination' -name 'folder' && robocopy '.\source\folder\path\or\file' 'd:\path\to\destination\folder' /e
双与号 ( && ) 适用于命令提示符和 PowerShell。
它允许连续运行两个单独的命令。
I have used this before:
new-item -path 'd:\path\to\destination' -name 'folder' && robocopy '.\source\folder\path\or\file' 'd:\path\to\destination\folder' /e
the double ampersand ( && ) works for a command prompt and PowerShell.
It allows two separate commands to be run consecutively.