11/08/2012

Dropbox Camera Upload功能的BUG修复


  作为一个Dropbox Camera Upload功能的资深用户,对于我Camera Uploads文件夹里数百个类似2002-12-08 12.00.00-1-2.jpg这种文件名的文件表示不能忍。
  我本来以为是MIUI的Bug,后来MIUI刷了几个月都没能解决。换了原生系统这毛病依然还在,果断去Dropbox的官方论坛搜索之。发现是有些拍照应用拍出来照片的EXIF信息出错造成的(感谢 Lihiniya https://forums.dropbox.com/topic.php?id=91810#post-504267)。虽然文件名和创建文件的时间没有了意义,但文件修改时间和拍照时间相差不大。于是打算用文件修改时间来修改文件名。
  开工,用Autohotkey写。基本思路是遍历Camera Uploads的所有文件,找出需要重命名的文件,然后一个一个的处理。好久没写过AHK了,花了两个多小时才写完。
  使用方法:直接打开exe文件,然后按开始键+2,输入你的Camera Uploads文件夹的地址,回车,它就会自动修改错误的文件名。PS:如果文件重名不会覆盖,会提示你无法重命名的文件名。
  编译好的exe文件链接:https://www.dropbox.com/s/gpthiuylklui3x0/fixDropboxCameraUploadTimeBug.exe
  源代码链接:https://www.dropbox.com/s/wnl6sg13qnc47e4/fixDropboxCameraUploadTimeBug.ahk
  源代码如下:
#2::
   InputBox, photoPath, Please Input the PATH of Camera Uploads , like:D:\Dropbox\Camera Uploads, , 300,128   ;input setting
   if ErrorLevel
      MsgBox, Exit,88~
   else
   {
      photoPath := RegExReplace(photoPath,"\\$","")   ;删除末尾多余的\ | Delete the extra \ in the end of the path
      ;MsgBox, You entered "%photoPath%"
      Loop, %photoPath%\*.*, 0, 1    ;Loop the input fold
      {
          ;MsgBox, 4, , Filename = %A_LoopFileFullPath%`n`nContinue?
          tempFile = %A_LoopFileFullPath%
          FoundPos := RegExMatch(tempFile, "2002-12-08 12.00.00")   ;detect one file is a file with time bug
          if FoundPos
          {
             ;msgbox %tempFile% %FoundPos%
             renameOneFile(tempFile)   ;process one file
          }
      }
   }
return

renameOneFile(filePath)
{
   FileGetTime, modifiedTime, %filePath%    ;get the modified time
   ;tempFileFullPath := RegExReplace(modifiedTime,"(\d4)(\d2)(\d2)(\d2)(\d2)(\d2)","$1-$2-$3 $4.$5.$6")
   tempFileFullPath := RegExReplace(modifiedTime,"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})","$1-$2-$3 $4.$5.$6")    ;convert only number time to the Dropbox format file name
   tempFileFullPath := RegExReplace(filePath,"(?<=\\)[\d-\. ]{19,}(?=.[a-z]{3})",tempFileFullPath)  ;add path
   ;msgbox %filePath%`n%tempFileFullPath%
   FileMove, %filePath%, %tempFileFullPath%, 0   ;change the file name
   if ErrorLevel
     msgbox %filePath%      ;msg the file path that cannot be changed for user
   return
}