Powershell中的文件和文件夹操作 [英] File and Folder Manipulation in Powershell

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

问题描述

我有一个包含数千个文件名的文件夹.

I have a folder of thousands of filenames.

所有文件名均相对于用户的userID.某些用户在此文件夹中有多个文件,因此它将在名称的末尾附加一个数字.IE.susy.txt,susy1.txt,susy2.txt,carl.txt,carl1.txt等...

All filenames are relative to a user's userID. Some users have multiple files in this folder, so it appends a number onto the end of the name. I.E. susy.txt, susy1.txt, susy2.txt, carl.txt, carl1.txt, etc...

我想做的是为具有多个文件的每个用户创建一个特定的文件夹,然后将所有关联的文件移动到该文件夹​​中.因此,我注意到有多个可疑文件.因此,我想创建一个名为Susy的文件夹,并将susy.txt,susy1.txt和susy2.txt放入其中……对于所有文件,依此类推.

What I am trying to do is create a specific folder for each user that has multiple files, and move all associated files into that folder. So I notice there are multiple susy documents. So I want to create a folder named Susy and place susy.txt, susy1.txt, and susy2.txt into it... And so on for all files.

是否甚至可以将其作为批处理文件进行处理,如果可以的话,有人可以指出正确的方向吗?我在编写批处理脚本方面知识很少,并希望以此为契机学习更多信息.

Is it even possible to do this as a batch file, if so can someone point me in the correct direction on doing this? I have a small amount of knowledge in writing batch scripts, and would like to take this as an opportunity to learn more.

谢谢

推荐答案

这是一种更简单/更紧凑的方法:

Here's a simpler/more compact way of doing it:

Get-ChildItem | ?{$_.Name -match '(\D+)\d*\.txt'} | %{
  md $matches[1] -ea SilentlyContinue 
  Move-Item $_ $matches[1]
}

一个纯粹主义者可能会说,在尝试创建目录之前,对 Test-Path 来说是更好的实践",但是我通常更喜欢紧凑的代码.如果没有其他决定取决于该目录是否已经存在,则每次尝试创建该目录并忽略错误会更快.

A purist might say that it's "better practice" to Test-Path before attempting to create the directory, but I generally prefer more compact code; if no other decisions hinge on whether the directory already exists, it's quicker to just attempt to create it each time and ignore errors.

针对OP的评论,有一些解释性注释:

A couple of explanatory notes, in response to the OP's comment:

  • ?{} Where-Object 的简写,它根据条件进行过滤,即,仅将满足指定条件的对象向下传递到管道.因此,在这种情况下,它将获取当前目录的列表,并仅选择其 Name 属性与正则表达式匹配的对象.
  • 使用 -match 运算符后,匹配项将存储在自动数组变量 $ matches 中. $ matches [0] 包含字符串的整个匹配部分,以1开头的索引包含匹配组,从左括号的序号开始编号,即 $ matches [1]是从遇到的第一个左括号开始的匹配组, $ matches [2] 是从第二个左括号开始的匹配组,等等.

  • ?{ } is shorthand for Where-Object, which filters on a condition, i.e. only objects that meet the specified condition are passed down the pipeline. So in this case, it takes the listing of the current directory and selects only the objects whose Name property matches the regex.
  • After the -match operator is used, the matches are stored in the automatic array variable $matches. $matches[0] contains the entire matched part of the string, and indices starting with 1 contain the match groups, numbered from the ordinal position of the left parenthesis, i.e. $matches[1] is the match group beginning with the first left parenthesis encountered, $matches[2] is the match group beginning with the second left parenthesis, etc.

在这种情况下,只有一个匹配组, $ matches [1] 包含由(\ D +)匹配的字符串部分,即在第一个数字或点之前的连续非数字字符.

In this case there is only one match group, and $matches[1] contains the part of the string matched by (\D+), i.e. the sequence of consecutive non-digit characters prior to the first digit or dot.

这篇关于Powershell中的文件和文件夹操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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