AutoHotkey: Create shortcuts keeping timestamps

From TotalcmdWiki
Jump to navigation Jump to search

This script creates shortcuts in the target panel to files selected in the source panel while keeping the shortcuts' timestamps the same as those of the original files.

Loop, Read, %1%
{
	SplitPath, A_LoopReadLine, , , , FileName
	FileGetTime, TimeStamp, %A_LoopReadLine%
	FileCreateShortcut, %A_LoopReadLine%, %2%%FileName%.lnk
	FileSetTime, %TimeStamp%, %2%%FileName%.lnk
}

To use, adda button to the Button Bar (or create a menu entry or user command for an alias or a keyboard shortcut) with the following parameters:

"%L" "%T\"

Alternative version to also process files in selected subdirectories:

Loop, Read, %1%
{
   FileGetAttrib, Attrib, %A_LoopReadLine%
   IfInString, Attrib, D
   {
      RegExMatch(A_LoopReadLine, ".*\\\K.*?(?=\\)", TopDirName)
      StringLen, SourceAbsolutePathLength, A_LoopReadLine
      SourceAbsolutePathLength--
      Loop, Files, %A_LoopReadLine%*.*, R
      {
         SplitPath, A_LoopFileLongPath, FileNameWithExt, SourceSubDirPath, , FileName
         StringTrimLeft, TargetSubDirPath, SourceSubDirPath, SourceAbsolutePathLength
         FileGetTime, TimeStamp, %A_LoopFileLongPath%
         FileCreateDir, %2%%TopDirName%%TargetSubDirPath%
         FileCreateShortcut, %A_LoopFileLongPath%, %2%%TopDirName%%TargetSubDirPath%\%FileName%.lnk
         FileSetTime, %TimeStamp%, %2%%TopDirName%%TargetSubDirPath%\%FileName%.lnk
      }
      Continue
   }

   SplitPath, A_LoopReadLine, FileNameWithExt, , , FileName
   FileGetTime, TimeStamp, %A_LoopReadLine%
   FileCreateShortcut, %A_LoopReadLine%, %2%%FileName%.lnk
   FileSetTime, %TimeStamp%, %2%%FileName%.lnk 
}

If you would like to include the file extension in the shortcut (filename.ext.lnk instead of filename.lnk), replace %FileName% with %FileNameWithExt% in all four FileSetTime and FileCreateShortcut lines.

Back to AutoHotkey