0

So I have the below script for a project at work:


    #  This script will look for a CSV in the script folder
    #  If found it will split the CSV based on a change in a column header
    #  It will then create seperate CSV files based on the column data

    #  get script directory and add it to a variable
Set-Location $PSScriptRoot
$baseDir = $PSScriptRoot

    #  get a list of csv file names
$csvfile = Get-ChildItem -Path $baseDir -Filter *.csv

    #  If multiple CSV files loop through all of them
foreach ($i in $csvfile) {
       #  Import and split the original csv
       #  Change the value after -Property to match the column header name of the column used to split on value change -
       #  Header names with spaces require surrounding quotes -
       #  This value will also be used to name the resulting CSV file
       Import-Csv $i | Group-Object -Property "Submission ID" | 
             Foreach-Object {$path="Output\"+$_.name+".csv" ; $_.group | 
             Export-Csv -Path $path -NoTypeInformation}
       
                #  get the current time and date
             $procdte = Get-Date -uformat "%m-%d-%Y %H %M %S" 
       
                #  rename the original file so that it's not processed again

             Rename-Item $i -NewName "Processed $procdte.txt" 
}
    #  End processing loop 

Important: some parts of this script are commented out - like the Rename-Item line is half-commented. Dunno why, think it's a stackoverflow markdown issue. It isn't like that in ISE

I tested it out with two csv files in the current directory. It's supposed to rename both files at the end to a .txt so they don't get processed again. When I just run the script, it misses the 2nd csv file. But when I step through it with debugger in ISE, it renames both files fine. What gives?

Ran powershell script, expecting both CSV files to be renamed, however it only renamed one of them. Thought there was an issue with the script, but when I run it in debug, it renames both files fine. Have managed to replicate multiple times.

Edit: to specify - this is not designed to work with two csv files specifically. It's supposed to work with one or more. I was just using two to test.

jetrel
  • 11
  • 2
  • It might just be that the code is too fast and there is still a file lock active. You can take a look at this [post](https://stackoverflow.com/questions/24992681/powershell-check-if-a-file-is-locked) on how to check if a file is locked via Powershell. –  Oct 25 '22 at 17:11
  • What would be the solution for that? – jetrel Oct 25 '22 at 17:12
  • 2
    If PowerShell processes the second file in less than 1 second, the new filename will collide with the previously renamed file. Either append the timestamp to the already existing filename or even use milliseconds in your timestamp. – stackprotector Oct 25 '22 at 17:13
  • Dumb question, but how do I add milliseconds to the timestamp? I thought %FFF would work but I guess not. not quite sure what to google for the %H %M %S stuff – jetrel Oct 25 '22 at 17:19
  • `-UFormat` doesn't have milliseconds specifier (see [Notes](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.2#notes)). Use `-Format` instead, see https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – zett42 Oct 25 '22 at 17:49
  • You can also use the .Net DateTime object and its `ToString()` overload: `[DateTime]::Now.ToString('yyyyMMddHHmmssms')` –  Oct 26 '22 at 08:51
  • Adding 1 ms = Waiting 1 ms. I.e. Add `Start-Sleep -Milliseconds 1` – Dennis Oct 26 '22 at 18:55

0 Answers0