반응형
PowerShell을 사용하는 파일 이름의 타임 스탬프
문자열에 경로가 있습니다.
"C : \ temp \ mybackup.zip"
예를 들어, 해당 스크립트에 타임 스탬프를 삽입하고 싶습니다.
"C : \ temp \ mybackup 2009-12-23.zip"
PowerShell에서이 작업을 쉽게 수행 할 수 있습니까?
다음과 같이 $ ()와 같은 하위 식을 사용하여 큰 따옴표로 묶인 문자열에 임의의 PowerShell 스크립트 코드를 삽입 할 수 있습니다.
"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"
그리고 다른 곳에서 경로를 얻는 경우-이미 문자열로 :
$dirName = [io.path]::GetDirectoryName($path)
$filename = [io.path]::GetFileNameWithoutExtension($path)
$ext = [io.path]::GetExtension($path)
$newPath = "$dirName\$filename $(get-date -f yyyy-MM-dd)$ext"
경로가 Get-ChildItem 의 출력에서 오는 경우 :
Get-ChildItem *.zip | Foreach {
"$($_.DirectoryName)\$($_.BaseName) $(get-date -f yyyy-MM-dd)$($_.extension)"}
다음은 작동해야하는 몇 가지 PowerShell 코드입니다. 이 대부분을 더 적은 줄로 결합 할 수 있지만 명확하고 읽기 쉽게 유지하고 싶었습니다.
[string]$filePath = "C:\tempFile.zip";
[string]$directory = [System.IO.Path]::GetDirectoryName($filePath);
[string]$strippedFileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath);
[string]$extension = [System.IO.Path]::GetExtension($filePath);
[string]$newFileName = $strippedFileName + [DateTime]::Now.ToString("yyyyMMdd-HHmmss") + $extension;
[string]$newFilePath = [System.IO.Path]::Combine($directory, $newFileName);
Move-Item -LiteralPath $filePath -Destination $newFilePath;
보안 로그를 내 보내야했고 날짜와 시간이 협정 세계시로 표시되기를 원했습니다. 이것은 알아 내기가 어렵지만 실행하기는 매우 간단합니다.
wevtutil export-log security c:\users\%username%\SECURITYEVENTLOG-%computername%-$(((get-date).ToUniversalTime()).ToString("yyyyMMddThhmmssZ")).evtx
매직 코드는 바로이 부분입니다.
$(((get-date).ToUniversalTime()).ToString("yyyyMMddThhmmssZ"))
위의 스크립트에 감사드립니다. 올바르게 끝나는 파일에 추가하기위한 약간의 수정. 이 시도 ...
$filenameFormat = "MyFileName" + " " + (Get-Date -Format "yyyy-MM-dd") **+ ".txt"**
Rename-Item -Path "C:\temp\MyFileName.txt" -NewName $filenameFormat
사용하다:
$filenameFormat = "mybackup.zip" + " " + (Get-Date -Format "yyyy-MM-dd")
Rename-Item -Path "C:\temp\mybackup.zip" -NewName $filenameFormat
참고 URL : https://stackoverflow.com/questions/1954203/timestamp-on-file-name-using-powershell
반응형
'programing tip' 카테고리의 다른 글
NullInjectorError : AngularFirestore에 대한 공급자가 없습니다. (0) | 2020.09.21 |
---|---|
C #에서 IList 정렬 (0) | 2020.09.21 |
BitmapDrawable 더 이상 사용되지 않는 대안 (0) | 2020.09.21 |
파이썬에서 하나의 항목을 * 제외 *하는 모든 인덱스 (0) | 2020.09.21 |
Android 5.0 (Lollipop)에서 프로그래밍 방식으로 수신 전화에 응답하려면 어떻게해야하나요? (0) | 2020.09.21 |