gpt4 book ai didi

windows - 按文件名的一部分将文本文件排序到文件夹中

转载 作者:行者123 更新时间:2023-12-02 23:06:45 26 4
gpt4 key购买 nike

我在一家律师事务所工作,我们有一个流程可以按特定的方式吐出带有特定信息的文本文件。
可以说是这样的:
1111.xxxxx_2222
我需要基于与“x”部分匹配的字符创建一个文件夹,并将“x”部分中具有相同匹配字符的文本文件重复移动到该文件夹​​中。一天有数百个我们需要经历,所以手工做起来会有些乏味。
我已经试过了:

$Source = 'Source folder'

(Get-ChildItem -Path $Source -File) | ForEach-Object {
$destination = Join-Path -Path $Source -ChildPath $_.BaseName

if(!(Test-Path $destination)){
New-Item -ItemType Directory -Path $destination | Out-Null
}
$_ | Move-Item -Destination $destination
}
我保留了源文件夹,因为它是我可以编辑的网络位置。
该脚本不够具体。它会根据它们的全名对它们进行排序,并且只会将它们移动到相同名称的文件夹中。它不能再次运行,因为该名称可能已经创建,在我的情况下将不起作用。我对 shell 供电非常新,并且可以在基本级别上进行编码,因此感谢您提供任何帮助!

最佳答案

您可以使用delay-bind script block:

$Source = 'Source folder'

Get-ChildItem -Path $Source -File | Move-Item -WhatIf -Destination {
# Derive the target dir. from the input filename.
$dirPath = Join-Path $Source ($_.BaseName -split '[._]')[1]
# Make sure that the target dir. exists. Note that the
# -Force switch ensures that no error occurs if the dir. already exists.
$null = New-Item -Type Directory -Force $dirPath
$dirPath # Output the target directory path.
}
请注意 -WhatIf common parameter的使用,它预览了移动操作,但是请注意,只有文件的移动才被预览;目标目录的创建仍然发生,因为即使在存在 WhatIf的情况下,也必须运行delay-bind脚本块,以便能够显示可能的目标路径。
一旦预览显示移动将按预期进行,请从 -WhatIf调用中删除 Move-Item($_.BaseName -split '[._]')[1]是使用基于正则表达式的 string splitting operator从文件(基本)名称(例如 xxxxx)中提取 111.xxxxx_2222的功能

关于windows - 按文件名的一部分将文本文件排序到文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62599323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com