0

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...

J.D.
  • 3
  • 2
  • 1
    I hope you're nolt expecting the SO community to write the script for you, that's not how SO works. What may help is to break down the problem in smaller pieces so you can search more focussed. You have filenames with a trailing numeric part. You could extract these using a RegEx. Once you have the number and filename you could create a `Dictionary` with the filename as key and the numeric part as value, sort is on value, then pick topmost (or last) key to get the file. That's just one approach. – Rno Aug 12 '20 at 20:36
  • That's a great idea... I was not looking for anyone to write a script btw... Just guidance. I will explore what you suggested. Thank you! I am still open to more suggestions though. I know there is a plethora of ways that this might be accomplished. But you just gave me a solid starting place! I appreciate that! – J.D. Aug 12 '20 at 22:14
  • You should first replacing smart quotes with this batch file [Replacing_SmartQuotes.bat](https://pastebin.com/9Qcb615g) .Just drag and drop your file over this batch and it will generate a new file with standard quotes. And don't forget to edit your question after modification has taken. – Hackoo Aug 13 '20 at 08:32

1 Answers1

0

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