Shortcut that points to folder named the current date. YYYY_MM_DD Format

ぃ、小莉子 提交于 2019-12-11 09:02:15

问题


So I am part of daily project. Each day, we create a new folder where we store all of our files for that day. It is named according to the current date, with a counter added to the front representing the 'episode'. The format is WXYZ_YYYY_MM_DD.

EX:

0001_2013-05-09

0002_2013-05-10

0003_2013-05-13

0004_2013-05-14

The folders are already being created, but I need to make a shortcut that will always take you to the 'current' folder for the day.

After working out my options, it seems like Powershell will be the most straightforward. I know I need to use scheduler here, but am torn between deleting existing shortcuts to create a new one, or editing existing shortcut path values. Not exactly sure how to increment my episode and append it to the date value. Do I want to be appending strings here?

I'm much more versed in C++, and Java than shells. Haven't worked with them in a long while so any help here is appreciated.


回答1:


You can create shortcuts in powershell using the following.

$sh = New-Object -ComObject WScript.Shell
$shortCut = $sh.CreateShortcut("C:\latest_folder.lnk")
$shortCut.TargetPath = "C:\foo\bar.txt"
$shortCut.Save()

You don't even need to remove the old one each time. If you just execute the same code it will overwrite the old shortcut.

For grabbing the latest folder you can just use the following line.

$file = Get-ChildItem -Path "c:\Foo" | Sort-Object -Descending LastWriteTime | select -First 1

$file.FullName will then give you the full path to that folder/file.




回答2:


Try this out

It will create the shortcut to the new directory

$File = Get-ChildItem -Path "C:\Foo" | ?{ $_.PSIsContainer } | Sort-Object -Descending Name | select -First 1

[int]$Counter = ($File -Split("_"))[0]
$Counter += 1
$NewFolder = ("{0:000#}" -f $Counter) + "_" + (Get-Date -Format "yyyy-MM-dd")


$Shell = New-Object -ComObject WScript.Shell
$Shortcut = $Shell.CreateShortcut("C:\foo\latest_folder.lnk")
$Shortcut.TargetPath = "C:\Foo\" + $Newfolder
$Shortcut.Save()


来源:https://stackoverflow.com/questions/16469899/shortcut-that-points-to-folder-named-the-current-date-yyyy-mm-dd-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!