2

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
Albert F D
  • 529
  • 2
  • 13
  • 33
  • *Update* Regarding the replace file functionality, I've been reading that movefile cannot replace an existing file, leaving me two options as far as I see. One being to use copyfile and deleting the source file, or, checking if file exists, deleting it and then using movefile. – Albert F D Mar 11 '15 at 19:48

3 Answers3

2

Thanks very much Nathan for your contribution, I've developed the code slightly to include the "no matches" msgbox (as originally intended) and a counter as well. I have debated with myself :P whether what follows in this comment should be stated, and from the verbosity to come you can imagine the result was affirmative.

Since I've been using these forums for VBS issues Ekkehard has always been a swift, reliable source for help and guidance, I am very grateful to him for all his advice, and I have learnt a lot from his replies. However, I'm frequently reminded in said replies (see my previous posts) of my utter ineptitude, ignorance and disorganization on the subject - which I accept being a meagre learner in need of assistance. To this end I will, and have, refrained from passing any damning judgement or severe criticism, since I'm in no position to do so, on the nature of his assistance i.e. his seemingly abrasive style and attitude (after all I'm here to get answers, explanations and to learn from those of you with more experience), so I will continue to presume his reasons for this are positive. I suppose, after all, a person who is highly proficient on any subject can afford to be pedantic, condescending and even humiliating to others if he so chooses, since this maybe interpreted subjectively as a means to motivate or spur novices like myself, to push them to find the solution themselves and understand the subject deeper, conversely, and I suppose this is a didactic warning of sorts it can also serve to dishearten, confuse or deter an individual from wanting to learn...

Anyway this is all off the subject i.e. my OP, which has been answered, I thank you both kind sirs, you've been very helpful. My revised code below.

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

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

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
    i = i+1
    if fso.fileexists(destfolder & "\" & file.name) then
    fso.deletefile destfolder & "\" & file.name, true
    end if
fso.movefile sourcefolder & "\" & file.name, destfolder 
end if
next

if i>0 then
wscript.echo i&" files moved to 'Sorted' in path: " & vbcrlf & sourcefolder
wscript.quit()
end if

wscript.echo "No matches found"
Albert F D
  • 529
  • 2
  • 13
  • 33
1

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
Albert F D
  • 529
  • 2
  • 13
  • 33
Nathan Rice
  • 3,091
  • 1
  • 20
  • 30
  • Please reconsider the `WScript.Quit`s. – Ekkehard.Horner Mar 11 '15 at 21:55
  • What are you trying to say? Reconsider in what way? – Nathan Rice Mar 11 '15 at 21:57
  • I'm trying to say: Don't post code you don't understand or haven't tested. Your script aborts on the first file that doesn't match the searchnames or isn't in the destination folder. – Ekkehard.Horner Mar 11 '15 at 22:04
  • OP posted code that he claims works in his environment. I only added the conditions he requested. I did not add or remove the wscript.quit() conditions. Perhaps you should post this as a comment to his post since it's totally unrelated. – Nathan Rice Mar 11 '15 at 22:06
  • (1) OP: "but only the "BBC news" text file is found and moved". (2) The question is meant to present a *problem*, an answer should *solve* it. (3) adding `> 0` does not change the condition (4) Quality of code is independent of its genesis (bad code is bad code even if there are (good?) reasons/explanations for the blunders. – Ekkehard.Horner Mar 11 '15 at 22:16
  • Why would you not edit the code then instead of posting cryptic one word replies? If an improvement can me made one should do it instead of lecturing about it. We are getting a bit meta here though so I will end my replies to you here. – Nathan Rice Mar 11 '15 at 22:17
  • Hello gentlemen. @Nathan... Thanks for your reply Nathan... it appears you have indeed resolved the file replace issue, thanks for that! :D However the, **...but only the "BBC news" text file is found and moved by the 'instr' function** issue persists. I am learning VBS, so my understanding is a work in progress, hence the request for help. Perhaps I should have emphasised one issue at a time in separate questions, but since they're both related to the same function, I chose not to, apologies for that, and for my novice skill set and messy script... – Albert F D Mar 11 '15 at 23:36
  • @Ekkehard thanks for your input, by reconsider **wscript.quit()** I assume you mean the first instance in Nathan's rewrite? I'm having trouble visualising the process as is with the nested if statement in the rewrite... I will try understand and report back – Albert F D Mar 11 '15 at 23:36
1

The plan for a solution for the task "move a given set of file to another folder" is:

Define the list L of file(name)s
For Each f in L
    If f exists in SrcFolder
       copy file(f) to DstFolder (with overwrite)
       delete file(f)
    End If
Next

(sorry, no editing of Christopher's/Nathan's code will result in an implementation of that plan)

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96