user1664305
user1664305

Reputation: 179

Batch or Powershell rename folder to yesterday's date

I am building out a batch file that takes a zip archive which is uploaded nightly and unzips the contents and puts it in a specific folder.

Currently, I am generating a temporary folder named "tmpExtract". Once the files are extracted, I want to rename "tmpExtract" with yesterday's date. Preferably in MM-DD-YYYY format.

I was able to locate a PowerShell command to get yesterdays date, however, I am stuck at the part where I can rename the directory to yesterdays date.

PowerShell $date = Get-Date; $date=$date.AddDays(-1); $date.ToString('yyyy-MM-dd')

The full directory that I am using: Y:\\Archive\TestFolder\Recordings\tmpExtract\

The full directory that I am looking for once the rename has completed: Y:\\Archive\TestFolder\Recordings\06-09-2021

I am open to either PowerShell or Batch/CMD. Whichever works best!

Upvotes: 1

Views: 1419

Answers (1)

user1664305
user1664305

Reputation: 179

Based on Theo's response I found the following code to work best

powershell Get-Item -Path 'Y:\Archive\TestFolder\Recordings\tmpExtract' | powershell Rename-Item -path 'Y:\Archive\TestFolder\Recordings\tmpExtract' -NewName ('{0:dd-MM-yyyy}' -f (Get-Date).AddDays(-1))

I needed to add powershell and -Path 'Y:\Archive\TestFolder\Recordings\tmpExtract' to the second part of the string. After testing it works as intended.

Also, I was unaware that the second '' was not needed for a mapped network path.

Upvotes: 1

Related Questions