adding a prefix to a file name if the prefix is not already there RRS feed

  • Question

  • I have a script that adds a prefix to a file name then moves the file from its source directory to a destination directory. The problem I am encountering is that if the destination directory is unavailable for any reason the file fails to move & so remains in the source directory and then the next time the script runs the prefix gets added again so I sometimes end up with files that look like this:


    I need to build in some kind of logic that prevents this from happening so that if the Prefix_ is there it doesn't add it again, but not I'm not sure what the best way to do this is.  Here is the code that adds the prefix.

    Get-ChildItem $Source\* -include *.jpg,*.tif,*.pdf -exclude *.log | % {Rename-item $_.Fullname ("Prefix_" + $_.Name)}

    # When I wrote this script only God and I knew what I was doing. # Now, only God Knows!

    Monday, October 20, 2014 4:00 PM


  • What about

    -exclude "Prefix_*","*.log"

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Wasisname Monday, October 20, 2014 4:41 PM
    Monday, October 20, 2014 4:08 PM

All replies