A short introduction. As a consultant, customers can send me files every day, for inspection/testing/output/... Emails are sent to Gmail. I need to download and organize these incoming files properly. I download the files from Gmail to my Downloads folder.
I created a working script to:
- close the 'Show all' button in Google Chrome (notification bar at the bottom of the browser page). It's annoying.
- ask for a customer name. The top 5 customers can be chosen with 1 / 2 / 3 / 4 / 5. Otherwise, enter a name
- ask for the number of most recent files I download. Usually 1 (default value) but it could be much more too. Say, x files.
- get the x most recent files from the Downloads folder
- move the files to a folder containing:
* customer name
* the month (yyyy-mm)
* the day (yyyy-mm-dd)
- files are moved, so deleted from the Downloads folder. I don't need them twice
- in case a zip file is among the most recent x files, it's unzipped in the destination folder using 7zip. Criteria: file extension is 7z / zip / rar
- a new Windows Explorer window is started, pointing to the first file in the destination folder
The shortcut is Ctrl-Shift-F5, start if from the Gmail page / Google Chrome browser. The listing below is self-contained.
Any remarks and improvements are welcome ! Enjoy !
Code: Select all
^+F5::
{
WinGetClass, class, A
if (class="Chrome_WidgetWin_1")
{
Send ^j
Sleep 1000
Send ^w
}
InputBox, Customer, Folder for customer,Choose your customer:`n`n (1) = Enter customer name 1`n (2) = Enter customer name 2`n (3) = Enter customer name 3`n (4) = Enter customer name 4`n (5) = Enter customer name 5,,,300,,,,,1
If ErrorLevel
{
MsgBox, %c_Msg_Cancel%
return
}
if Customer is integer
{
if Customer = 1
Customer = Enter customer name 1
else if Customer = 2
Customer = Enter customer name 2
else if Customer = 3
Customer = Enter customer name 3
else if Customer = 4
Customer = Enter customer name 4
else if Customer = 5
Customer = Enter customer name 5
}
else
{
; MsgBox, %c_Msg_Invalid_Choice%
; this combination is allowed too ==> create your own new case
; Return
}
; getting the day and month
myNow := % A_Now
FormatTime, currentMonth, % myNow, yyyy-MM
FormatTime, currentDay, % myNow, yyyy-MM-dd
sFilePath = D:\Consultancy\%Customer%\%currentMonth%\%currentDay%
if isValidFileName(sFilePath) != 0
{
msgbox Something's wrong: %sFilePath%
}
else
{
InputBox, nr_of_files, , Enter number of most recent files to be moved:,,,,,,,, 1
If ErrorLevel
{
MsgBox, %c_Msg_Cancel%
return
}
if (nr_of_files >= 0)
FileCreateDir %sFilePath%
{
loop % nr_of_files
{
modTime:=0
newestFile :=
newestFile_File_Only :=
newestFile_is_zip := 0
Loop, D:\Z_Rest\Downloads\*.*
{
If ( SubStr( A_LoopFileName, 1, 1 ) != "~" )
{
If modTime < %A_LoopFileTimeModified%
{
modTime := A_LoopFileTimeModified
newestFile := A_LoopFileFullPath
newestFile_File_Only := A_LoopFileName
if A_LoopFileExt in "zip,7z,rar,"
newestFile_is_zip = 1
}
}
}
; Track and move the most recent file
If newestFile !=
{
FileDelete, %sFilePath%\%newestFile_File_Only%
; 7 zip unzipping (based on: https://autohotkey.com/board/topic/87699-express-7-zip-extract/)
if newestFile_is_zip = 1
{
if (A_Is64bitOS = 1)
runwait, "C:\Program Files\7-Zip\7z.exe" x "%newestFile%" -o"%sFilePath%" -y,,hide
else
runwait, "C:\Program Files (x86)\7-Zip\7z.exe" x "%newestFile%" -o"%sFilePath%" -y,,hide
; msgbox, 7zip has finished extracting "%newestFile%".
FileDelete, %newestFile%
}
else
FileMove, %newestFile%, %sFilePath%
}
}
}
; select the first file in the output folder, if not possible, select the folder
Sleep 300
Loop, %sFilePath%\*.*
{
; Run, %A_LoopFileLongPath%
found := 1
Run %COMSPEC% /c explorer.exe /select`, "%A_LoopFileLongPath%",, Hide
break
}
If Not Found
Run %COMSPEC% /c explorer.exe /select`, "%sFilePath%",, Hide
}
}
return
isValidFileName(_fileName, _isLong=true)
{
forbiddenChars := _isLong ? "[<>|""\\/:*?]" : "[;=+<>|""\]\[\\/']"
ErrorLevel := RegExMatch( _fileName , forbiddenChars )
Return ! ErrorLevel
}
return