powershell - 使用powershell在文件名中附加字符串
问题描述
我正在尝试在文件名中附加字符串。
$cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("India Standard Time")
$csttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $cstzone)
$d = Get-Date $csttime -f "dd-MM-yyyy"
$t = Get-Date $csttime -f "HH:mm"
Write-Host "Date : " $d
Write-Host "Time : " $t
gci C:\Result.jtl | % { rename-item –Path $_.FullName –Newname ( $_.basename + $t + $_.extension) }
预期的
结果_14:42.jtl
错误
rename-item : Cannot rename the specified target, because it represents a path or device name.
At line:10 char:25
+ ... t.jtl | % { rename-item –Path $_.FullName –Newname ( $_.basename + $t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand
解决方案
文件名中不能有冒号,它们是为驱动器号保留的。您可以使用 tostring 获取不同格式的日期:
(Get-Date).ToString("hh-mm")
推荐阅读
- java - 发现多个文件具有操作系统独立路径“META-INF/INDEX.LIST”(xyz)
- c# - 通用包装器 WEB API 集成器
- assembly - SASM 程序集 IDE 64 示例编译错误“地址大小的不可能组合”在 PRINT_STRING 消息上
- java - 如何解决错误无法使用 th:href 解析为表达式
- python-3.x - 编译项目时 auto-py-to-exe 卡在 hook-gevent.py
- javascript - JS HTML CSS:如何使函数每次使用不同的变量动态重复,而不在函数内部重复相同的变量
- javascript - 单页 Jquery/Vanilla Javascript 上超过了多个轮播最大调用堆栈
- php - PHP HTML foreach 循环
- c - C Tinkercad 模型 Arduino 在哪个 lcd 中不工作?
- python - 如何从数据框列中的值列表中获取值