I have bunch of SQL Server backup files with name like this:
master_backup_201308172100.bak
It has date time as the string in it. When I need to run the database restore script, I have to trim off the date time to make the file name like this:
master_backup.bak
Otherwise I have to update the database restore script each time I restore a different day’s backup. A PowerShell script comes to help:
<span style="color: #5f9ea0; font-weight: bold;">dir</span><span style="color: #000000;"> | </span><span style="color: #5f9ea0; font-weight: bold;">rename-item</span><span style="color: #5f9ea0; font-style: italic;"> -newname</span><span style="color: #000000;"> {</span><span style="color: #800080;">$_</span><span style="color: #000000;">.Name.SubString(</span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #800080;">$_</span><span style="color: #000000;">.Name.IndexOf(</span><span style="color: #800000;">"</span><span style="color: #800000;">2013</span><span style="color: #800000;">"</span><span style="color: #000000;">) </span><span style="color: #ff0000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">) </span><span style="color: #ff0000;">+</span><span style="color: #800080;">$_</span><span style="color: #000000;">.extension}</span>
Have fun!!