Search code examples
vbscriptforeachcomparemove

VBScript - Find files in folder by string in filename, move found to different folder


I am still learning VBS, not sure if I'm going about this in the correct, or most efficient manner. The test scenario is as follows, in addition to regular desktop items I've added three .txt files named:

"Tool - YouTube"
"welcome to facebook"
"BBC news"

When my code (bottom) runs it creates "Sorted" folder as intended (if it doesn't exist), but only the "BBC news" text file is found and moved by the 'instr' function. Furthermore if the "Sorted" folder already exists with "BBC news" text file within, then running the script will return.

Line 20
Char 2
Error: File already exists
Code: 800A003A

The script when working should find any files in a 'source' folder according to a search string and move them to another 'destination' folder. If a duplicate exists in the 'destination' folder, it should be replaced by the file found in the 'source' folder. Please can anyone explain what changes I should apply to get my script working?

dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3

sourcefolder = "C:\Users\...\Desktop"
destfolder = "C:\Users\...\Desktop\Sorted\"
searchname1 = "youtube"
searchname2 = "bbc"
searchname3 = "facebook"

set fso = createobject("scripting.filesystemobject") 
set folder = fso.getfolder(sourcefolder)  

if not fso.folderexists(destfolder) then
    newfolder = fso.createfolder(destfolder)
    wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
end if

for each file in folder.files
    x = fso.getbasename(file)
    if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) or instr(lcase(x), searchname3) then
        fso.movefile sourcefolder & "\" & file.name, destfolder
        wscript.echo"Files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
        wscript.quit()
    else
        wscript.echo "No matches found"
        wscript.quit()
    end if
next

Solution

  • Here you go, I fixed your if/then statements and added and if fso.fileexists:

    dim fso, folder, newfolder, sourcefolder, destfolder, searchname1, searchname2, searchname3
    
    sourcefolder = "C:\Users\...\Desktop"
    destfolder = "C:\Users\...\Desktop\Sorted\"
    searchname1 = "youtube"
    searchname2 = "bbc"
    searchname3 = "facebook"
    
    set fso = createobject("scripting.filesystemobject") 
    set folder = fso.getfolder(sourcefolder)  
    
    if not fso.folderexists(destfolder) then
        newfolder = fso.createfolder(destfolder)
        wscript.echo "'Sorted' folder created in path: " & vbcrlf & sourcefolder
    end if
    
    for each file in folder.files
        x = fso.getbasename(file)
        if instr(lcase(x), searchname1) > 0 or instr(lcase(x), searchname2) > 0  or instr(lcase(x), searchname3) > 0 then
            if fso.fileexists(destfolder & "\" & file.name) then
                fso.deletefile destfolder & "\" & file.name, true
                fso.movefile sourcefolder & "\" & file.name, destfolder
            else
                fso.movefile sourcefolder & "\" & file.name, destfolder
            end if
        end if
    next
    
    wscript.echo "Files moved to 'Sorted' in path: " & vbcrlf & sourcefolder