Windows Scripting
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsSystem AdministrationWindows Scripting

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 6th, 2008, 07:03 PM
chrisj chrisj is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 49 chrisj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 39 m 41 sec
Reputation Power: 2
VBScript - Need to distribute files evenly to other folders

I need to distribute files evenly to other folders, from a single directory. Would a VBscript work? If so, can you help me with that?

Thanks.

Reply With Quote
  #2  
Old August 7th, 2008, 01:08 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
Sure. But can you be more specific when you say you need to distribute files evenly?
__________________
Click the image if at any point you don't like my decision.

Scripting problems? Windows questions? Ask the Windows Guru!


Reply With Quote
  #3  
Old August 7th, 2008, 09:45 AM
chrisj chrisj is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 49 chrisj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 39 m 41 sec
Reputation Power: 2
Thanks

Thanks for your reply.

To be more specific, when files are uploaded into the Upload Directory, I'd like the VBscript to evenly distribute the files that it finds in the Upload Directory to the four destination folders, wait X seconds, then repeat the process again. So, if 20 files arrive into the Upload Directory while the script is "sleeping", I will end up with 5 files in each destination folder. Also, we will need to add one more measure, that is a file should not be moved while in the middle of being uploaded.

Thanks> i look forward to your reply.

Reply With Quote
  #4  
Old August 7th, 2008, 12:19 PM
Hangman Hangman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 29 Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 h 56 m 6 sec
Reputation Power: 0
Quote:
Originally Posted by chrisj
Thanks for your reply.

To be more specific, when files are uploaded into the Upload Directory, I'd like the VBscript to evenly distribute the files that it finds in the Upload Directory to the four destination folders, ...

Are these specific files with specific names which need to be distributed to one of the four specific folders, or can you just pick any file and send it to any of the four folders ??

Reply With Quote
  #5  
Old August 7th, 2008, 12:29 PM
chrisj chrisj is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 49 chrisj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 39 m 41 sec
Reputation Power: 2
Thanks

Thanks for your reply. Great question.

These are not specific files needing to go to specific folders.

I just want the script to distribute the files evenly. So that each of the four destination folders keeps getting files one after another, rather than having one destinaton folder having way more files than the other three destination folders.

Someone suggested:
"You can use the FileSystemObject to get a list of all the files in folder A.Then use a for loop to iterate through the list. Then use do: <your for loop variable> mod 4 to determine which folder to place it in and move the file. That should evenly distribute the files."

But I don't know enough about code to know if this is a viable solution. Any suggestions will be welcomed.
Thanks

Last edited by chrisj : August 7th, 2008 at 03:03 PM. Reason: more info

Reply With Quote
  #6  
Old August 7th, 2008, 04:35 PM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
There are several different solutions to his problem. I might suggest using WMI Events. You can monitor a folder for file creations (i.e. a file upload) then simply move it as soon as it appears. You could simply rotate through each of your folders as a destination.
vb Code:
Original - vb Code
  1. strFolder = "C:\myfolder"
  2. arrDestFolders = Array("C:\myfolder2", _
  3.     "C:\myfolder3", _
  4.     "C:\myfolder4", _
  5.     "C:\myfolder5") 
  6.  
  7. Const FOF_SILENT = 4
  8. Const FOF_RENAMEONCOLLISION = 8
  9. Const FOF_NOCONFIRMATION = 16
  10. Const FOF_NOERRORUI = 1024
  11. intSHOptions = FOF_SILENT + FOF_RENAMEONCOLLISION + FOF_NOCOMFIRMATION + FOF_NOERRORUI
  12. Set objShell = CreateObject("Shell.Application")
  13.  
  14. strComputer = "."
  15. Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")
  16. Set colEvents = objWMIService.ExecNotificationQuery _
  17.     ("SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE " _
  18.         & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
  19.         & "TargetInstance.GroupComponent = 'Win32_Directory.Name = " & Chr(34) & strFolder & Chr(34) & "'")
  20.  
  21. intFolder = 0
  22.  
  23. Do While True
  24.     Set objEvent = colEvents.NextEvent()
  25.     'A new file was just uploaded
  26.     strFile = objEvent.TargetInstance.PartComponent
  27.     objShell.NameSpace(arrDestFolders(intFolder)).Move  Here strFile, intSHOptions
  28.  
  29.     intFolder = intFolder + 1
  30.     If intFolder > arrDestFolders.Count - 1 Then intFolder = 0
  31. Loop

Reply With Quote
  #7  
Old August 7th, 2008, 04:55 PM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
On a side note, this script runs indefinitely. You'll have to manually stop it in Task Manager.

Reply With Quote
  #8  
Old August 7th, 2008, 05:04 PM
Hangman Hangman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 29 Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level)Hangman User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 h 56 m 6 sec
Reputation Power: 0
Quote:
Originally Posted by Nilpo
There are several different solutions to his problem. I might suggest using WMI Events. You can monitor a folder for file creations (i.e. a file upload) then simply move it as soon as it appears. You could simply rotate through each of your folders as a destination ...

Wahooo !! (Sorry, I'm thrilled.) I am thinking along the same lines as the pro !! Nilpo the pro I was wondering how one would monitor the folder, but I was trying to write some code that would rotate through the destination folders as you suggested. I'm not nearly as fast though as I had to go look some things up and do a little trouble shooting. But that's what I was think'n.

Reply With Quote
  #9  
Old August 7th, 2008, 05:40 PM
chrisj chrisj is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 49 chrisj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 39 m 41 sec
Reputation Power: 2
Thank you

Thank you Nilpo for this solution. As I'm not highly skilled in Vbs, I have a few questions, please.

1. Would I save this code as a .vbs file?
2. How do I execute this code on a windows server?
3. How often does it scan the "C:\myfolder" for new files?
4. Is there anything in this code to make sure it doesn't move a file while the file is still in the middle of uploading to "C:\myfolder"?

Thank you again. I look forward to your reply.

Last edited by chrisj : August 7th, 2008 at 06:09 PM.

Reply With Quote
  #10  
Old August 8th, 2008, 06:50 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 34 sec
Reputation Power: 667
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
Quote:
Originally Posted by chrisj
Thank you Nilpo for this solution. As I'm not highly skilled in Vbs, I have a few questions, please.

1. Would I save this code as a .vbs file?
2. How do I execute this code on a windows server?
3. How often does it scan the "C:\myfolder" for new files?
4. Is there anything in this code to make sure it doesn't move a file while the file is still in the middle of uploading to "C:\myfolder"?

Thank you again. I look forward to your reply.
Hey, Chris. I'll go through each of your questions in order.

1. Yes, this should be saved with a .vbs extension.

2. In most cases, this can be executed by simply double-clicking the file. Since it doesn't require user input and doesn't output anything it does not matter which engine (wscript.exe or cscript.exe) it runs in.

3. Technically, it isn't scanning the folder. It monitors the system for new file creations. I'm just limiting the results to those files in a specific folder. Each time a file is created, WMI issues an event. We are monitoring and reacting to those events. The "WITHIN 1" part of the query indicates how often to check for new events. In this case, every 1 second.

4. Since the __InstanceCreationEvent is not fired until a file is completely written, the script should never try to move a file before the upload completes.

Reply With Quote
  #11