Saturday, May 2, 2020

Adding datestamp to a filename using a function with a filename as a parameter in Powershell 7 in Debian 10

function adddatetofilename{
    Param(
        [Parameter(Mandatory=$true,position=1)]
        [string]$filename

    )
   
#$filename="\home\sourav\test.txt"
new-item -type file -name $filename
$todaysdate=get-date -Format MMddyy
$file=Get-ChildItem $filename 
$newfilename=$file.BaseName+"."+$todaysdat+$file.Extension
Rename-Item -path $filename -NewName $newfilename -verbose
}
adddatetofilename -filename ./test.txt

No comments:

Post a Comment