Получение метки времени из имени файла с powershell - PullRequest
0 голосов
/ 20 мая 2019

Мне нужно сгруппировать файлы по дате в имени файла. Пример:

- input (folder)
-- random_folder_name_1 (folder)
--- 01-Apr-19, 10_33_37_Sample_1.pdf
-- random_folder_name_2 (folder)
--- some_other_file.pdf
--- 04-Apr-19, 14_33_37_Sample_15.pdf
...

Все файлы имеют шаблон: %datestamp%, %timestamp%_%keyword% Мне нужно отсортировать их как:

- output (folder)
-- %datestamp% (folder)
--- %keyword%.pdf

Я реализовал пошаговую папку input и искал pdf -файлы, но задушил получение даты.

$origin_folder = "input"
$destination_folder = "output"

$origin = Join-Path -Path $(Get-Location) -ChildPath "$origin_folder"
$destination = Join-Path -Path $(Get-Location) -ChildPath "$destination_folder"

$files = Get-ChildItem -Path $origin -Recurse -Filter *.pdf
# RegEx for date stamp as day-3_leters_of_month-year
$regex = "\d{2}-\D{3}-\d{2}"

foreach ($file in $files) {
    $source_file = $file.FullName
    $datestamp = [regex]::Matches($file.BaseName, $regex)
    Write-Output "$datestamp"
}

По какой-то причине $datestamp является пустой строкой. Что с этим не так?

Кроме того, как вычесть регулярное выражение из имени файла? Допустим, из имени файла %datestamp%, %timestamp%_%keyword%.pdf вычтите %datestamp%, %timestamp%_, чтобы получить %keyword%.pdf

Финальный скрипт: Рабочая версия

$origin_folder = "input"
$destination_folder = "output"

$origin = Join-Path -Path $(Get-Location) -ChildPath "$origin_folder"
$destination = Join-Path -Path $(Get-Location) -ChildPath "$destination_folder"

# Get all files in subfolders
$files = Get-ChildItem -Path $origin -Recurse -Filter *.pdf

# Date Regular Expression
# '2 digits of day'-'3 symbols of month'-'2 digits of year'
# Equals to template 'dd-MMM-yy'
$date_regex = "\d{2}\-\w{1,3}\-\d{2}"

# Ballast Regular Expressions
# Equals to template 'dd-MMM-yy, hh_mm_ss_'
$ballast_regex = "\d{2}\-\w{1,3}\-\d{2}, \d{2}_\d{2}_\d{2}_"

# Walk through all found files
foreach ($file in $files){
    # Get the full address of file which needs to be copied
    $source_file = $file.FullName

    # Get the datestamp from filename
    $datestamp = [regex]::Matches($file.BaseName, $date_regex)
    # Convert into usable format with digits only in filename
    $datestamp = [datetime]::parseexact($datestamp, 'dd-MMM-yy', $null).ToString('yyyy-MM-dd')

    # Take the name of sample from filename
    $keyword = $file.Name -replace $ballast_regex

    # Create the folder based on date stamp
    $destination_subfolder = Join-Path -Path $destination -ChildPath $datestamp

    # Create the folder based on datestamp if it doesn't exist
    If(!(Test-Path -Path $destination_subfolder))
        {   
            # Create folder silently
            # To make it "as usual" : remove " | Out-Null" from the end
            New-Item -Path $destination_subfolder -ItemType Directory -Force | Out-Null
        }

    # Path of file where it will be copied, but with changed name to sample name only
    $destination_file = Join-Path -Path $destination_subfolder -ChildPath $keyword

    # Copy actual file
    Copy-Item $source_file -Destination $destination_file
}

Ответы [ 2 ]

0 голосов
/ 21 мая 2019

Так что тот факт, что это свидание выглядит неважно. Вы не пытаетесь разобрать это, вы просто хотите получить необработанный текст. Так что я просто разработал RegEx, который собирал бы это и пример данных из конца имени файла.

'01-Apr-19, 10_33_37_Sample_1.pdf'|?{$_ -match '^(.+?), \d\d_\d\d_\d\d_(.+)\....$'}|%{$Matches[1],$Matches[2]}
0 голосов
/ 21 мая 2019

Я изменил ваше регулярное выражение с

$regex = "\d{2}-\D{3}-\d{4}"

к этому:

$regex = "\d{2}\-\w{1,3}\-\d{2,4}"

Теперь он правильно принимает даты.

...