Hi all :)
Ii made a Script and now i'm trying to find out 2 things: How can i find out, what codeblock exactly does slow down my script and the second thing is: When i found out it: What can i do? First of all, how you can see, this is a long question, but not only that, also the script is long. It does contain 26k signs. Second: Half of them is debugging, so actualy code is "only" half of it. But the debugging does not make the script slow, because i rely on the speed with deactivated debugging. So please do NOT look on the bad implementation of debugging. Later, i will take $debug2 out probably. $Debug3 i did also deactivate, only $debug1 is activated. Third: I did use powershell for a reason. It shall be a script, which everyone can use with ANY installation of anything. Not even Powershell 7 or a module/package or so.
But to the topic: I asked ChatGPT a lot of times, but he couldn't help me. I did this script with help from ChatGPT, so i have actually very less programming-knowledge. So to say it: Before i startet with creating this script, i had 0 knowledge. I did not even know, what debugging is. And that was 1 week ago. So if you can help me, im VERY thankfully, but pls hold that in mind.
What my script should do:
My scripts need another tool, called ARCtool. It does unpack packed gamefiles of a game. So the Tool does unpack all files in a given folder (and all its subfolders) to .tex files and then further to .dds files. It also creates for each .tex file a .txt file, which contains information about the .dds file.
So and know my script comes in:
It shall copy all dds files out of a folder (and all it's subdirectorys) to a given folder in a specific way AND then write the path of the original and the new path into a csv-file. Because in a second script it will copy back the files to it's respective original location (this question is only about script 1). "In a specific way" means, it does check the information about the .dds files and do then create folders with specific names based on that information. So the tool does mainly filter the .dds files by filename, dds-format and resolution (while the dds-format and resolution is given by the .txt file). And based on these informations, it will create folders with specific names. Basically a whole folderstructure.
What the script actually does:
It does search in a given folder for .txt files which contains "DDSFormat=" and "Width=" and "Height=". Then it searches for .dds files whith the same name in the same subfolder. This is the first loop. Then the second (the big one) lopp begins. Inside this second loop i sort the dds files and do the folder-structur and naming and copying the files and writing the information into the csv. So and i did add 2 progress-functions. One for the first loop and one for the second. Therefore i know already, that the main slow-downs come from the second loop. But i don't know where exactly in the second loop it is coming from because:
I did try to improve the file-checking logic in the second loop. For example i do check for "special" resolution in the second loop, but only probably 20-30% of the files do actually have this "special" resolution. So that means 80-70% have to go through all checks. That's why i did an additional check, which basically checks, if the opposite is true (so if it does have a "normal" resolution). So that means, 70-80% of the files should only pass one of the resolution checks (if it's "normal" resolution), because i did make the other in a else-command. But it did not help for a single second. I did measure the time of how long the script takes on a specific test-folder. It gaves me not a single second less time.
Same principal i did with the filename-checks. ChatGPT suggested me the usage of a variable, which is defined as "false", when i enter the filename-checking proces. If one of the filename-checking is positiv, it will set this variable to true and all other file-name checking will be skipped, because they do only start, when the variable is on false. But... again, it does not gave me a single second time saving.
So that's why i wonder, where the bottleneck is. So what does actually take so much time? I did (i think i did) improve the checking-process a lot, but it gaves me not a single second. So the time-consuming code is not that part. And i want to find out, which one it is. And thats my first question. How the hell can i find out that? Chat GPT did suggest me to use "measure-commands", which you can use for checking, how much time a code-part took. You can even check, how much time it tooks for all iterations.
So an then i tried to implement that in my small loop. It worked, because it's not so complicated. And then i tried to implement it in the second loop, and there it completly fails, because i get errors like "else cmdlet could not be found" and such stuff. But in notepad++ i can clearly see, that each {} is set correctly (it lights up and i can see whre it belongs to). Also i did chatGPT let it write, to make sure, it is correct syntax. but i think, he is not able to to it correctly, he has no understanding of context.
Ok, then i gave up and tried to further improve just the code, but that did not work great (i asked Chat gpt, what i could use to improve). always i got no time imrpovement. But ok, lets come to the important part, the code. I have 4 versions: version 14, version 15.1, version 15.2 and version 15.3. They do all the same, but i tried to make it more perfomant. Did not work. (The description in the script is german)
Before you have a look on the code: Remember only $debug1 is actually true in my tests. And i also do not want to improve debugging-speed, because i will take out $debug 2 later probably. So pls concentrate only on non-debugging stuff. I do share version 14 directly in this post and the rest i will upload in a zip file, because i can not use so much letters in one question i guess.
Here is the zip-file containing all 4 versions:
https://uploadnow.io/f/dd841hx
# Am Anfang des Skripts
$logFile = "C:\Users\Gaming-Tower\Pictures\Textur-Upscaling\DDDA\1-scripttest\upscaled\log.txt"
Start-Transcript -Path $logFile -Append
# Geben Sie Ihre Pfade an
$rootFolder = "C:\Users\Gaming-Tower\Pictures\Textur-Upscaling\DDDA\1-scripttest\ARCtool\enemy"
$outputFolder = "C:\Users\Gaming-Tower\Pictures\Textur-Upscaling\DDDA\1-scripttest\upscaled"
$csvPath = "C:\Users\Gaming-Tower\Pictures\Textur-Upscaling\DDDA\1-scripttest\upscaled\mapping.csv"
# Debugging-Flags
$debug1 = $true # Aktiviert die von Ihnen angegebenen Debugging-Ausgaben
$debug2 = $false # Deaktiviert alle anderen Debugging-Ausgaben
$debug3 = $true # Deaktiviert die Zählfunktion von totalDdsFilesInOutputFolder (Es wird ausgelesen, wie viele .dds Dateien sich am Ende im outputFolder befinden)
# Erstellen Sie leere Strings für die Ausgaben
$output1 = ""
$output2 = ""
$output3 = ""
function Search-DDSFormat {
param (
[string]$filePath
)
$content = Get-Content -Path $filePath
$match = $content | Select-String -Pattern "DDSFormat=(.*)"
$width = $content | Select-String -Pattern "Width=(.*)"
$height = $content | Select-String -Pattern "Height=(.*)"
if ($match) {
@{
'DDSFormat' = $match.Matches.Value -replace "DDSFormat="
'Width' = if ($width) { $width.Matches.Value -replace "Width=", "" } else { $null }
'Height' = if ($height) { $height.Matches.Value -replace "Height=", "" } else { $null }
}
}
}
# Definition einiger Variablen
$mapping = @()
$ddsFormatFiles = @()
$files = Get-ChildItem -Path $rootFolder -Recurse -Filter *.txt
$totalDdsFiles = 0
$totalTxtFilesWithDDSFormat = 0
$totalTxtFilesWithWidth = 0
$totalTxtFilesWithHeight = 0
$totalCopiedFiles = 0
# Durchsuchen des Inhalts der .txt Dateien im rootFolder
$filesCount = $files.Count
$k = 0
foreach ($file in $files) {
$k++
if ($k % 100 -eq 0) {
Write-Progress -Activity "Running Script" -Status "Processing File $k of $filesCount" -PercentComplete (($k / $filesCount) * 100)
}
if ($debug2) { $output2 += "`nÜberprüfe Datei: $($file.FullName)" } # Debugging-Ausgabe
$result = Search-DDSFormat -filePath $file.FullName
if ($result.DDSFormat) {
$totalTxtFilesWithDDSFormat++
}
if ($result.Width) {
$totalTxtFilesWithWidth++
}
if ($result.Height) {
$totalTxtFilesWithHeight++
}
if ($result.DDSFormat -and $result.Width -and $result.Height) {
$totalTxtFiles++
$ddsFormatFiles += @{
'file' = $file
'format' = $result.DDSFormat
'width' = $result.Width
'height' = $result.Height
}
}
}
# Filterung und Kopiervorgang der dds Dateien
$ddsFormatFilesCount = $ddsFormatFiles.Count
$j = 0
foreach ($item in $ddsFormatFiles) {
$j++
$resolution = ""
$fileNameCheck = ""
if ($j % 100 -eq 0) {
Write-Progress -Activity "Running Script" -Status "Processing Item $j of $ddsFormatFilesCount" -PercentComplete (($j / $ddsFormatFilesCount) * 100)
}
$file = $item['file']
$result = $item['format']
$width = $item['width']
$height = $item['height']
$ddsFile = Get-ChildItem -Path (Split-Path $file.FullName) -Filter ($file.BaseName + ".dds") -Recurse
if ($ddsFile) {
if ($debug2) { $output2 += "`nFound .dds file: $($ddsFile.FullName)" } # Debugging-Ausgabe
$totalDdsFiles++
$godFatherFolder = Join-Path -Path $outputFolder -ChildPath $result
if ($debug2) { $output2 += "`nGodFatherFolder nach der Zuweisung: ${godFatherFolder}" }
# Definieren Sie den Pfad für den GrandParentFolder innerhalb des GodFatherFolder
$grandParentFolder = Join-Path -Path $godFatherFolder -ChildPath (Split-Path $godFatherFolder -Leaf)
# Definieren Sie den Pfad für den ParentFolder innerhalb des GrandParentFolder
$parentFolder = Join-Path -Path $grandParentFolder -ChildPath (Split-Path $godFatherFolder -Leaf)
# Prüfen, ob die Breite und die Höhe kleiner oder gleich 16 sind
if ([int]$width -le 16 -and [int]$height -le 16) {
$godFatherFolder += "_UltraLowRes"
$grandParentFolder = $godFatherFolder
$parentFolder = $godFatherFolder
if ($debug2) {
$output2 += "`nBreite und Höhe sind kleiner oder gleich 16, GodFatherFolder: ${godFatherFolder}"
}
}
# Prüfen, ob der Dateiname der .dds-Datei dem Muster "*col*_ID.dds" entspricht
elseif ($ddsFile.Name -match ".*col.*_ID.dds$") {
$godFatherFolder += "_color"
$grandParentFolder = $godFatherFolder
$parentFolder = $godFatherFolder
if ($debug2) { $output2 += "`nDateiname enthält '*col*_ID.dds', GodFatherFolder: ${godFatherFolder}" }
}
else {
# Prüfen, ob der Dateiname der .dds-Datei "NM" oder "DM" enthält
if ($ddsFile.Name -cmatch "NM.*dds$") {
$godFatherFolder += "_NM"
if ($debug2) { $output2 += "`nDateiname enthält 'NM', GodFatherFolder: ${godFatherFolder}" }
} elseif ($ddsFile.Name -cmatch "DM.*dds$") {
$godFatherFolder += "_NM"
if ($debug2) { $output2 += "`nDateiname enthält 'DM', GodFatherFolder: ${godFatherFolder}" }
}
# Definieren Sie den Pfad für den GrandParentFolder innerhalb des GodFatherFolder
$grandParentFolder = Join-Path -Path $godFatherFolder -ChildPath (Split-Path $godFatherFolder -Leaf)
# Prüfen, ob die Breite und die Höhe größer als 2048 sind und gleich sind
if ([int]$width -gt 2048 -and [int]$height -gt 2048 -and [int]$width -eq [int]$height) {
$resolution = "_HyperHighResSquare"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite größer als 2048 ist und die Höhe kleiner oder gleich 2048 ist
elseif ([int]$width -gt 2048 -and [int]$height -le 2048) {
$resolution = "_HyperHighResWide"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Höhe größer als 2048 ist und die Breite kleiner oder gleich 2048 ist
elseif ([int]$height -gt 2048 -and [int]$width -le 2048) {
$resolution = "_HyperHighResTall"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite und die Höhe größer als 1536 und kleiner oder gleich 2048 sind und gleich sind
if ([int]$width -gt 1536 -and [int]$width -le 2048 -and [int]$height -gt 1536 -and [int]$height -le 2048 -and [int]$width -eq [int]$height) {
$resolution = "_UltraHighResSquare"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite größer als 1536 und kleiner oder gleich 2048 ist und die Höhe kleiner oder gleich 1536 ist
elseif ([int]$width -gt 1536 -and [int]$width -le 2048 -and [int]$height -le 1536) {
$resolution = "_UltraHighResWide"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Höhe größer als 1536 und kleiner oder gleich 2048 ist und die Breite kleiner oder gleich 1536 ist
elseif ([int]$height -gt 1536 -and [int]$height -le 2048 -and [int]$width -le 1536) {
$resolution = "_UltraHighResTall"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite und die Höhe genau 1536 sind
elseif ([int]$width -eq 1536 -and [int]$height -eq 1536) {
$resolution = "_SuperHighResSquare"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite genau 1536 ist und die Höhe kleiner als 1536 ist
elseif ([int]$width -eq 1536 -and [int]$height -lt 1536) {
$resolution = "_SuperHighResWide"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Höhe genau 1536 ist und die Breite kleiner als 1536 ist
elseif ([int]$height -eq 1536 -and [int]$width -lt 1536) {
$resolution = "_SuperHighResTall"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite und die Höhe größer als 1024 und kleiner als 1536 sind und gleich sind
elseif ([int]$width -gt 1024 -and [int]$width -lt 1536 -and [int]$height -gt 1024 -and [int]$height -lt 1536 -and [int]$width -eq [int]$height) {
$resolution = "_HighResSquare"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite größer als 1024 und kleiner als 1536 ist und die Höhe kleiner oder gleich 1024 ist
elseif ([int]$width -gt 1024 -and [int]$width -lt 1536 -and [int]$height -le 1024) {
$resolution = "_HighResWide"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Höhe größer als 1024 und kleiner als 1536 ist und die Breite kleiner oder gleich 1024 ist
elseif ([int]$height -gt 1024 -and [int]$height -lt 1536 -and [int]$width -le 1024) {
$resolution = "_HighResTall"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Prüfen, ob die Breite und die Höhe kleiner oder gleich 64 sind, aber mindestens einer der Werte größer als 16 ist
elseif ([int]$width -le 64 -and [int]$height -le 64 -and ([int]$width -gt 16 -or [int]$height -gt 16)) {
$resolution = "_LowRes"
$grandParentFolder += $resolution
if ($debug2) {
$output2 += "`nWidth: ${width}"
$output2 += "`nHeight: ${height}"
$output2 += "`nResolution: ${resolution}"
}
}
# Definieren Sie den Pfad für den ParentFolder innerhalb des GrandParentFolder
$parentFolder = Join-Path -Path $grandParentFolder -ChildPath (Split-Path $godFatherFolder -Leaf)
# Prüfen, ob der Dateiname der .dds-Datei "DM" enthält
if ($ddsFile.Name -cmatch "DM.*dds$") {
# Prüfen, ob $parentFolder auf "NM" endet und ersetzen Sie es durch "DM", wenn es zutrifft
if ($parentFolder -match "NM$") {
$parentFolder = $parentFolder -replace "NM$", "DM"
if ($debug2) { $output2 += "`n'NM' in ParentFolder durch 'DM' ersetzt, neuer ParentFolder: ${parentFolder}" }
}
}
# Prüfen, ob der Dateiname der .dds-Datei "LM" enthält
if ($ddsFile.Name -cmatch "LM.*dds$") {
$fileNameCheck = "_LM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), LM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "AM" enthält
if ($ddsFile.Name -cmatch "AM.*dds$") {
$fileNameCheck = "_AM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), AM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "BM" enthält
if ($ddsFile.Name -cmatch "BM.*dds$") {
$fileNameCheck = "_BM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), BM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "HM" enthält
if ($ddsFile.Name -cmatch "HM.*dds$") {
$fileNameCheck = "_HM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), HM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "TM" enthält
if ($ddsFile.Name -cmatch "TM.*dds$") {
$fileNameCheck = "_TM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), TM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "CMM" enthält
if ($ddsFile.Name -cmatch "CMM.*dds$") {
$fileNameCheck = "_CMM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), CMM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "CM" (aber nicht CMM) enthält
if ($ddsFile.Name -cmatch "_CM([^M].*|)\\.dds$") {
$fileNameCheck = "_CM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), CM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "GM" enthält
if ($ddsFile.Name -cmatch "GM.*dds$") {
$fileNameCheck = "_GM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), GM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "MM" (aber nicht CMM) enthält
if ($ddsFile.Name -cmatch "_MM.*dds$") {
$fileNameCheck = "_MM"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), MM gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
# Prüfen, ob der Dateiname der .dds-Datei "NUKI" enthält
if ($ddsFile.Name -cmatch "NUKI.*dds$") {
$fileNameCheck = "_NUKI"
$parentFolder += $fileNameCheck
if ($debug2) { $output2 += "`nDateiname: $($ddsFile.Name), NUKI gefunden, neuer fileNameCheck: ${fileNameCheck}" }
}
}
# Erstellen Sie den GodFatherFolder im Ausgabeordner
if (-not (Test-Path $godFatherFolder)) {
$testPathResult = Test-Path $godFatherFolder
if ($debug2) { $output2 += "`nTest-Path Ergebnis für ${godFatherFolder}: ${testPathResult}" }
$newItemResult = New-Item -ItemType Directory -Path $godFatherFolder
if ($debug2) { $output2 += "`nNew-Item Ergebnis für ${godFatherFolder}: ${newItemResult}" }
}
# Erstellen Sie den GrandParentFolder im Ausgabeordner
if (-not (Test-Path $grandParentFolder)) {
$testPathResult = Test-Path $grandParentFolder
if ($debug2) { $output2 += "`nTest-Path Ergebnis für ${grandParentFolder}: ${testPathResult}" }
$newItemResult = New-Item -ItemType Directory -Path $grandParentFolder
if ($debug2) { $output2 += "`nNew-Item Ergebnis für ${grandParentFolder}: ${newItemResult}" }
}
# Erstellen Sie den ParentFolder im Ausgabeordner
$parentFolder += $resolution
if (-not (Test-Path $parentFolder)) {
$testPathResult = Test-Path $parentFolder
if ($debug2) { $output2 += "`nTest-Path Ergebnis für ${parentFolder}: ${testPathResult}" }
$newItemResult = New-Item -ItemType Directory -Path $parentFolder
if ($debug2) { $output2 += "`nNew-Item Ergebnis für ${parentFolder}: ${newItemResult}" }
}
# Erstellen Sie den DestinationFolder innerhalb des ParentFolder
$destinationFolder = Join-Path -Path $parentFolder -ChildPath (Split-Path $parentFolder -Leaf)
$i = 2
while (Test-Path (Join-Path -Path $destinationFolder -ChildPath $ddsFile.Name)) {
if ($destinationFolder -match "_\d+$") {
$destinationFolder = $destinationFolder -replace "_\d+$", "_$i"
} else {
$destinationFolder = $destinationFolder + "_$i"
}
$i++
}
if (-not (Test-Path $destinationFolder)) {
$testPathResult = Test-Path $destinationFolder
if ($debug2) { $output2 += "`nTest-Path Ergebnis für ${destinationFolder}: ${testPathResult}" }
$newItemResult = New-Item -ItemType Directory -Path $destinationFolder
if ($debug2) { $output2 += "`nNew-Item Ergebnis für ${destinationFolder}: ${newItemResult}" }
}
Copy-Item -Path $ddsFile.FullName -Destination $destinationFolder
if ($debug2) { $output2 += "`nCopying file: $($ddsFile.FullName) to ${destinationFolder}" } # Debugging-Ausgabe
# Erstellen Sie den Pfad zur kopierten .dds-Datei
$newDdsFilePath = Join-Path -Path $destinationFolder -ChildPath $ddsFile.Name
$mapping += [PSCustomObject]@{
OldDdsFilePath = $ddsFile.FullName
NewDdsFilePath = $newDdsFilePath
DDSFormat = $result
}
$totalCopiedFiles++
if ($debug2) { $output2 += "`n`$mapping has $(($mapping | Measure-Object).Count) items after adding a new item" }
} else {
if ($debug2) { $output2 += "`nKeine .dds-Datei gefunden für $($file.FullName)" }
}
}
if ($debug2) {
$output2 += "`n`$mapping has $(($mapping | Measure-Object).Count) items before Export-Csv"
$output2 += "`n`$csvPath is: $csvPath"
}
# Versuch, die $mapping-Variable in eine Datei mit der Add-Content-Funktion anstelle von Export-Csv zu schreiben
try {
$mapping | ConvertTo-Csv -NoTypeInformation -Delimiter "," | Out-String | Set-Content -Path $csvPath -ErrorAction Stop
} catch {
if ($debug2) { $output2 += "`nFehler beim Schreiben der CSV-Datei: $($_.Exception.Message)" }
}
# Ausgabe des Ergebnisses des `Set-Content`-Befehls
$result = $mapping | ConvertTo-Csv -NoTypeInformation -Delimiter "," | Out-String | Set-Content -Path $csvPath
if ($debug2) { $output2 += "`n`$result is: $result" }
# Ausgabe der Gesamtanzahl der gefundenen .dds-Dateien, der kopierten .dds-Dateien, der gefundenen .txt-Dateien mit "DDSFormat=" und der gefundenen .txt dateien mit "DDSFormat=", "Width=" und "Height="
if ($debug1) {
$output1 += "`nEs wurden insgesamt $totalDdsFiles .dds-Dateien gefunden."
$output1 += "`nEs wurden insgesamt $totalCopiedFiles .dds-Dateien kopiert."
$output1 += "`nEs wurden insgesamt $totalTxtFiles .txt-Dateien mit 'DDSFormat=' gefunden."
$output1 += "`nEs wurden insgesamt $totalTxtFiles .txt-Dateien mit 'DDSFormat=', 'Width=' und 'Height=' gefunden."
}
# Nachdem alle Kopiervorgänge abgeschlossen sind
$specialFolders = Get-ChildItem -Path $outputFolder -Directory -Recurse | Where-Object { $_.Name -match "_\d+$" }
$totalCopiedDuplicates = 0
foreach ($folder in $specialFolders) {
$totalCopiedDuplicates += (Get-ChildItem -Path $folder.FullName -File | Measure-Object).Count
}
if ($debug2) {
$output2 += "`nEs wurden insgesamt $totalCopiedDuplicates Duplikate in 'Sonderordner' kopiert."
}
# Ausgabe der Gesamtanzahl der gefundenen .txt-Dateien mit "Width=", der gefundenen .txt-Dateien mit "Height=" und der gefundenen .dds-Duplikate
if ($debug2) {
$output2 += "`nEs wurden insgesamt $totalTxtFilesWithWidth .txt-Dateien mit 'Width=' gefunden."
$output2 += "`nEs wurden insgesamt $totalTxtFilesWithHeight .txt-Dateien mit 'Height=' gefunden."
}
# Durchsuchen Sie den Output-Ordner und alle seine Unterordner nach .dds-Dateien und zählen Sie diese
if ($debug3) {
$ddsFilesInOutputFolder = Get-ChildItem -Path $outputFolder -Recurse -Filter *.dds
$totalDdsFilesInOutputFolder = ($ddsFilesInOutputFolder | Measure-Object).Count
$output3 += "`nEs wurden insgesamt $totalDdsFilesInOutputFolder .dds-Dateien im Ausgabeordner gefunden."
}
# Geben Sie die Debugging-Ausgaben am Ende des Skripts aus
Write-Host $output2
Write-Host $output1
Write-Host $output3
# Am Ende des Skripts
Stop-Transcript