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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie to VBScript

Status
Not open for further replies.

sqldba20

MIS
Feb 24, 2009
2
US
I am a newbie to VBScript and need help with this code. Here is a script which is for copying files. My requirement is to copy file names starting with "ml_*" (asterick to act as wildcard) in folder (X:\ABC) and all other files in folder (X:\PQR). For some reason it is not working. Any help is appreciated.

strComputer = "."

Set objNetwork = CreateObject("Wscript.Network")
Set fs = CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='U:\'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFiles
if objFile.FileName = "ml_*" Then

destinationPROD = "X:\ABC\" & objFile.FileName & "." & objFile.Extension
objFile.Copy(destinationPROD)

objFile.delete

else

destinationPROD = "X:\PQR\" & objFile.FileName & "." & objFile.Extension
objFile.Copy(destinationPROD)


objFile.delete

End If

Next
 
1) Why recreate the wheel? Use robocopy, xcopy, etc... You're not doing anything here that requires advanced parsing of file or folder names. Existing tools should be sufficient.

2) Wildcards are not permitted in comparison you are attempting. You must use a string parsing method. Here's the fix for the code:
Code:
[red][s]if objFile.FileName = "ml_*" then[/s][/red]
If Left(objFile.FileName, 3) = "ml_" then

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Thanks, Scott !

I made the changes you proposed and it is working fine but for some reason the file doesn't get overwritten. I want the files to get overwritten.


strComputer = "."

Set objNetwork = CreateObject("Wscript.Network")
Set fs = CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='U:\'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFiles
If Left(objFile.FileName, 3) = "ml_" then

destinationPROD = "X:\ABC\" & objFile.FileName & "." & objFile.Extension
objFile.Copy(destinationPROD)

objFile.delete

else

destinationPROD = "X:\PQR\" & objFile.FileName & "." & objFile.Extension
objFile.Copy(destinationPROD)


objFile.delete

End If

Next
 
If you take a look here, [URL unfurl="true"]http://msdn.microsoft.com/en-us/library/aa389347(VS.85).aspx[/url], you can see that this method does not have an option to overwrite. You will have to use another logic to perform this.

The File System Object is able to perform overwrites, so maybe use that instead? My suggested alternative below does not use WMI at all and uses the file system object exclusively.

Code:
[s]strComputer = "."[/s]

Set objNetwork = CreateObject("Wscript.Network")
Set fs = CreateObject("Scripting.FileSystemObject")
Set colFiles = fs.GetFolder("H:\").Files

[s]Set objWMIService	= GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles		= objWMIService.ExecQuery _
						("ASSOCIATORS OF {Win32_Directory.Name='H:\'} Where " _ 
						& "ResultClass = CIM_DataFile")[/s]

For Each objFile In colFiles
[s]	If Left(objFile.FileName, 3) = "ml_" Then[/s]
	If Left(objFile.Name, 3) = "ml_" Then
[s]		destinationPROD = "X:\ABC\" & objFile.FileName & "." & objFile.Extension[/s]
		destinationPROD = "X:\ABC\" & objFile.Name
[s]		objFile.Copy(destinationPROD)[/s]
		objFile.Copy destinationPROD, True
[s]		objFile.delete[/s]
		objFile.Delete True

	Else
[s]		destinationPROD = "X:\PQR\" & objFile.FileName & "." & objFile.Extension[/s]
		destinationPROD = "X:\PQR\" & objFile.Name
[s]		objFile.Copy(destinationPROD)[/s]
		objFile.Copy destinationPROD, True
[s]		objFile.delete[/s]
		objFile.Delete True
	End If
Next


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Why not simply this ?
Code:
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile "U:\ml_*", "X:\ABC\", True
fs.DeleteFile "U:\ml_*", True
fs.CopyFile "U:\*", "X:\PQR\", True
fs.DeleteFile "U:\*", True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top