I am looking for a series of files that follow a very rigid filename format, but are in different numbered subfolders based on a string in the filename.
The filename is always "LAX_CA_Baseball_<8 digits>.pdf
The files are always 4 folders deep, and the top two folders are always \\fileserver\sports\pdf
The last 4 digits always represent the bottom two subfolders, for example LAX_CA_Baseball_12345678.pdf would be located in "\\fileserver\sports\pdf\56\78"
There are tens of millions of files in total, so just searching for the filename from \\fileserver\sports\pdf will take a very long time
Can I write something in powershell that will parse the last 4 digits of the filename, convert that to the bottom subfolders, and then get the file and copy it to a new folder?
CodePudding user response:
Assuming you already have the filename somewhere, you can extract the digits and construct the path with a regular expression:
$f = "LAX_CA_Baseball_12345678.pdf"
if ($f -match ".*_\d{4,4}(\d\d)(\d\d)\.pdf") {
$sourcePath = "\\server\sports\pdf\$($matches[1])\$($matches[2])\$f"
$destinationPath = "some other folder path"
Copy-Item $sourcePath $destinationPath
} else {
Write-Error "Can't determine folder from filename $f"
}
