|
Answer» I have two folders on the root of my C: drive, one is named "In" and the other "Out". I have tried to create a batch command that I hope would allow me to copy the 'In" folder and it's contents to the "Out" folder. I have used this command, however all it does is a continuous scroll, what am I doing wrong ?
xcopy C:\In Out
also is there a way to poll the "In" folder for files automatically( it will be receiving files throughout the day) without having to run the batch file each time.
Thanks again for all your help
Try Xcopy c:\in\*.* c:\outThese are the fun questions. This little snippet will monitor the arrival of files into the specified folder:
Code: [Select]strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 3 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""c:\\\\in""'") ' <== Change folder if necessary
Do Set objLatestEvent = colMonitoredEvents.NextEvent Wscript.Echo objLatestEvent.TargetInstance.PartComponent Loop
Save the script with a vbs extension and run from the command line as: cscript scriptname.vbs. As written, a message will appear whenever a new file arrives in the folder. This can easily be changed to process the new file after it arrives, either in-line or by spinning off an application or another script. If you replace the WScript.Echo statement with actual work, you can use wscript in place of cscript, and have the script run transparently in the background.
Good luck. 8-)
Note: You can test the script by running it in one command window and creating dummy files (echo. > dummy.txt) from a second command window.
Also Note: The script runs in a loop. Use the task manager to end execution. -your code worked great , thanks a bunch
As this is beyond my scope , how would I go about transferring the files in the "IN" folder automatically to an "out" folder without asking for my verification. Also how do i stop the script from running once started
This is the code so far:
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 3 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""c:\\\\in""'") ' <== Change folder if necessary
Do Set objLatestEvent = colMonitoredEvents.NextEvent Wscript.Echo objLatestEvent.TargetInstance.PartCompo nent Loop This should set you up:
Code: [Select]strComputer = "." set fso = CreateObject("Scripting.FileSystemObject") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 3 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""c:\\\\in""'")
Do Set objLatestEvent = colMonitoredEvents.NextEvent ' Wscript.Echo objLatestEvent.TargetInstance.PartComponent arrFile = SPLIT(objLatestEvent.TargetInstance.PartComponent, "\\") fname = arrFile(UBound(arrFile)) If RIGHT(fname, 1) = """" Then fname = Left(fname, Len(fname) - 1) End If fso.CopyFile "c:\in\" & fname, "c:\out\" & fname, True Loop
Note: I removed the message that a file had arrived. If you want it back, remove the apostrophe from the WScript.Echo line.
To kill the script, ctl-c in the window where the script is running will work. If you're using cscript, closing the windows WORKS. If you're using wscript, use the task manger and look for wscript.
Good luck. 8-)Fantastic, one last question , how could I clear the files from the "in" folder after it has been sent
Thanks for the great help
strComputer = "." set fso = CreateObject("Scripting.FileSystemObject") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 3 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""c:\\\\in""'")
Do Set objLatestEvent = colMonitoredEvents.NextEvent ' Wscript.Echo objLatestEvent.TargetInstance.PartCompo nent arrFile = Split(objLatestEvent.TargetInstance.PartCompo nent, "\\") fname = arrFile(UBound(arrFile)) If Right(fname, 1) = """" Then fname = Left(fname, Len(fname) - 1) End If fso.CopyFile "c:\in\" & fname, "c:\out\" & fname, True Loop Not for nothing, but why not receive the file in OUT directory in the first place? :-?
Code: [Select]strComputer = "." set fso = CreateObject("Scripting.FileSystemObject") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 3 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""c:\\\\in""'")
Do Set objLatestEvent = colMonitoredEvents.NextEvent ' Wscript.Echo objLatestEvent.TargetInstance.PartComponent arrFile = Split(objLatestEvent.TargetInstance.PartComponent, "\\") fname = arrFile(UBound(arrFile)) If Right(fname, 1) = """" Then fname = Left(fname, Len(fname) - 1) End If fso.CopyFile "c:\in\" & fname, "c:\out\" & fname, True fso.DeleteFile "c:\in\" & fname, True Loop
Happy Computing. 8-)Thanks again for your help, it works great. I'm doing it this way as the "in" folder is on a remote computer that is mapped to the computer having the "out" folder. ACCESS and permissions to the remote computer is limited . Hope this makes some sense. One small problem occurs, every so often I get this error
Script: C:\Foldermonitor.vbs Line: 21 Char: 5 Error: Permission denied Code: 800A0046 Source: Microsoft VBScript runtime error
Most of the code is boilerplate and came from my snippet closet. I added the last few lines based on your specific request. Might have used too low a number for the WITHIN clause.
With source files on a remote PC, try using the UNC path for the file, not the mapped drive letter.
(ie: & "'Win32_Directory.Name=""\\\\computername\\\\sharename\\\\in""'")
Permissions are always a hassle. Ensure the user running the script has permissions for both the IN and OUT folders.
This blurb from the Hey! Scripting Guys may help:
Quote A WMI script like this works by “polling;” it periodically goes out and checks to see if any new files have been added to the folder. For this sample, we’re checking every 3 seconds (that’s what the WITHIN 3 represents) to see if there are any new files in the folder. If that’s too fast or too SLOW, you can change that value to anything you want. Keep in mind two things, however. For one, if you poll too often (say, every second), you’ll have a script that is constantly running, and could theoretically put a drain on your system resources.
Conversely, if you make the value too long, you might miss new files, assuming they get added and then deleted before the polling time expires. For example, say your script checks every 5 minutes for new files. If you add 100 new files and then delete all those files 3 minutes later, the script will never know that those files were added to the folder. That’s because scripts like this work by comparing the files that currently in the folder with the files that were in the folder the last time the script checked. Play around with the polling interval and see what works best for you. Good luck 8-)SW- thanks for all your help and suggestions, i did increase the polling time and it seems stable now. I am very happy with the result and of course with your knowledgeable assistance
much appreciated !!!
I knew i shouldn't have said it was my last question, but is there a way to also transfer a folder that is placed inside the "in" folder. these folders could have different names. At present all files will be transferred to the "out" folder without any problem , folders however,do not get transferred. I guess basically what I'm trying to say is that everything that's get placed in the IN folder, I would like to get transferred to the out folder.
thanks againApparently WMI does not support any Directory monitor events.
Code: [Select]strComputer = "." set fso = CreateObject("Scripting.FileSystemObject") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 7 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""c:\\\\in""'")
Do Set objLatestEvent = colMonitoredEvents.NextEvent ' Wscript.Echo objLatestEvent.TargetInstance.PartComponent arrFile = Split(objLatestEvent.TargetInstance.PartComponent, "\\") fname = arrFile(UBound(arrFile)) If Right(fname, 1) = """" Then fname = Left(fname, Len(fname) - 1) End If fso.CopyFile "c:\in\" & fname, "c:\out\" & fname, True fso.DeleteFile "c:\in\" & fname, True Set f = fso.GetFolder("c:\in") Set sf = f.SubFolders For Each fl in sf fso.CopyFolder fl, "c:\out", True fso.DeleteFolder f1, True Next Loop
Note: The folder logic is dependent on a file arrival. Another option is to isolate the folder logic in another script and use the task scheduler to check for folders, say every hour or so.
8-)
SW-thanks for all your help, it was much appreciated !!!
|