PS V2 Rename and remove script

Hi all,

I’ve been having issues with a script i’m writing;
The script needs to be PowerShell V2 compatible as the system I’m running it on cannot be upgraded.
[url]http://tatux.co.uk/2015/04/02/rename-and-remove-script/[/url]

It loops through all files with in a folder and removes a specific portion of the name. I have about 116,000 files to loop through.
If it comes across a file that has the same name while renaming, it deletes it if it has the same length or it renames it appending the end of the file to indicate the amount of files with duplicate names but with different file sizes.

The script almost works perfectly, but some times when there are multiple items with the same name after renaming and they have different lengths it allows 2 files with the same length through the delete check process.

The script its quite messy as some of the files have multiple (.) Periods in them and I only want to work with the end of the file when removing the part of the file name specified in my regex pattern.

I’m sorry if this is confusing,

Let me know if there is anymore information I can provide.

Thank you for your time.

Nigel Tatschner.

What makes debugging this difficult is that you’re not currently outputting much in the way of diagnostic information. Consider adding Write-Verbose calls, and running the script with -Verbose to activate those calls.

For example, after you finish manipulating $SubjectItem, write verbose the new contents of $SubjectItem:

Write-Verbose "SubjectItem is now $SubjectItem"

If you add enough of those, it’ll be easy to see the information being manipulated “as it happens,” and you’ll be able to spot the exact point where it’s “going wrong.”

Your Check-MetaCharactersNT can have Write-Verbose added inside it; because it uses [CmdletBinding()], it will support -Verbose.

Your script as a whole currently doesn’t support -Verbose. At the top, you would add:

[CmdletBinding()]Param()

That will let your script recognize -Verbose when you run it, and you’d be able to use Write-Verbose throughout the script.

Also… I can’t tell if this would help or not, but did you know PowerShell lets you use $variable[-1] to access the last item in an array? E.g., -1 is the last item, -2 is next-to-last, etc. That might help you tighten up some of the code.