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

Copying files

Status
Not open for further replies.

ferryjenny

Technical User
Nov 22, 2000
24
0
0
GB
Hi looking for some scripting help to copy files.

We have a directory that files are feed into, once in the directory they are processed and then deleted. I am looking for some way to copy these files when they are initially placed in the folder before they are processed.

So basically what I want is a script that when the file is put in the folder it makes a copy of it to another folder. I think I can do the copy part but I need help with the code that would trigger the copy.

Does anybody know code that would constantly poll the folder and when a file is added copy it to another folder.

 
Don't know about doing this in VBS but you could certainly do it in VB .Net or VB6, simply create a program that contains a timer, on every click of the timer it counts the number of files in the folder. If this number has gone up since the last count then process your script.

You could sit this program in the startup folder on you PC or run it as a service, doesn't need to have a visible form for it to be running.

I haven't written the code but if you can find someone reasonably competent at VB6 or VB .Net it should be fairly straight forward.
 
Hello ferryjenny!

I offer you two solutions.

1. Figgure out a way to call a copy script as part of the normal processes that you spoke of. If you defined what those processes are, I/we could help you achieve this.
This is your best solution because if you write a script to pick up the files before a process kicks off, you're leaving it to timing and chance that your files will be stored off somewhere. That's a risk you'll have to work out.

Never the less, here is the other solution...
2. Create the following script:
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""C:\\\\testing\\\\test""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
The value '5' is the amount of seconds between intervals of checking for a file. The directory name does need to have 4 backslashes instead of just 1. After creating this script, simply run the program to kick it off, and it will run until you stop it via task manager. When running, if you copy a file into C:\testing\test\ called "test.txt", then you will see the following message:
Code:
\\[computername]\root\cimv2:CIM_DataFile.Name="C:\\testing\\test\\test.txt"
You will have to do some string parsing to get to the file name, but that's not hard to do.

As I said earlier, the 1st solution is better for a number of reasons. Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top