RS232 book cover

Centrum Outlook Express Centrum Outlook Express

OE PowerTool 4.5.5


Twój adres IP to: 3.142.198.129
Przeglądarka: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

Nazwy plików a duplikaty - rozwiązanie w PowerShellu

   Na grupie dyskusyjnej pojawił się problem generowania nazw plików. Było to w kontekście poniższego polecenia:

iconv.exe -t utf-8 "source.txt" > "target.txt"

Chodzi o to, że jeśli istnieje plik target.txt, to żeby był tworzony plik target(1).txt itd. A jeśli istnieje np. target(5).txt a nie istnieje target(4).txt to żeby i tak był tworzony target(6).txt. Skrypt w PowerShellu, który to zadanie realizuje, może wyglądać tak:

$filename = "target.txt"
if(Test-Path "target.txt") {
   $number = ls | foreach { if($_.Name -match "target\((\d+)\)\.txt") {$matches[1]} } | sort -InputObject {[decimal]$_} | select -last 1
   $number++
   $filename = "`"target`(" + $number + "`).txt`""
}
$command = "iconv.exe -t utf-8 source.txt > " + $filename
Invoke-Expression $command