Copying files - RegEx help

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
}

… 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? :wink: :smiley:

:), 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.

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.

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.

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!!!

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’)

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

I’m from Germany … we’re used to drink beer from a young age. :wink: