' ==========
' Kill@Files
' ==========
' Version 1.0.0.1 - May 28th 2018
' Copyright © Steve MacGuire 2018
' http://samsoft.org.uk/iTunes/Kill@Files.vbs
' Please visit http://samsoft.org.uk/iTunes/scripts.asp for updates

' =======
' Licence
' =======
' This program is free software: you can redistribute it and/or modify it under the terms
' of the GNU General Public License as published by the Free Software Foundation, either
' version 3 of the License, or (at your option) any later version.

' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
' without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
' See the GNU General Public License for more details.

' Please visit http://www.gnu.org/licenses/gpl-3.0-standalone.html to view the GNU GPLv3 licence.



' ===========
' Description
' ===========

' A VBScript to clean unwanted files of the form <normal filename>@<32 hex digits>.<ext>
' In response to this thread at Apple Support Communities: https://discussions.apple.com/thread/8404099



' =========
' ChangeLog
' =========
' Version 1.0.0.1 - Initial GNU GPLv3.0 Release.


' ==========
' To-do List
' ==========
' Think of more things to do.


' ============
' Declarations
' ============

Option Explicit	        ' Declare all variables before use
Dim FSO,SAO             ' File System and shell objects
Dim Path                ' Path to be processed
Dim nl                  ' New line string
Dim Title               ' Script title
Dim cp,cr               ' Count processed folders, removed files

Title="Kill @ Files"


' ============
' Main program
' ============

Init                            ' Set things up
DoFolders FSO.GetFolder(Path)   ' Do what needs to done
Report                          ' Summary of what happened

' ===================
' End of main program
' ===================



' ===============================
' Declare subroutines & functions
' ===============================


' Process folders recursively
' Modified 2018-05-28
Sub DoFolders(Folder)
  Dim S,objFolder
' Do something with each folder on entry, breadth first
  cp=cp+1
' Do something with each subfolder
  Set objFolder=FSO.GetFolder(Folder)
  For Each S in objFolder.SubFolders
    DoFolders S.Path
  Next
' Do something with each folder on exit, depth first
  DoTidy(Folder)
End Sub


' Search folder for unwanted files and delete if found
' Modified 2018-05-28
Sub DoTidy(Folder)
  Dim F,objFolder,P
  Set objFolder=FSO.GetFolder(Folder)
  ' MsgBox "Processing folder:" & nl & Folder,0,Title
  For Each F in objFolder.Files
    ' MsgBox "Processing file:" & nl & F.Name,0,Title
    P=InStrRev(F.Name,"@")
    If P>0 And Len(F.Name)-P=36 Then
      ' MsgBox "Found file:" & nl & F.Name & nl & "P=" & P & nl & "Len(F.Name)-P=" & Len(F.Name)-P,0,Title
      ' Delete this file!
      FSO.DeleteFile F.Path,True
      cr=cr+1
    End If
  Next
End Sub


' Open a Browse for folder dialog and return the selected folder path
' Modified 2018-05-28
Function GetFolder(P)
  ' Set constants required for browse folder dialog
  Const MY_COMPUTER=&H11&
  Const WINDOW_HANDLE=0
  Const OPTIONS=0
  Dim objFolder,objFolderItem,strStart
  Set objFolder=SAO.Namespace(MY_COMPUTER)
  Set objFolderItem=objFolder.Self
  strStart = objFolderItem.Path
  Set objFolder=SAO.BrowseForFolder (WINDOW_HANDLE, P, OPTIONS, strStart) 
  If objFolder Is Nothing Then
    GetFolder=""
  Else
    Set objFolderItem=objFolder.Self
    If Left(objFolderItem.Path,2)="::" Then
      MsgBox "That item cannot be processed, please try again.",0,Title
    Else 
      GetFolder=objFolderItem.Path
      If Right(GetFolder,1)="\" Then GetFolder=Left(GetFolder,Len(GetFolder)-1)
    End If
  End If
End Function


' Set things up
' Modified 2018-05-28
Sub Init
  ' Initialise counters
  cp=0 : cr=0
  ' Get FileSystemObject to allow script access to files
  Set FSO=CreateObject("Scripting.FileSystemObject")
  ' Get Application Object to allow access to browse folder dialog
  Set SAO=CreateObject("Shell.Application")
  ' Newline string for text output
  nl=vbCrLf
  ' Get source & target folders, abort if dialog is cancelled
  Path=GetFolder("Select the Media folder to scan for unwanted file:" & nl & "E.g. My Documents\My Music\iTunes\iTunes Media")
  If Path="" Then wScript.quit
  ' Confirm action before proceeding
  If Not MsgBox("About to clean unwanted files of the form" & nl & "<normal filename>@<32 hex digits>.<ext> from" & nl & Path,vbQuestion+vbOKCancel,Title)=vbOK Then wScript.quit
End Sub


' Return relevant string depending on whether value is plural or singular
' Modified 2011-10-04
Function Plural(V,P,S)
  If V=1 Then Plural=S Else Plural=P
End Function


' Report summary of activity when task complete or process aborted
' Modified 2018-05-28
Sub Report
  Dim R
  R=cp & " folder" & Plural(cp,"s were"," was") & " processed and " & cr & " file" & Plural(cr,"s were"," was") & " removed."
  MsgBox R,0,Title
End Sub



' =============
' End of script
' =============