Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move Files to new folder 1

Status
Not open for further replies.

JMSBR

MIS
Aug 27, 2009
14
GB
Hi

I need to create a VBScript to automatically move files from one folder location to another. Unfortunatley the source folder contains mulitple versions of the same file and I only need to move the latest. The versions are detemined by an underscore at the end of the file name. e.g

abc_1.doc
abc_2.doc
def_1.doc
def_2.doc
def_3.doc

There are mulitple files in the folder with different file names which are subject to change.

What I need help with is itterating around each file name and finding the latest version e.g. the greatest number after the underscore.

Ultimately in the example above we would want abc_2.doc and def_3.doc in the target folder.

I have been banging my head against it all day and haven't really got close.

Any help would be really appreciated.

Jon
 
Is the format of the name files as shown above, is the underscore the delimiter between the file name and revision? Are there ever multiple underscores in a file name?
 
Hi JMSBR,

What have you tried so far? Please post your script so far and we'll take a look and help.


Thanks,
FOXUP!
 
Hi jges & foxup, thanks for your responses.

jges - yes the underscore is the delimiter between file name and revision and there are never more than one underscore.

foxup - below is the code so far, this all works fine if there are only files with the same name in the folder.

e.g. abc_1.doc
abc_2.doc
abc_3.doc

In reality there are hundreds of different file names.

What I need help with is running the code below for each distinct file name. We can get this by using Left(oFile, Instr(oFile,"_")) which will give us the name upto the underscore.

Any help much appreciated.


Dim fso
Dim oFolder
Dim oFile
Dim oSubFolder
Dim i
Dim result
Dim fileName
Dim Ext
dim filesys
Dim latestdoc

Path1 = "\\file\IT\IT Projects\Office 2010\Test\"


Set fso = createobject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(Path1)
i = 0


'Remove complied documents
For Each oFile In oFolder.files

If Left(oFile.Name,10) <> "[complied]" Then

'Finds latest version
fileName = Left(oFile, Instr(oFile,"_"))
Ext = Right(oFile, Len(oFile) - Instr(oFile,".")+1)

If Instr(oFile,"_") Then
result = Mid(oFile, Instr(oFile,"_")+1, (Instr(oFile,".") - Instr(oFile,"_")-1))

If result > i Then
i = result
End If
End If
End If

Next



latestdoc = fileName & i & ext

set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists(latestdoc) Then
filesys.CopyFile latestdoc, "C:\Property\"
End If


 
take a look at NAME AS

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Oh sorry that's not in VBS is it.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Here's some sample code that should get you started:
Code:
[COLOR=green]'find latest version of file, <fileName>_<rev.minor>.<file_extension>[/color]

[COLOR=blue]Dim[/color] strPath  
[COLOR=blue]Dim[/color] FSO  
[COLOR=blue]Dim[/color] FLD  
[COLOR=blue]Dim[/color] file  
[COLOR=blue]Dim[/color] strNewName  
[COLOR=blue]Dim[/color] fileDic  
[COLOR=blue]Set[/color] fileDic [COLOR=blue]=[/color] CreateObject("Scripting.Dictionary")  
[COLOR=blue]Dim[/color] [COLOR=blue]key[/color]  
[COLOR=blue]dim[/color] fileParts  

 [COLOR=green]'Define file path.[/color]
    strPath [COLOR=blue]=[/color] "C:\Temp\"  

 [COLOR=green]'Create the instance of the FSO.[/color]
    [COLOR=blue]Set[/color] FSO [COLOR=blue]=[/color] CreateObject("Scripting.FileSystemObject")  
 [COLOR=green]'Set the folder you want to search.[/color]
    [COLOR=blue]Set[/color] FLD [COLOR=blue]=[/color] FSO.GetFolder(strPath)  

 [COLOR=green]'Loop through each file in the folder.[/color]
    [COLOR=blue]For[/color] [COLOR=blue]Each[/color] file [COLOR=blue]in[/color] FLD.Files  
		fileParts [COLOR=blue]=[/color] Split(FSO.GetBaseName(file), "_")  
		if fileDic.Exists(fileParts(0)) [COLOR=blue]then[/color]  
			  [COLOR=green]'exists, check revision number[/color]
			rev [COLOR=blue]=[/color] fileDic.Item(fileParts(0))  
			if CSng(fileParts(1)) > CSng(rev) [COLOR=blue]then[/color]  
				  [COLOR=green]'we found a higher revision[/color]
				fileDic.Item(fileParts(0)) [COLOR=blue]=[/color] fileParts(1)  
			end [COLOR=blue]if[/color]  
		else  
			  [COLOR=green]'does not exist in the list, add it[/color]
			fileDic.Add fileParts(0), fileParts(1)  
		end [COLOR=blue]if[/color]  
    [COLOR=blue]Next[/color]  
	  
	  [COLOR=green]'list highest revisions, sample output[/color]
	msg [COLOR=blue]=[/color] ""  
	if fileDic.Count > 10 [COLOR=blue]then[/color]  
		a [COLOR=blue]=[/color] fileDic.keys  
		for i [COLOR=blue]=[/color] 0 [COLOR=blue]to[/color] 9  
			msg [COLOR=blue]=[/color] msg [COLOR=blue]&[/color] a(i) [COLOR=blue]&[/color] "_" [COLOR=blue]&[/color] fileDic.Item(a(i)) [COLOR=blue]&[/color] vbcrlf  
		next  
	else  
		For [COLOR=blue]Each[/color] [COLOR=blue]key[/color] [COLOR=blue]in[/color] fileDic  
			msg [COLOR=blue]=[/color] msg [COLOR=blue]&[/color] [COLOR=blue]key[/color] [COLOR=blue]&[/color] "_" [COLOR=blue]&[/color] fileDic.Item(key) [COLOR=blue]&[/color] vbcrlf  
		Next  
	end [COLOR=blue]if[/color]  
	msgbox(msg)
 
Thanks jges, that was really helpful, we tweaked it a bit but the fundamentals are all yours.

Final code is:

'find latest version of file, <fileName>_<rev.minor>.<file_extension>

Dim strPath
Dim FSO
Dim FLD
Dim file
Dim strNewName
Dim fileDic
Set fileDic = CreateObject("Scripting.Dictionary")
Dim key
dim fileParts
dim ext
Dim shortName
Dim compare
Dim filesys
Dim moveFile

'Define file path.
strPath = "\\file\ILEData\Development\vdm\Merge Templates\CASE Conveyancing"

'Create the instance of the FSO.
Set FSO = CreateObject("Scripting.FileSystemObject")
'Set the folder you want to search.
Set FLD = FSO.GetFolder(strPath)

'Loop through each file in the folder.

For Each file in FLD.Files
If Left(file.Name, 10) <> "[compiled]" Then
fileParts = Split(FSO.GetBaseName(file), "_")
ext = (FSO.GetExtensionName(file))

if fileDic.Exists(fileParts(0)) then
'exists, check revision number
rev = fileDic.Item(fileParts(0))
if fileParts(1) > rev then
'we found a higher revision
fileDic.Item(fileParts(0)) = fileParts(1)
end if

else
'does not exist in the list, add it
fileDic.Add fileParts(0), fileParts(1)
end if
end if
Next

'list highest revisions, sample output
msg = ""
if fileDic.Count > 10 then
a = fileDic.keys
for i = 0 to 9
shortName = a(i) & "_" & fileDic.Item(a(i)) & "."
For Each file in FLD.Files

compare = Left(FSO.GetFileName(file), Instr(FSO.GetFileName(file),"."))
If compare = shortName Then
set filesys=CreateObject("Scripting.FileSystemObject")
moveFile = file
filesys.CopyFile moveFile, "C:\Property\"

End If
Next
next
else
For Each key in fileDic


shortName = key & "_" & fileDic.Item(key) & "."
For Each file in FLD.Files

compare = Left(FSO.GetFileName(file), Instr(FSO.GetFileName(file),"."))
If compare = shortName Then
set filesys=CreateObject("Scripting.FileSystemObject")
moveFile = file
filesys.CopyFile moveFile, "C:\Property\"

End If
Next
Next
end if
 
Glad to help.

The last bit of my code checked to see if there were more than 10 entries in the dictionary object. If there are more than 10, it outputs only the first 10 strings to the messagebox. This was simply to limit the example output if you tested it on a folder with hundreds of different files.

As a result, the last portion of your code can be simplified from:
Code:
 [COLOR=green]'list highest revisions, sample output[/color]
    msg [COLOR=blue]=[/color] ""    
    [COLOR=blue]if[/color] fileDic.Count > 10 [COLOR=blue]then[/color]    
        a [COLOR=blue]=[/color] fileDic.keys    
        [COLOR=blue]for[/color] i [COLOR=blue]=[/color] 0 [COLOR=blue]to[/color] 9    
        shortName [COLOR=blue]=[/color] a(i) [COLOR=blue]&[/color] "_" [COLOR=blue]&[/color] fileDic.Item(a(i)) [COLOR=blue]&[/color] "."  
                        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] file [COLOR=blue]in[/color] FLD.Files  
                  
                        [COLOR=blue]compare[/color] [COLOR=blue]=[/color] Left(FSO.GetFileName(file), Instr(FSO.GetFileName(file),"."))  
                        [COLOR=blue]If[/color] [COLOR=blue]compare[/color] [COLOR=blue]=[/color] shortName [COLOR=blue]Then[/color]  
                        [COLOR=blue]set[/color] filesys=CreateObject("Scripting.FileSystemObject")  
                                    moveFile [COLOR=blue]=[/color] file  
                                    filesys.CopyFile moveFile, "C:\Property\"  
                                        
                        End [COLOR=blue]If[/color]  
                        [COLOR=blue]Next[/color]  
        [COLOR=blue]next[/color]    
    [COLOR=blue]else[/color]    
        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] [COLOR=blue]key[/color] [COLOR=blue]in[/color] fileDic    
               
                           
                        shortName [COLOR=blue]=[/color] [COLOR=blue]key[/color] [COLOR=blue]&[/color] "_" [COLOR=blue]&[/color] fileDic.Item(key) [COLOR=blue]&[/color] "."  
                        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] file [COLOR=blue]in[/color] FLD.Files  
                  
                        [COLOR=blue]compare[/color] [COLOR=blue]=[/color] Left(FSO.GetFileName(file), Instr(FSO.GetFileName(file),"."))  
                        [COLOR=blue]If[/color] [COLOR=blue]compare[/color] [COLOR=blue]=[/color] shortName [COLOR=blue]Then[/color]  
                        [COLOR=blue]set[/color] filesys=CreateObject("Scripting.FileSystemObject")  
                                    moveFile [COLOR=blue]=[/color] file  
                                    filesys.CopyFile moveFile, "C:\Property\"  
                                        
                        End [COLOR=blue]If[/color]  
                        [COLOR=blue]Next[/color]  
        [COLOR=blue]Next[/color]    
    end [COLOR=blue]if[/color]
to:
Code:
        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] [COLOR=blue]key[/color] [COLOR=blue]in[/color] fileDic    
               
                           
                        shortName [COLOR=blue]=[/color] [COLOR=blue]key[/color] [COLOR=blue]&[/color] "_" [COLOR=blue]&[/color] fileDic.Item(key) [COLOR=blue]&[/color] "."  
                        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] file [COLOR=blue]in[/color] FLD.Files  
                  
                        [COLOR=blue]compare[/color] [COLOR=blue]=[/color] Left(FSO.GetFileName(file), Instr(FSO.GetFileName(file),"."))  
                        [COLOR=blue]If[/color] [COLOR=blue]compare[/color] [COLOR=blue]=[/color] shortName [COLOR=blue]Then[/color]  
                        [COLOR=blue]set[/color] filesys=CreateObject("Scripting.FileSystemObject")  
                                    moveFile [COLOR=blue]=[/color] file  
                                    filesys.CopyFile moveFile, "C:\Property\"  
                                        
                        End [COLOR=blue]If[/color]  
                        [COLOR=blue]Next[/color]  
        [COLOR=blue]Next[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top