0

The script I'm working on is producing a log file every time it runs. The problem is that when the script runs in parallel, the current log file becomes inaccessible for Out-File. This is normal because the previous script is still writing in it.

So I would like the script being able to detect, when it starts, that there is already a log file available, and if so, create a new log file name with an increased number between the brackets [<nr>].

It's very difficult to check if a file already exists, as it can have another number each time the script starts. It would be great if it could then pick up that number between the brackets and increment it with +1 for the new file name.

The code:

$Server = "UNC"
$Destination ="\\domain.net\share\target\folder 1\folder 22"
$LogFolder = "\\server\c$\my logfolder"

# Format log file name
$TempDate = (Get-Date).ToString("yyyy-MM-dd")
$TempFolderPath = $Destination -replace '\\','_'
$TempFolderPath = $TempFolderPath -replace ':',''
$TempFolderPath = $TempFolderPath -replace ' ',''
$script:LogFile = "$LogFolder\$(if($Server -ne "UNC"){"$Server - $TempFolderPath"}else{$TempFolderPath.TrimStart("__")})[0] - $TempDate.log"
$script:LogFile

# Create new log file name
$parts = $script:LogFile.Split('[]')
$script:NewLogFile = '{0}[{1}]{2}' -f $parts[0],(1 + $parts[1]),$parts[2]
$script:NewLogFile

# Desired result
# \\server\c$\my logfolder\domain.net_share_target_folder1_folder22[0] - 2014-07-30.log
# \\server\c$\my logfolder\domain.net_share_target_folder1_folder22[1] - 2014-07-30.log
#
# Usage
# "stuff" | Out-File -LiteralPath $script:LogFile -Append
tshepang
  • 12,111
  • 21
  • 91
  • 136
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • See the second half of [my answer to your previous question](http://stackoverflow.com/a/25021286/1630171). – Ansgar Wiechers Jul 30 '14 at 07:36
  • Thank you Ansgar for the help. But I'm a bit confused. In my example I try to create an initial log file with the nr. `[0]` in it. This fails when I use it with `Out-File` because I can't seem to escape the brackets properly. If I manage to do this, then how do I check for only the latest number to add `+1`? Because the variable `$filename` in your example will be lost when the script closes and the next one runs. – DarkLite1 Jul 30 '14 at 08:01
  • `-LiteralPath` should take care of the square brackets. Please show the error you're getting. As for losing the value of `$filename`: the loop in my code sample will automatically find the lowest unused number `#` for a given name pattern `foo[#]bar.log`. If `foo[0]bar.log` and `foo[1]bar.log` already exist in the destination folder, the loop will produce `foo[2]bar.log` as the next filename. – Ansgar Wiechers Jul 30 '14 at 09:04
  • Your code works indeed on a fixed value within `$filename`. I'm now trying to figure out how to retrieve the existing file names on the drive and put it in the `$filename` variable. Of course only for those with the correct `$Destination` and `Get-Date` in their names. – DarkLite1 Jul 30 '14 at 09:26
  • I'm over complicating things, it works as designed. Thank you Ansgar, this was really helpful. I'll update my question with your solution. Thanks again man! – DarkLite1 Jul 30 '14 at 09:44

1 Answers1

1

As mentioned in my answer to your previous question you can auto-increment the number in the filename with something like this:

while (Test-Path -LiteralPath $script:LogFile) {
  $script:LogFile = Increment-Index $script:LogFile
}

where Increment-Index implements the program logic that increments the index in the filename by one, e.g. like this:

function Increment-Index($f) {
  $parts = $f.Split('[]')
  '{0}[{1}]{2}' -f $parts[0],(1 + $parts[1]),$parts[2]
}

or like this:

function Increment-Index($f) {
  $callback = {
    $v = [int]$args[0].Groups[1].Value
    $args[0] -replace $v,++$v
  }

  ([Regex]'\[(\d+)\]').Replace($f, $callback)
}

The while loop increments the index until it produces a non-existing filename. The parameter -LiteralPath in the condition is required, because the filename contains square bracket, which would otherwise be treated as wildcard characters.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328