windows_scripts_powershell_timestamp_file
Различия
Показаны различия между двумя версиями страницы.
| windows_scripts_powershell_timestamp_file [2025/11/13 16:44] – создано | windows_scripts_powershell_timestamp_file [2025/11/13 16:44] (текущий) – удалено | ||
|---|---|---|---|
| Строка 1: | Строка 1: | ||
| - | ====== timestamp_file ====== | ||
| - | Создать копию текущего файла с меткой времени в имени.\\ | ||
| - | |||
| - | Запуск через bat файл.\\ | ||
| - | Файл запускает *.ps1 файл с таким же названием передавая аргументы.\\ | ||
| - | <code batch timestamp_file.bat> | ||
| - | @echo off | ||
| - | setlocal | ||
| - | set " | ||
| - | if not exist " | ||
| - | |||
| - | set " | ||
| - | " | ||
| - | |||
| - | endlocal | ||
| - | pause | ||
| - | exit | ||
| - | </ | ||
| - | |||
| - | |||
| - | <code powershell timestamp_file.ps1> | ||
| - | # ========================================================== | ||
| - | # File: timestamp_file.ps1 | ||
| - | # Purpose: Append current date/time to filename | ||
| - | # Modes: | ||
| - | # - Rename : переименовать файл | ||
| - | # - Copy : создать копию с меткой времени | ||
| - | # Format: | ||
| - | # | ||
| - | # Usage: | ||
| - | # | ||
| - | # ========================================================== | ||
| - | |||
| - | param( | ||
| - | [Parameter(Mandatory = $true)] | ||
| - | [string]$Path, | ||
| - | |||
| - | [ValidateSet(" | ||
| - | [string]$Mode = " | ||
| - | ) | ||
| - | |||
| - | # Проверяем существование файла | ||
| - | if (-not (Test-Path -Path $Path -PathType Leaf)) { | ||
| - | Write-Host "[!] File not found:" | ||
| - | exit 1 | ||
| - | } | ||
| - | |||
| - | # Разбираем путь и имя | ||
| - | $dir = Split-Path $Path | ||
| - | $name = [System.IO.Path]:: | ||
| - | $ext = [System.IO.Path]:: | ||
| - | |||
| - | # Временная метка | ||
| - | $timestamp = (Get-Date).ToString(" | ||
| - | $newName = " | ||
| - | $newPath = Join-Path $dir $newName | ||
| - | |||
| - | # Проверка существования целевого файла | ||
| - | if (Test-Path $newPath) { | ||
| - | Write-Host "[!] Target file already exists:" | ||
| - | exit 1 | ||
| - | } | ||
| - | |||
| - | # Выполняем действие | ||
| - | switch ($Mode) { | ||
| - | " | ||
| - | Rename-Item -Path $Path -NewName $newName | ||
| - | Write-Host "[✓] File renamed to:" $newName -ForegroundColor Cyan | ||
| - | } | ||
| - | " | ||
| - | Copy-Item -Path $Path -Destination $newPath | ||
| - | Write-Host "[✓] Copy created:" | ||
| - | } | ||
| - | } | ||
| - | |||
| - | </ | ||
windows_scripts_powershell_timestamp_file.1763041455.txt.gz · Последнее изменение: —
