VBScript how to copy files with specific extensions

April 20, 2016 at 17:11:34
Specs: Windows 7
I have just started to learn vbscript by watching a few youtubes and thought I would start working on something. While learning some and kind of understanding the language I get stuck on things that weren't described so I tried to just find code and copied to add a piece. specifically the latest on creating an extension library and a copy function that doesn't do anything. I am not sure why.

So what I ask is a nudge in the right direction. If you read my script below the part that doesn't work is that it (4) wont copy anything. f.copy and the f are new to me but figured it would work because everything before it worked as I hoped and I would continue to be amazed at how well I have been up to about two hours ago.


'======================================================
'This script once Ive fixed and completed will copy all documents of a
'specified type, ie. word doc, to a folder for review. Logging
'document names and locations to a log file.
'
'**NOT COMPLETE**
'1) scan a system for all 'targetted' documents
'2a) name a destination folder logically i.e: using drive being copied & date
'3a) Log all executed copy actions with filenames and locations for later use in reorg scripts
'
'***COMPLETE***
'(2) Check for or Create the destination folder/create if missing
'(3) Check for or Create a log file to list the documents and locations
'
'****WORKING*** and my current roadblock/issue most of this is just copied and tweaked
'so am not sure I am learning the best way just playing around with vbscript to try and learn.
'4) copy all 'word' documents to the destination folder
'
' Eric C.
'======================================================

Option Explicit
On Error Resume Next

'--------------------------------------
'Declare variables to be used createstuff, folder, wordfiles,library, logging
'--------------------------------------

Dim objFSO, objTargetFolder, objFile, objDestinationFolder, LogFile
objDestinationFolder = "F:\ScriptTesting\CopyTestDocs1"
Logfile = "F:\ScriptTesting\CopyTestDocs1\LogFile.txt"
Const Forwriting = 2
Const ForAppending = 8


'--------------------------------------
'(Set) Create the file system object FSO to create things
'Create a library of extensions for type based copying
'>> copy this to add more to library [extensions.Add "", True]<<
'--------------------------------------

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTargetFolder = objFSO.GetFolder("C:\Users\Eric\Desktop")

'--------------------------------------
'(2) Check for or Create the destination folder/create if missing
'--------------------------------------

If objFSO.FolderExists(objDestinationFolder) Then
WScript.Echo "The destination folder " & objDestinationFolder & " already exists"
Else
WScript.Echo "The destination folder " & objDestinationFolder & " will be created "
Set objDestinationFolder = objFSO.CreateFolder(objDestinationFolder)
End If

'--------------------------------------
'(3) create a log file for listing the documents and locations
' a) create a logfile
' b) Append with CreatedDate or runTest Messages
' c) Close file
'--------------------------------------

If objFSO.FileExists(LogFile) Then
WScript.Echo "The File " & LogFile & " Exists"
Set objFile = objFSO.OpenTextFile(LogFile, ForAppending)
objFile.Write "Testing script/ and dude the logfile already exists " & Now & VbCrLf
Else
WScript.Echo "The File " & LogFile & " Doesn't Exist, and dude I will Create it"
Set objFile = objFSO.CreateTextFile(LogFile, Forwriting)
objFile.Write "Created LogFile " & Now & VbCrLf
' is there a character for a tab?
End If
objFile.Close

'--------------------------------------
'(4) copy all word docs in target folder to destination folder
' extensions.Library has doc and docx
'
' btw this I found on computing.net but can't seem to make it work
'--------------------------------------

Set extensions = CreateObject("Scripting.Dictionary")
' ok so this is a library to check contains: doc and docx
extensions.CompareMode = vbTextCompare
extensions.Add "doc", True
extensions.Add "docx", True
For Each f In objTargetFolder
If extensions.Exists(objFSO.GetExtensionName(f.Name)) Then
f.Copy objDestinationFolder & "\"
End If
Next

message edited by fcleffer


See More: VBScript how to copy files with specific extensions

Report •


#1
April 20, 2016 at 21:34:50
For Each f In objTargetFolder
This line should error out on you. How are you running your script such that you can't see the errors?

As for your hint: https://msdn.microsoft.com/en-us/li...

How To Ask Questions The Smart Way


Report •

#2
April 20, 2016 at 23:29:21
Thanks Razor for the links. Especially the 'How to ask questions ...' I will visit & heed!

Running in Command. I got the error line and char message. So I saw the error just didn't understand enough yet to know what was wrong with the line. Appreciate the msdn link as hint and location to learn the language.

message edited by fcleffer


Report •

#3
April 21, 2016 at 08:49:03
I think the line should be

For Each f In objTargetFolder.files


Report •

Related Solutions

#4
April 21, 2016 at 11:07:25
AlwaysWilling.. yes. thnx
only copied from target. want to copy from all folders in target hierarchy-is that what would be called 'recursive'?

Report •

#5
April 21, 2016 at 12:23:42
Well, you'd probably make a recursive function to do the copying, if that's what you mean. There's added difficulty if you wanted it to copy the directory structure as well as the documents. Ironically you've picked one of the few operations that's easier to do in batch than VBScript.

How To Ask Questions The Smart Way


Report •


Ask Question