Copying files - RegEx help

This topic contains 8 replies, has 2 voices, and was last updated by  Olaf Soyk 4 weeks, 1 day ago.

  • Author
    Posts
  • #84394

    Andy
    Participant

    Hi everyone,

    I have one PDF file in: C:\Temp\AVMS\232323 AVM.pdf

    I need for the script to create a folder called 232323.1 in this location C:\Temp\AVMS\CWB\232323.1

    The script will then need to copy the file 232323 AVM.pdf and place it into a new folder above (C:\Temp\AVMS\CWB\232323.1)

    My script below successfully copies this file C:\Temp\AVMS\CWB\232323.1 to folder C:\Temp\AVMS\CWB\232323

    I cannot find a way to have the script copy file C:\Temp\AVMS\CWB\232323.1 to folder C:\Temp\AVMS\CWB\232323.1

    I guess I need to find a way to modify $targetdir = C:\Temp\AVMS\CWB\232323.1

    Of course I have many of these PDF files to process but are all in the same format (number_follow_by AVM.pdf

    Please assist and thank you in advance.

    ==================================================================================

    $target = 'C:\Temp\CWB\'
    Get-ChildItem C:\Temp\AVMS\*.pdf |
    Where-Object{ $_.Name -match '^(\d+)' } |
    ForEach-Object{
    $targetdir = Join-Path $target $matches[1]

    write-host $targetdir

    Copy-Item $_ $targetdir
    }

  • #84398

    Olaf Soyk
    Participant

    ... should work this way actually ...

    $Source = 'C:\Temp\AVMS'
    $target = 'C:\Temp\CWB'
    
    Get-ChildItem -Path $Source -Filter *.pdf -File |
        Where-Object{ $_.Name -match '^(\d+.*)AVM\.pdf' } |
            ForEach-Object{
                $targetdir = Join-Path -Path $target -ChildPath $matches[1].trim()
                If(-not (Test-Path -Path $targetdir)){
                    New-Item -Path $targetdir -ItemType Directory -Force
                }
                Copy-Item -Path $_.FullName -Destination $targetdir -Force
            }

    BTW: Did you know – every time you use Write-Host a puppy dies? 😉 😀

  • #84404

    Andy
    Participant

    :), never knew that.

    Thanks so much for your response. However, the file 232323 AVM.pdf still gets copied to folder C:\Temp\CWB\081230351 NOT C:\Temp\CWB\081230351.1. I need it to be copied to folder 081230351.1.

    Thank you.

    • #84410

      Olaf Soyk
      Participant

      However, the file 232323 AVM.pdf still gets copied to folder C:\Temp\CWB\081230351 NOT C:\Temp\CWB\081230351.1. I need it to be copied to folder 081230351.1.

      Now you confused me completely ... sorry. I understood you want to copy the files to a subfolder with the same name like the number in the file name. If your destination folder needs more characters you should add this to the line where you build the $targetdir variable.

  • #84418

    Andy
    Participant

    Thank you. Yes that is my problem. I don't know how to modify this variable. Could you suggest how I would build this variable $targetdir to reflect what I needed? Thanks again.

  • #84424

    Andy
    Participant

    Perfect thank you. I got it by trial and error :).

    $newfile = $target_dir + $matches[1] + ".1\" + $file.basename + $file.extension

    Thanks again for all your help!!!

  • #84428

    Olaf Soyk
    Participant

    You could have told that you just want to add a period and a "1"
    So you can change that

     $targetdir = Join-Path -Path $target -ChildPath $matches[1].trim()

    to that

     $targetdir = Join-Path -Path $target -ChildPath ($matches[1].trim() + '.1')
  • #84466

    Andy
    Participant

    I'd offer you a beer but it appears you are underage :).

    • #84469

      Olaf Soyk
      Participant

      I'm from Germany … we're used to drink beer from a young age. 😉

You must be logged in to reply to this topic.