Script to create folders every 500 files

Discuss our other products here. Get help with installation and running, notify us of bugs, request new features and enhancements.

Moderators: DataMystic Support, Moderators, DataMystic Support, Moderators, DataMystic Support, Moderators

Post Reply
User avatar
DataMystic Support
Site Admin
Posts: 2227
Joined: Mon Jun 30, 2003 12:32 pm
Location: Melbourne, Australia
Contact:

Script to create folders every 500 files

Post 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
Post Reply