Advertisement
jcunews

AppendFileDateToFileName.vbs

Nov 15th, 2021
1,803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fmt = "mdy" 'code for default date format
  2.  
  3. sub help
  4.   wsh.echo "Usage:" & vblf & vblf & _
  5.     "Append file date to given file:" & vblf & _
  6.     "  AppendFileDateToFileName [option] {file...}" & vblf & vblf & _
  7.     "Add to context menu:" & vblf & _
  8.     "  AppendFileDateToFileName /addmenu" & vblf & vblf & _
  9.     "Remove from context menu:" & vblf & _
  10.     "  AppendFileDateToFileName /delmenu" & vblf & vblf & _
  11.     "Option:" & vblf & vblf & _
  12.     "/mdy" & vbtab & "Use mm-dd-yyyy format (default)." & vblf & _
  13.     "/ymd" & vbtab & "Use yyyy-mm-dd format." & vblf & _
  14.     "/dmy" & vbtab & "Use dd-mm-yyyy format."
  15.   wsh.quit
  16. end sub
  17.  
  18. function zerolead(s)
  19.   zerolead = right("0" & s, 2)
  20. end function
  21.  
  22. if wsh.arguments.count = 0 then help
  23. set fs = createobject("scripting.filesystemobject")
  24. set ws = createobject("wscript.shell")
  25. on error resume next
  26. if wsh.arguments.named.exists("addmenu") then
  27.   ws.regwrite "HKCR\*\shell\AppendFileDateToFileName\", _
  28.     "Append file date to file name"
  29.   if err.number = 0 then
  30.     ws.regwrite "HKCR\*\shell\AppendFileDateToFileName\command\", _
  31.       "wscript.exe """ & wsh.scriptfullname & """ ""%1"""
  32.   end if
  33.   if err.number <> 0 then wsh.echo err.description
  34.   wsh.quit
  35. elseif wsh.arguments.named.exists("delmenu") then
  36.   ws.regdelete "HKCR\*\shell\AppendFileDateToFileName\command\"
  37.   ws.regdelete "HKCR\*\shell\AppendFileDateToFileName\"
  38.   if err.number <> 0 then wsh.echo err.description
  39.   wsh.quit
  40. end if
  41. if wsh.arguments.unnamed.count = 0 then help
  42. if wsh.arguments.named.exists("mdy") then
  43.   fmt = "mdy"
  44. elseif wsh.arguments.named.exists("ymd") then
  45.   fmt = "ymd"
  46. elseif wsh.arguments.named.exists("dmy") then
  47.   fmt = "dmy"
  48. end if
  49. for each fn in wsh.arguments.unnamed
  50.   if fs.fileexists(fn) then
  51.     set f = fs.getfile(fn)
  52.     select case fmt
  53.       case "mdy"
  54.         s = zerolead(datepart("m", f.datelastmodified)) & "-" & _
  55.           zerolead(datepart("d", f.datelastmodified)) & "-" & _
  56.           datepart("yyyy", f.datelastmodified)
  57.       case "ymd"
  58.         s = datepart("yyyy", f.datelastmodified) & "-" & _
  59.           zerolead(datepart("m", f.datelastmodified)) & "-" & _
  60.           zerolead(datepart("d", f.datelastmodified))
  61.       case "dmy"
  62.         s = zerolead(datepart("d", f.datelastmodified)) & "-" & _
  63.           zerolead(datepart("m", f.datelastmodified)) & "-" & _
  64.           datepart("yyyy", f.datelastmodified)
  65.     end select
  66.     i = instrrev(f.name, ".")
  67.     if i > 0 then
  68.       n = left(f.name, i - 1)
  69.       e = mid(f.name, i)
  70.     else
  71.       n = f.name
  72.       e = ""
  73.     end if
  74.     err.clear
  75.     f.name = n & " " & s & e
  76.     if err.number <> 0 then wsh.echo err.description & ": " & f.path
  77.   else
  78.     wsh.echo "Not found: " & fn
  79.   end if
  80. next
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement