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

wmi script 1

Status
Not open for further replies.

texasit

MIS
Jun 26, 2008
21
US
I need a script to search a folder and subfolders for certain file extensions then move the folder and all it contents to another folder.I have a script that will find a certain extension and move it but I want the script to search the whole folder and any subfolders for that file extension and once it finds it then it will move the folder that contains that extension plus the extension.Here is what I have so far.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\data'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile in colFiles
If objFile.Extension = "doc" Then
strCopy = "D:\Officedata\" & objFile.FileName _
& "." & objFile.Extension
objFile.Copy(strCopy)
objFile.Delete
End If
If objFile.Extension = "xls" Then
strCopy = "D:\Officedata\" & objFile.FileName _
& "." & objFile.Extension
objFile.Copy(strCopy)
objFile.Delete
End If
Next
 
Break up what you want to do into two steps, find files and move them. It looks like your method of collecting files only collects files and not folders. For finding the files, make a recursive function so when a folder is encountered, the function calls itself. This is how I would do it.

Code:
'***********************************
'  FUNCTIONS AND SUBS
'***********************************

function moveFiles(arrFiles, strDestination)
	for i = 0 to ubound(arrFiles)
		strPath = arrFiles(i)
		if (strPath <> "") then
			if (objFSO.FileExists(strPath)) then
				strFileName = objFSO.GetFileName(strPath)
				objFSO.MoveFile strPath, strDestination & "\" & strFileName
			end if
		end if
	next
end function

function searchDir(strDir, strExt)
	set objFolder = objFSO.GetFolder(strDir)
	'[green]'Loop through all folder objects found in the parent folder and call searchDir for each object[/green]
	for each objSubFolder in objFolder.SubFolders
		strResults = strResults & searchDir(objSubFolder.Path, strExt)
	next

	'[green]'Now loop through all file objects found in the parent folder and return them if their extention equal strExt[/green]
	for each objFile in objFolder.Files
		if (right(lcase(objFile.Name), len(strExt)) = lcase(strExt)) then strResults = strResults & objFile.Path & vbNewLine
	next
	searchDir = strResults
end function

'***********************************
'  BEGIN
'***********************************

set objFSO = CreateObject("Scripting.FileSystemObject")

strFolder = "C:\temp"
strDestination = "C:\temp\pages"

'[green]'Get a string of files[/green]
strDocFiles = searchDir(strFolder, ".txt")
strXlsFiles = searchDir(strFolder, ".xls")

'[green]'Convert string of files into array of files[/green]
arrDocFiles = split(strDocFiles, vbNewLine)
arrXlsFiles = split(strXlsFiles, vbNewLine)

'[green]'move all files in the array[/green]
moveFiles arrDocFiles, strDestination
moveFiles arrXlsFiles, strDestination

-Geates

PS. The searchDir function returns a string of file paths separated by vbNewLine. I would like the figure out a way to have the function return an array, but it is a recursive function and returning arrays is not feasible.

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
thank you Geates let me try what you suggested and I will report back.
 
Thank you for your help Geates I tried your script and it moved my files but it did not move the folder too. I'm looking for a script to search a root folder say c:\data for certain file extensions anywhere in that folder or subfolders or subfolders of that subfolder and move that file extension plus any other files and the folder as well. For example if I had a folder called c:\data\HR and it had a .doc file , a pdf file and a few new employee training videos I would want the script to that whole folder to the destination folder but if I had a file in c:\data that was not in a folder than just move the file.I'm very new with scripting so please bare with me
 
Ah, yes. A few tweaks.

[red]1. Just prior to moving, modify the destination path to include the child directories of the source directory. This will provide the multi-level directories you desire[/red]
[blue]2. Create the destination directory if it doesn't exist.[/blue]

Code:
'***********************************
'  FUNCTIONS AND SUBS
'***********************************

[blue]
function createDirectory (strDir)
	if (inStr(replace(strDir, objFSO.GetParentFolderName(strDir), ""), ".")) then strDir = objFSO.GetParentFolderName(strDir)
	if (right(strDir, 1) = "\") then strDir = left(strDir, len(strDir) - 1)
	strParentDir = objFSO.GetParentFolderName(strDir)
	if NOT (objFSO.FolderExists(strParentDir)) then createDirectory (strParentDir)
	if NOT (objFSO.FolderExists(strDir)) then objFSO.CreateFolder (strDir)
end function
[/blue]

function moveFiles(arrFiles, strDestination, strSource)
    for i = 0 to ubound(arrFiles)
        strSourcePath = arrFiles(i)
        if (strSourcePath <> "") then
            if (objFSO.FileExists(strSourcePath)) then
[red]                strFileName = replace(strSourcePath, objFSO.GetParentFolderName(strSourcePath), "")
                strDestPath = replace(strSourcePath, strSource, strDestination)
                [blue]createDirectory strDestPath[/blue]
                objFSO.MoveFile strSourcePath, strDestPath & "\" & strFileName[/red]
            end if
        end if
    next
end function

function searchDir(strDir, strExt)
    set objFolder = objFSO.GetFolder(strDir)
    ''Loop through all folder objects found in the parent folder and call searchDir for each object
    for each objSubFolder in objFolder.SubFolders
        strResults = strResults & searchDir(objSubFolder.Path, strExt)
    next

    ''Now loop through all file objects found in the parent folder and return them if their extention equal strExt
    for each objFile in objFolder.Files
        if (right(lcase(objFile.Name), len(strExt)) = lcase(strExt)) then strResults = strResults & objFile.Path & vbNewLine
    next
    searchDir = strResults
end function

'***********************************
'  BEGIN
'***********************************

set objFSO = CreateObject("Scripting.FileSystemObject")

strSource = "C:\temp\source"
strDestination = "C:\temp\dest"

''Get a string of files
strDocFiles = searchDir(strSource, ".doc")
strXlsFiles = searchDir(strSource, ".xls")

''Convert string of files into array of files
arrDocFiles = split(strDocFiles, vbNewLine)
arrXlsFiles = split(strXlsFiles, vbNewLine)

''move all files in the array
moveFiles arrDocFiles, strDestination, strSource
moveFiles arrXlsFiles, strDestination, strSource

In order to accomplish 1 and 2, strSource needs to be passed to moveFiles(). To move files to a single-level directory, pass "" as strSource. Notice that I replaced strFolder with strSource for consistency.

-Geates



"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
your edited script seems to be doing what I needed thanks buddy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top