I need a PowerShell script to monitor text files (files ending with *.txt) real time in a particular directory, export the file names to a csv file and start capturing & exporting ONLY if new files are saved in that directory. Please note that more than one file is saved in the directory simultaneously.
Below is what I have written, but obviously this keeps writing the same text files over and over again. I want the script to capture files the first time, and from the next time, capture & export only if there is a new file
$x = 1
do {
$file1 = Get-ChildItem D:\Test\*.txt | Select-Object name,length,LastWriteTime
$file2 = $file1
foreach ($file2 in $file1) {
$file1 | Export-Csv -path D:\FileSize\TEMPTXT.csv
[System.IO.File]::ReadAllText("D:\FileSize\TEMPTXT.csv") |
Out-File D:\FileSize\TEXT.csv -Append -Encoding ascii
}
} until ($x=0)