Search code examples
vbscriptautomationcopyfilenamesmove

VBScript to copy/move file by lowest value in filename


I'm fairly new to scripting and am in need of some help. I have come across a unique situation for a Non-Profit client of ours that requires us to compare two or more files in a specific folder and move the file with the lowest numerical value in the filename.

This organization runs a non-profit radio station which has content submitted from hundreds of volunteers that name their files (when they record more than one) with various numbers at the end that either represent the date or the order in which the files are to be aired.

Essentially I am looking to create a vbscript (because I think it can be done this way) that will run with windows task scheduler 30 minutes prior to the first air date of the content and move the file with the lowest value (if more than one file exists) to a folder where it will be automatically processed by the radio automation software.

Examples of files in a folder might look something like these:

Folder1: (in this instance, "news.mp3" is the lowest value)

  • news.mp3
  • news1.mp3
  • news2.mp3

Folder2:

  • entertainment24.mp3
  • entertainment26.mp3

Folder3:

  • localnews081420.mp3
  • localnews081520.mp3

Honestly, on this one, I'm not even sure where to start. I've found several scripts that can look at file date or a specific numerical or date format in the filename, but none that can parse numbers from a filename and move/copy a file based on the numerical value. I'm hoping there is someone out there smarter than me that can point me in the right direction. Thanks for looking at my problem!

One script I've been playing with (from the scripting guy) looks at specific years in a filename:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colFiles = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Test’} Where ” _
        & “ResultClass = CIM_DataFile”)
Set objRegEx = CreateObject(“VBScript.RegExp”)
For Each objFile in colFiles
    objRegEx.Global = True   
    objRegEx.Pattern = “\d{4}”
    strSearchString = objFile.FileName
    Set colMatches = objRegEx.Execute(strSearchString)
    strYear = colMatches(0).Value
    strNewFile = “C:\Test\” & strYear & “\” & objFile.FileName & _
        “.” & objFile.Extension
    objFile.Copy(strNewFile)
    objFile.Delete
Next

...but I can't seem to make the leap to regular numbers and then take a lowest value...


Solution

  • You can use FileSystemObject to Work with Drives, Folders and Files.
    Also i used GETNUM function to get number.

    Try my way :

    sFolder = "C:\Test\"
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    For Each objFile in oFSO.GetFolder(sFolder).Files
        Number=GETNUM(objFile.Name)
        strNewFile = sFolder & Number & "\" & objFile.Name
    
        If NOT (oFSO.FolderExists(sFolder & Number)) Then
        oFSO.CreateFolder(sFolder & Number)
        End If
        oFSO.MoveFile objFile, strNewFile
    Next
    
    Function GETNUM(Str)
    For i=1 To Len(Str)
    if IsNumeric(Mid(Str,i,1)) Then
    Num=Num&Mid(Str,i,1)
    End if
    Next
    GETNUM=Num
    End Function
    

    For understanding the used code and how they work, open these sites and read all pages very carefully.

    MoveFile method

    Vbs Script to check if a folder exist