标题: [文件操作] [已解决]根据文件夹中文件判断缺失情况对应输出? [打印本页]
作者: czvde 时间: 2022-5-29 18:04 标题: [已解决]根据文件夹中文件判断缺失情况对应输出?
本帖最后由 czvde 于 2022-5-30 11:07 编辑
文件夹文件如下:
0123ABCD___1.doc
0123ABCD___3.doc
ABC123___002.doc
ABC123___003.doc
效果:
0123ABCD___1.doc
0123ABCD___3.doc
ABC123___002.doc
ABC123___013.doc
aaa.txt
aaa.txt
0123ABCD___2
ABC123___001
已解决!
作者: flashercs 时间: 2022-5-29 23:10
本帖最后由 flashercs 于 2022-5-30 10:23 编辑
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
- pause
- exit /b
- #>
- $beginNumber = 1
- Add-Type -TypeDefinition @'
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
-
- namespace MyCode
- {
- public class ExplorerViewComparer : Comparer<string>
- {
- [DllImport("Shlwapi.dll", EntryPoint = "StrCmpLogical", CharSet = CharSet.Auto)]
- internal static extern int StrCmpLogical(string p1, string p2);
-
- public override int Compare(string x, string y)
- {
- return StrCmpLogical(x, y);
- }
- }
- }
- '@
- $ExpComp = New-Object MyCode.ExplorerViewComparer
- [system.collections.arraylist]$alfiles= @(Get-ChildItem -Path .\*_* | Where-Object { -not $_.PSIsContainer } | ForEach-Object { $_.Name })
-
- [void]$alfiles.Sort($ExpComp)
-
- $alfiles | Select-Object -Property @{
- Name = 'prefix'
- Expression = {
- if ($_ -match '^([^_]*_+)(\d+)') {
- $script:m = $Matches
- $script:m[1]
- } else {
- $Script:m = $null
- ""
- }
- }
- }, @{
- Name = 'suffix'
- Expression = { if ($Script:m) { $Script:m[2] } }
- }, @{
- Name = 'number'
- Expression = { if ($Script:m) { $Script:m[2] -as [int] } }
- } | Group-Object -Property prefix | ForEach-Object {
- if ($_.Name -ne "") {
- $arrnumbers = @($_.Group | ForEach-Object { $_.number })
- $numberLength = $_.Group[0].suffix.Length
- for ($i = $beginNumber; $i -le $arrnumbers[-1]; $i++) {
- if ($arrnumbers -notcontains $i) {
- $_.Name + $i.ToString().PadLeft($numberLength, '0')
- }
- }
- }
- } | Set-Content -LiteralPath .\aaa.txt
复制代码
作者: czvde 时间: 2022-5-29 23:45
本帖最后由 czvde 于 2022-5-30 10:42 编辑
回复 2# flashercs
谢谢
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |