文件名唯一部分的powershell列表 [英] powershell list of unique parts of filename

查看:331
本文介绍了文件名唯一部分的powershell列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多台机器将文件上传到一个FTP目录.文件名的第一部分是机器,其余部分是时间戳,例如AAAAA_20130312_125113.

I have multiple machines uploading files to one FTP directory. The first part of the filename is the machine, the rest is a timestamp, e.g. AAAAA_20130312_125113.

现在,我要获取已上传到此目录的所有唯一计算机的排序列表. 我设法将丢失的所有filenames.substring(0,5)写入主机,但是我仍然没有唯一的计算机名称.

Now I want to get a sorted list of all Unique machines that have uploaded to this directory. I managed to write the lost of all filenames.substring(0,5) to the host but I still don't have the unique machine names.

$files=Get-ChildItem $strMOVETO -Name -Include TAS*.csv -Recurse
ForEach ($i in $files) { Write-Host $i.Substring(0,5) }

关于如何执行此操作的任何提示?不必一定要是一个班轮,尽管那是一个很好的挑战;-).

Any hints on how to do this? Does not necessary have to be a one liner, although that would be a nice challenge ;-).

谢谢!

推荐答案

当您使用8个字符的计算机名称时会发生什么?您的substring将中断.由于机器名称,日期&时间用_分隔,并在该&上分开.得到第一项.

What happens when you have an 8-character machine name? Your substring will break. Since the machine name, date & time are delimited by an _, split on that & get the first item.

Get-ChildItem $strMOVETO -recurse -name -include TAS*.csv|%{$_.split("_")[0]}|sort-object -unique

要同时过滤日期,请执行以下操作:

To filter on date as well:

Get-ChildItem $strMOVETO -recurse -include TAS*.csv|where-object{$_.lastwritetime -ge (get-date).adddays(-1)}|%{$_.basename.split("_")[1]}|sort-object -unique

这篇关于文件名唯一部分的powershell列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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