PowerShell创建一个带有零尺寸文件的重复文件夹
但它并不能保存所有我想要的信息:
robocopy D:\documents E:\backups\documents_$(Get-Date -format "yyyyMMdd_HHmm")\ /mir /create
我想创建一组文件夹的0 filesize镜像映像,但是虽然Robocopy确实很好, 重复文件夹中的文件的大小为零,这很好,但是我希望重复文件夹中的每个文件都具有[size]
附加到名称末尾的,size in kb in MB或GB,以及每个文件上的创建 /最后修改时间,以与原始文件完全匹配。这样,我将拥有可以存档的文件夹的零尺寸重复,但是其中包含该目录中文件的所有相关信息,显示每个文件的大小以及确切的创建 /最后修改时间。
是否有良好 /简单的方法可以通过PowerShell中的树迭代,并且对于每个项目,都可以创建一个带有所有相关信息的零尺寸文件?
I want to create a 0-filesize mirror image of a set of folder, but while robocopy is really good, it doesn't save all of the information that I would like:
robocopy D:\documents E:\backups\documents_$(Get-Date -format "yyyyMMdd_HHmm")\ /mir /create
The /create
switch makes each file in the duplicate folder have zero-size, and that is good, but I would like each file in the duplicate folder to have [size]
appended to the end of the name with the size in KB or MB or GB, and the create / last modified time on every file to exactly match the original file. This way, I will have a zero-size duplicate of the folder that I can archive, but which contains all of the relevant information for the files in that directory, showing the size of each and the exact create / last modified times.
Are there good / simple ways to iterate through a tree in PowerShell, and for each item create a zero size file with all relevant information like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用我在评论中提到的方法实现复制命令的一种方法。这应该给您一些一些想法。我不打算在它上花费那么多时间,但是我在几个目录上运行了它,发现一些问题并调试了我遇到的每个问题。在这一点上,这是一个相当可靠的例子。
注意:上面的实现很简单,代表任何不是文件目录的内容。这意味着符号联系等。将是文件,没有信息,他们将链接到什么。
这是一个函数,可以将转换从字节数到NN B/K/M/G格式。要获取更多小数位置,只需在格式字符串的末端添加
0
。人们通常会弄错这些转换。例如,使用1024 * 1000获取Megabytes(将1K的基本10值与1K的base2值混合)并遵循相同的逻辑以获取GB和TB是一个常见的错误。
This would be one way to implement the copy command using the approach I mentioned in the comments. This should give you something to pull ideas from. I didn't intend to spend as much time on it as I did, but I ran it on several directories and found some problems and debugged each problem I encountered. This is a pretty solid example at this point.
Note: the above implementation is simplistic and represents anything that isn't a directory as a file. That means symbolic links, et al. will be files with no information what they would be linked to.
Here's a function to get the conversion from number of bytes to N.N B/K/M/G format. To get more decimal places, just add
0
's to the end of the format strings.Often, people get these conversions wrong. For instance, it's a common error to use 1024 * 1000 to get Megabytes (which is mixing the base10 value for 1K with the base2 value for 1K) and follow that same logic to get GB and TB.
这是我提出的问题中提出的其他部分,请根据需要更改
$ src
/$ dst
我保留大量虚拟机)。我包括设置所有创建时间,持久时间,最后一个访问时间,以便具有零尺寸文件的备份位置是源的完美表示。由于我想将其用于档案目的,因此我终于将其拉开了,并在Zipfile名称中包含了日期时间邮票。Here is what I came up with with the additional parts in the question, change
$src
/$dst
as required (D:\VMs
is where I keep a lot of Virtual Machines). I have included setting all of CreationTime, LastWriteTime, LastAccessTime so that the backup location with zero-size files is a perfect representation of the source. As I want to use this for archival purposes, I have finally zipped things up and included a date-time stamp in the zipfile name.