仅复制子文件夹中的最新文件 [英] Copy only the latest file from subfolders

查看:196
本文介绍了仅复制子文件夹中的最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含1000多个文档文件夹的文件夹;每个doc文件夹都有1个或多个版本的文档(可以是excel,word,html类型)。



如何使用PowerShell仅将子文件夹中的最新版本复制到单个文件夹中,以便我可以执行PowerGREP搜索?



获取文件的初始文件夹后;有没有简单的方法来维护它?



非常感谢任何帮助!



我尝试过:



搜索Google&在Code Project上查看了这个帖子:使用Microsoft Powershell进行文件复制

解决方案

一位同事找到了这个有用的网站:主题:每个子目录中最近修改过的文件的返回日期| PowerShell.org [ ^ ]



他写了以下解决方案。它不排除文件夹,但会遍历文件夹并复制出最新文件:



#识别要搜索的文件夹

projectsFolder ='C:\ Users \ widewide \Documents \DELETE\ACE增强'

#获取被搜索文件夹的子文件夹并找到最近修改过的文件

#按最后修改日期按降序排序这些文件

#将它们复制到新文件夹并选择Name,LastWriteTime和FullName属性并编写它们到csv #file

Get-ChildItem -LiteralPath


projectsFolder -Directory -Force | ForEach-Object {

#获取每个子文件夹下的所有文件,按最后修改日期降序排序,选择第一个(最近修改过的文件)并返回

Get-ChildItem -LiteralPath


I have a folder containing 1000+ document folders; each doc folder has 1 or more versions of the document (can be excel, word, html types).

How can I use PowerShell to copy only out the most current version from the sub-folders into a single folder so that I can perform PowerGREP searches?

After I get the initial folder of files; is there an easy way to maintain it?

Any help is greatly appreciated!

What I have tried:

Searched Google & looked at this thread on Code Project: File copy using Microsoft Powershell

解决方案

A co-worker found this helpful site: Topic: Return date of most recently modified file in each sub-dir | PowerShell.org[^]

He wrote the following solution. It doesn't exclude folders, but does traverse folders and copy out the most current file:

# Identify the folder you want to search


projectsFolder = 'C:\Users\swidene\Documents\DELETE\ACE Enhancement'
# Get the subfolders of the folder being searched and find the most recently modified file
# Sort those files by last modification date in descending order
#copy them to new folder and select the Name, LastWriteTime, and FullName properties and write them to the csv #file
Get-ChildItem -LiteralPath


projectsFolder -Directory -Force | ForEach-Object {
# Get all files under each subfolder, sorted by the last modification date in descending order, select the first one (the most recently modified file) and return it
Get-ChildItem -LiteralPath


这篇关于仅复制子文件夹中的最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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