【问题标题】:Obtaining timestamp from filename with powershell使用powershell从文件名中获取时间戳
【发布时间】:2019-05-20 20:37:11
【问题描述】:

我需要根据文件名中的日期对文件进行分组。 示例:

- 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-files,但在获取日期戳时绞杀。

$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
}

【问题讨论】:

    标签: regex powershell parsing text-parsing


    【解决方案1】:

    我改变了你的正则表达式

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

    到这里:

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

    现在它的日期是正确的。

    【讨论】:

      【解决方案2】:

      所以这是一个约会的事实看起来并不重要。你不是想解析它,你只想要它的原始文本。所以我开始设计一个正则表达式,它会从文件名的末尾抓取它和样本数据。

      '01-Apr-19, 10_33_37_Sample_1.pdf'|?{$_ -match '^(.+?), \d\d_\d\d_\d\d_(.+)\....$'}|%{$Matches[1],$Matches[2]}
      

      【讨论】:

      • 好吧,您的正则表达式会修剪文件扩展名。我使用-replace "\d{2}\-\w{1,3}\-\d{2}, \d{2}_\d{2}_\d{2}_" 选项修改了脚本
      • 如果你想包含扩展,这很简单,只需从它的末尾删除\....。但是,嘿,如果你有一个工作模型,那就太好了!我只是想我会提供一个替代方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 2019-05-28
      • 2013-06-18
      • 2012-06-08
      • 2013-06-24
      • 2015-05-10
      相关资源
      最近更新 更多