Инструменты пользователя

Инструменты сайта


windows:scripts:powershell

Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слеваПредыдущая версия
Следующая версия
Предыдущая версия
windows:scripts:powershell [2025/11/13 16:35] windows:scripts:powershell [2025/11/13 16:43] (текущий)
Строка 1: Строка 1:
 ====== PowerShell ====== ====== PowerShell ======
- +[[.:powershell:timestamp_file|timestamp_file]]
-Создать копию текущего файла с меткой времени в имени.\\ +
- +
- +
-<code> +
-# ========================================================== +
-# File: timestamp_file.ps1 +
-# Purpose: Append current date/time to filename +
-# Modes: +
-#   - Rename : переименовать файл +
-#   - Copy   : создать копию с меткой времени +
-# Format: +
-#   NAME.txt -> NAME_2025-11-08_19-37-01.txt +
-# Usage: +
-#   .\timestamp_file.ps1 -Path "C:\path\file.txt" -Mode Copy +
-# ========================================================== +
- +
-param( +
-    [Parameter(Mandatory = $true)] +
-    [string]$Path, +
- +
-    [ValidateSet("Rename","Copy")] +
-    [string]$Mode = "Copy" +
-+
- +
-# Проверяем существование файла +
-if (-not (Test-Path -Path $Path -PathType Leaf)) { +
-    Write-Host "[!] File not found:" $Path -ForegroundColor Red +
-    exit 1 +
-+
- +
-# Разбираем путь и имя +
-$dir  = Split-Path $Path +
-$name = [System.IO.Path]::GetFileNameWithoutExtension($Path) +
-$ext  = [System.IO.Path]::GetExtension($Path) +
- +
-# Временная метка +
-$timestamp = (Get-Date).ToString("yyyy-MM-dd_HH-mm-ss"+
-$newName = "${name}_${timestamp}${ext}" +
-$newPath = Join-Path $dir $newName +
- +
-# Проверка существования целевого файла +
-if (Test-Path $newPath) { +
-    Write-Host "[!Target file already exists:" $newPath -ForegroundColor Yellow +
-    exit 1 +
-+
- +
-# Выполняем действие +
-switch ($Mode) { +
-    "Rename"+
-        Rename-Item -Path $Path -NewName $newName +
-        Write-Host "[✓] File renamed to:" $newName -ForegroundColor Cyan +
-    } +
-    "Copy"+
-        Copy-Item -Path $Path -Destination $newPath +
-        Write-Host "[✓] Copy created:" $newName -ForegroundColor Cyan +
-    } +
-+
- +
-</code>+
windows/scripts/powershell.1763040909.txt.gz · Последнее изменение:

Если не указано иное, содержимое этой вики предоставляется на условиях следующей лицензии: CC Attribution 4.0 International
CC Attribution 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki