Page 1 of 1

Script to create folders every 500 files

Posted: Tue Oct 09, 2012 11:07 am
by DataMystic Support
In FileWatcher, specify the command as

Code: Select all

Path\wscript.exe
and the parameters as

Code: Select all

Your Path\Group files by 500.vbs
Set FileWatcher to watch c:\incoming

If you edit the .vbs file, you can alter the number of files per folder (default 500), the incoming folder (default c:\incoming), and the base name of new output folders created (default c:\doc).

Code: Select all

'FileWatcher script - move files to a new folder

Option Explicit

'configuration
const incoming = "c:\incoming"
const maxfiles = 500
'this is the base filename, e.g. c:\doc01, c:\doc02, c:\doc03 ...
const folderbase = "c:\doc"

'variables
dim fso
dim files
dim folder


Function getNewFolder()

  'find an unused folderbase name
  dim number
  number = 1
  do
    'does it exist already?
    msgbox folderbase + CStr(number)
	if not fso.folderExists( folderbase + CStr(number) ) then
      msgbox "creating " + folderbase + CStr(number)
	  fso.createFolder folderbase + CStr(number)
	  getNewFolder = folderbase + CStr(number)
	  Exit Function
	end if
	
	'already exists, try again
    number = number + 1
  loop

end Function


'move up to maxfiles files to a new folder
sub moveFiles( files, newFolder )

  dim filename
  msgbox "Moving to " + newFolder
  dim count
  count = 0
  For each filename In files
    msgbox filename
    fso.MoveFile filename, newFolder + "\"
	count = count + 1
	if count >= maxfiles then 
		Exit Sub
	end if
  Next
  

end sub


Set fso = CreateObject("Scripting.FileSystemObject")


'Count files in incoming folder
Set folder = fso.GetFolder(incoming)
Set files = folder.Files
if files.count > maxfiles then
  moveFiles files, getNewFolder() 
end if



Set fso = Nothing