Powershell variable regex

Pagina: 1
Acties:

Onderwerpen

Vraag


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Hoi allen, ik ben bezig met een PS script te schrijven om files te moven van uit de root naar bestaande of nieuwe subdirectories aan de hand van bepaalde cijfers in de betstandsnaam.
Het PS script is zo goed als klaar maar ik vind niet hoe ik een bepaalde cijfers uit de bestandsnaam kan opslaan in een variable.

PS script:
PowerShell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$root = "\\fqdn\share"

$files = Get-ChildItem -Path $root

ForEach ($file in $files)
{
    $year = $file.regexfilter
    $month = $file.regexfilter
    $dir_path = Join-Path -Path $root -ChildPath $year
    $dir_path_month = Join-Path $dir_path -ChildPath $month
    $letters = $file.BaseName[0..2] -join ""
    $dir_sub_full = Join-Path -Path $dir_path_month -ChildPath $letters
    If (!(Test-Path $dir_path)) {
        
        New-Item -Path $root -Name $year -ItemType Directory
    
    }
    If (!(Test-Path $dir_path_month))
    {
        New-Item -Path $root\$year\ -Name $month -ItemType Directory
    }
    If (!(Test-Path $dir_sub_full))
    {
        New-Item -Path $root\$year\$month -Name $letters -ItemType Directory
    }
    Move-Item -Path $file.FullName -Destination $dir_sub_full
 }


De filenames zijn opgebouwd als volgt:
blablabla_117012789090.png

Hiervan heb ik cijfer 2 & 3 nodig voor het jaar en cijfer 4 & 5 voor de maand. Dus als regex heb ik:
jaar: .*_\d\d\d(\d{2})\d*
maand: .*_\d\d\(\d{2})\d*

Alleen vind ik nu niet hoe ik deze in $year & $month variable moet krijgen.
Kan me iemand even de juiste richting uitduwen?

Beste antwoord (via Verwijderd op 09-11-2017 11:30)


  • Dark Blue
  • Registratie: Februari 2001
  • Laatst online: 24-08 09:31

Dark Blue

Compositionista!

Alpenmeisje

Heb je al gezocht op de manier waarop je in PowerShell een regex gebruikt?
Want ik zie het nu onder je code staan, maar niet ín. Daar staat alleen 'regexfilter'.
Noot: ik ben niet thuis in PowerShell zelf.

Wellicht kan dit stukje je helpen:
https://stackoverflow.com/a/18001540

PowerShell:
1
2
3
$a = [regex]"\[(.*)\]"
$b = $a.Match("sdfqsfsf[fghfdghdfhg]dgsdfg") 
$b.Captures[0].value


waarbij ik denk dat het overbodig is om je regex eerst in een variabele te stoppen, maar zie maar eens.

heidiulrich.nl | adventura.nl : rugzakavonturen | pathwise.nl : prepping geeks to get jobs

Alle reacties


Acties:
  • Beste antwoord
  • 0 Henk 'm!

  • Dark Blue
  • Registratie: Februari 2001
  • Laatst online: 24-08 09:31

Dark Blue

Compositionista!

Alpenmeisje

Heb je al gezocht op de manier waarop je in PowerShell een regex gebruikt?
Want ik zie het nu onder je code staan, maar niet ín. Daar staat alleen 'regexfilter'.
Noot: ik ben niet thuis in PowerShell zelf.

Wellicht kan dit stukje je helpen:
https://stackoverflow.com/a/18001540

PowerShell:
1
2
3
$a = [regex]"\[(.*)\]"
$b = $a.Match("sdfqsfsf[fghfdghdfhg]dgsdfg") 
$b.Captures[0].value


waarbij ik denk dat het overbodig is om je regex eerst in een variabele te stoppen, maar zie maar eens.

heidiulrich.nl | adventura.nl : rugzakavonturen | pathwise.nl : prepping geeks to get jobs


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Thanks voor de reply, ondertussen met wat hulp op SO het script correct kunnen maken
PowerShell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$root = \\fqdn\path'
$files = Get-ChildItem -Path $root -File

foreach ($file in $files) {
    $match = $file.BaseName -match '.*_\d(?<year>\d{2})(?<month>\d{2}).*'    
    if (-not $match) {
        Write-Warning "Could not extract year/month from: $file"
        continue
    }    
      $subdir = [IO.Path]::Combine($root, $matches.year, $matches.month)
    if (-not (Test-Path -Path $subdir)) {
        New-Item -Path $subdir -ItemType Directory > $null
    }
    $newfile = Join-Path -Path $subdir -ChildPath $file.Name
    if (Test-Path -Path $newfile) {
        Write-Warning "File $($file.Name) already exists in: $subdir"
        continue
    }
    Move-Item -Path $file.FullName -Destination $subdir
}