Reputation: 127
I'm trying to do the following three steps with a powershell script:
drop the file extension (.part) of all files ending with the pattern "*flv.part". So files ending with ".flv.part" would now end simply with ".flv".
Add yesterday's date (in YYYYMMDD format) at the beginning of the filename of these FLV files.
Move these flv files to some other directory.
I figured out the first and the last step. But I can't figure out the middle step (the second step where I add yesterday's date to the beginning of the filename).
Here's what I have figured out:
cd C:\Users\appa\Desktop
Dir | Rename-Item -NewName { $_.name -replace ".part","" }
---------this step needs to add yesterday's date (in YYYYMMDD format) to filename--------
move-item -path .\*.flv -destination d:\D:\VideoCaptures
Any ideas anyone? I'm using windows 7 with powershell v2.
EDIT: Thanks everyone for your contributions! I have made the following script for this task:
cd C:\Users\appa\Desktop
$dt_str = (get-date).AddDays(-1).tostring("yyyyMMdd")
Dir *.part | Rename-Item -NewName { $dt_str + "-" + $_.name -replace ".part","" }
move-item -path .\*.flv -destination D:\VideoCapture
EDIT: This is purely for FYI purposes...I've already up-voted the helpful posts and marked a post as an answer, but SU says that it won't reflect my votes until I reach 10 or something reputation. So please wait till that time for my votes to become "visible"! :)
Upvotes: 2
Views: 5309
Reputation: 7350
Read about Get-Date method and Formatting Dates , please.
For example for current date:
$dt = Get-Date -format 'yyyyMMdd'
or for yesterday
$dt = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
Other functions you can use:
Get-Item -Path "C:\Windows\*.flv.part"
[System.IO.Path]::ChangeExtension(".flv.part", ".flv")
Maybe something like this:
$dt = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
Get-Item -Path "C:\Windows\*.flv.part" | Foreach-Object -Process {
$newpath = Join-Path -Path $_.DirectoryName -ChildPath "$dt-$($_.BaseName).flv" # in PowerShell "" it's a Magic String
Move-Item -Path $newpath -Destination "??"
}
Please be aware about PowerShell’s Type Conversion Magic in PowerShell ;)
Upvotes: 2
Reputation: 10050
Try this:
$date = Get-Date
$date = $date.AddDays(-1)
$dateStr = $date.ToString("yyyyMMdd")
Dir | Rename-Item -NewName { $dateStr + $_.name -replace ".flv.part",".flv"}
Upvotes: 7