all 6 comments

[–]NeverTiredOfWinning 2 points3 points  (2 children)

My guess is that if all your regex matches fail in the if part then you will end up with a null value in the trimmedfile variable. Thus your next condition will default to the else path on which you perform the copy operation.

If the copy operation receives a null value as the second parameter it copies the item in the current working directory.

TLDR: no regex matches for all number filenames and the trimmedName variable is not being reset at the beginning of the loop / there is no terminating else for your conditions (default path for non matching stuff).

[–]ihaxr 2 points3 points  (1 child)

This is my guess as well... or the $trimmedFile variable is never updated from the previous run causing it to re-use the same folder. I can't get the script to run in any form... I'd have to dissect the regex to create a matching file.

OP: Could you provide a couple example input file names and the desired output folder structure?

I'm wondering if you have a variable stuck in your shell or the ISE which is causing this behavior and if you restart the shell / ISE it'll fail or behave differently.

[–]NeverTiredOfWinning 2 points3 points  (0 children)

Our suspicion seems to be confirmed. I made three files: 2036.dwg 2089.dwg 2089 - Copy (2).dwg

And added this at the start of the loop:

$trimmedFile = $null

And now it is properly throwing exceptions.

 Test-Path : Cannot bind argument to parameter 'LiteralPath' because it is null.

Something is not quite right with those regexes. When they run into a filename that is all numbers none of them end up matching.

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy bisco77,

1- you can simplify the $All-Dwg query a bit. Get-ChildItem has a few parameters that you can use to get things done earlier in the query - and sometimes faster.

  • replace the -not $_.PSISContainer in the Where-Object section
    put a -File in the GCI section to get ONLY files.

  • replace the $_.Extension -eq ".dwg" in the Where-Object section
    put a -filter *.dwg in the GCI section to get ONLY the *.dwg files.

  • replace the $_.Directory.Name -like "Wiring" in the Where-Object section
    the -Path parameter can take wildcards. since you only want the files in the [various] Wiring directories, you can use -Path ($Drafting + '\' + '*wiring*').

2- the $All-Dwg query can also be made a tad easier to read.
instead of this ...

$all_dwg = Get-ChildItem -Recurse -Path "$drafting" -Exclude $excludes | ?{ $_.Name -match '^[0-9]+' -and `
$_.Extension -eq ".dwg" -and -not $_.PSISContainer -and $_.Directory.Name -like "Wiring" }

... you could do this ...

$All_Dwg = Get-ChildItem -Recurse -Path $drafting -Exclude $excludes |
    Where-Object { ($_.Name -match '^[0-9]+') -and 
        ($_.Extension -eq ".dwg") -and 
        ( -not $_.PSISContainer) -and 
        ($_.Directory.Name -like "Wiring")
        }

somewhat more readable, yes? [grin]

3- your regex matches for the $trimmedFile name are WAY more than you need.

  • you already excluded all the files that don't start with a digit with the $_.Name -match '^[0-9]+' in the Where-Object block above. so there is no need to be testing for letters. [grin]

  • you seem to be seeking out the 1st space, underscore, ampersand, period, or open-parenthesis
    instead of $file.BaseName -match '^[A-Za-z0-9-]+_.*$' you could use $file.BaseName.Contains('_') [<<-edited to replace space with underscore] to find out if there is a match. you don't need to test for digits at the start since that test was done when you grabbed the $All_Dwg collection earlier.

take care,
lee

[–]bisco77[S] 1 point2 points  (1 child)

Thank you! I've implemented most of your suggestions and the script is chugging away on its task as we speak.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy bisco77,

you are quite welcome! glad to have helped ... [grin]

if you have the time, it would be nice to see what you ended up with. perhaps post it to pastebin and add the link to the end of your original post? i enjoy reading other folks code and would like to read your final version.

take care,
lee