PowerShell: Select file with specific name from the folder and rename it by adding the current date -
i sincerely apologize if question seems easy. i'm new powershell , i'm still learning basics.
i have folder e:\lastmonthbackup
where sql server everyday adds 1 new file named projectbackup.bak
i need powershell script rename newly added file adding date when file created.
so in essence want each new file named projectbackup_year_month_day.bak
i don't want script touch files have dates in names. 1 new projectbackup.bak file should renamed.
could please me write such script?
i found related question here, when tried applying solution in script didn't work or maybe messed something. totally new powershell appreciated.
if 1 file want rename pattern, can try this:
$filetorename = "e:\lastmonthbackup\projectbackup.bak" if (test-path $filetorename) { $filename = [system.io.path]::getfilenamewithoutextension($filetorename) $datesuffix = get-date -format 'yyyy_mm_dd' $fileextension = [system.io.path]::getextension($filetorename) $newfilename = '{0}_{1}{2}' -f $filename, $datesuffix, $fileextension rename-item -path $filetorename -newname $newfilename }
if file $filetorename
exisits, script determines filename projectbackup
, date format yyyy_mm_dd
, file extension .bak
, formats new string. finally, renames file new name.
Comments
Post a Comment