Matt
Matt

Reputation: 33

Removing part of a file name before copy using PowerShell

I need to rename files during the copy process and strip out part of the file name. What I have been doing before users added to the file name was simple:

dir $PROCDIR\$PDFTYPE\holding_pattern\*.pdf -recurse | sort -property lastwritetime | select    -first 1 | move-item -destination $PROCDIR\$PDFTYPE\begin_processing

The file name format that I am working with is now xxx_xxx_xxx_xxx_xxx.pdf where the _ splits the information apart. The x's are just an example because the file could be named LakeTahoe_February15_Airplane_0115201457_baseball.pdf. When I perform the copy I need to keep the first three.... so from aaa_aaa_aaa_aaa_aaa.pdf to aaa_aaa_aaa.pdf. Basically stripping out the last two. Further if there is nothing beyond LakeTahoe_February15_Airplane_.pdf I want to get rid of the last "_" as well.

I am still very new with powershell but learning. It is good stuff however frustrates me from time to time :). Ideas?

Thanks!

Upvotes: 3

Views: 444

Answers (2)

Adi Inbar
Adi Inbar

Reputation: 12321

Another way to do it, inline and more succinct (this is the last stage of your pipleine; everything that precedes it stays the same):

| mi -Des $PROCDIR\$PDFTYPE\begin_processing\$(($_.Name -split '_')[0..2] -join '_').pdf

(mi -Des is the same as Move-Item -Destination; I used the short version to fit it into one line without scroll bars.)

What this does is splits the base filename into an array of the underscore-separated parts, selects the first three elements of the array (i.e., the first three parts of the filename), glues them back together with underscores, and tacks the .pdf extension back on.

Another approach is to use the -replace operator to match the part you want to get rid of and replace it with an empty string:

| mi -Des $PROCDIR\$PDFTYPE\begin_processing\$($_.Name -replace '(_[^_]+){2}(?=\.)', '')

Upvotes: 0

user189198
user189198

Reputation:

Here is a regex solution that might help you out:

dir $PROCDIR\$PDFTYPE\holding_pattern\*.pdf | sort -property lastwritetime | select    -first 1 | % { $_.Name -match '.*?_.*?_.*?(?=_)'; $Target = '$PROCDIR\$PDFTYPE\begin_processing\{0}.pdf' -f $matches[0]; Move-Item -Path $_.FullName -Destination $Target -WhatIf; };

The results I got during my test seem to indicate that the move/rename operation was successful:

What if: Performing the operation "Move File" on target "Item: C:\test\asdf_blah_asdf_qwer_trew_ytui - Copy.pdf Destination: C:\Windows\System32\WindowsPowerShell\v1.0\$PROCDIR\$PDFTYPE\begin_processing\asdf_blah_a sdf.pdf".

You can safely ignore the phony destination path in my example, since I don't have the $ProcDir and $PDFType variables defined.

Here's a version that's a bit more readable, on multiple lines.

Get-ChildItem -Path c:\test\*.pdf | 
Sort-Object -Property lastwritetime | Select-Object -First 1 | 
ForEach-Object -Process { $_.Name -match '.*?_.*?_.*?(?=_)'; $Target = 'c:\test\subtest\{0}.pdf' -f $matches[0]; Move-Item -Path $_.FullName -Destination $Target -WhatIf; };

Result:

What if: Performing the operation "Move File" on target "Item: C:\test\asdf_blah_asdf_qwer_trew_ytui - Copy.pdf Destination: C:\test\subtest\asdf_blah_asdf.pdf".

Upvotes: 1

Related Questions