Author Posts

July 11, 2014 at 12:05 pm

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)
        }
       $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  

July 11, 2014 at 12:23 pm

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'
	}
}

July 13, 2014 at 8:14 am

@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. 🙂

July 13, 2014 at 10:10 am

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