Generate new path and append (number)

This topic contains 3 replies, has 2 voices, and was last updated by  Rob Simmers 3 years, 1 month ago.

  • Author
  • #17032

    Rob Simmers

    So, this code works great and I actually figured out a regex, albeit simple, I figured it out which is an accomplishment on a Friday afternoon. Basically, if a file exists, I want to update the path to append (1), (2)...and so on. I'm more curious if there is a more efficient way to do this:

    $path = "C:\Test\Test_Jim_Johnson_8-12-2014.txt"
    $int = 2
    $regex = "\(\d+\)"
    if (Test-Path $path) {
        do {
            if ($path -match $regex) {
                $path = $path -replace $regex, ("({0})" -f $int)
            else {
                $path = $path -replace ".txt", ("({0}).txt" -f $int)
        while (Test-Path -Path $path)
    New-Item -ItemType File -Path $path -Force

    Which generates:

    PS C:\Users\sysrs.INT> gci C:\test
        Directory: C:\test
    Mode                LastWriteTime     Length Name                                         
    ----                -------------     ------ ----                                         
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(10).txt           
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(11).txt           
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(2).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(3).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(4).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(5).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(6).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(7).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(8).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014(9).txt            
    -a---         7/11/2014   4:02 PM          0 Test_Jim_Johnson_8-12-2014.txt  
  • #17033

    Adam Bertram

    This is a little cleaner and no regex (yay!)

    $FilePath = 'C:\Users\ABertram\Desktop\testing\test.txt'
    if (Test-Path $FilePath -PathType Leaf) {	
    	[System.IO.FileInfo]$FilePath = 'C:\Users\ABertram\Desktop\testing\test.txt'
    	for ($i = 1; $i -lt 11; $i++) {
    		New-Item -ItemType File -Path "$($FilePath.Directory)\$($FilePath.BaseName)($i)$($FilePath.Extension)" -ErrorAction 'SilentlyContinue'
  • #17074

    Rob Simmers

    @adam, I don't think you understand what I'm trying to accomplish. I'm automating a process, specifically in Word and auto filling out a form. I open a template and try to save the file to C:\Test\Test.doc, but the path exists because the process has already run. I want to 'increment' the path accordingly so when I call .SaveAs() then I'm passing a path so that the previous item does not get overwritten. So, when the script starts I'll have a function that takes the static C:\Test\Test.doc path and dynamically generate the next incremental file name (e.g.C:\Test\Test(1).doc). Your code just generates 10 incremented directories. If you try the code above, the first run it will generate text.txt and each successive run will generate a new path\item because the initial static path exists. I wasn't very clear in my post, but there was a cold Porter calling my name. 🙂

  • #17078

    Rob Simmers

    Converted to a function. See attached, the pre tag or standard text was playing hell with my comment-based help XML. Comments\Improvements welcome...

You must be logged in to reply to this topic.