# Solved: Powershell - Copying Files When They Are Modified



## Scottcom4 (Apr 30, 2012)

Hi Guys,

I am a first time user of powershell and thought that someone on this forum might be able to help me out. Please be gentle.

Basically what I am looking to do is copy a specific file when it exists in our drop zone or has been modified. I hear you mocking me already, but please hold off until you read the rest.

Unfortunately the process I am developing is an interim solution where we are migrating from one server to another, yet still need the data stored permanently on the old one. In the "new world" we will have a landing zone which data gets FTP'd to, we will pick this data up, rename it and stick it in a new location. This process is set in stone, so the idea of my code is to copy the data from our existing FTP location to our new landing zone and let the pre-established scheduled job do the rename and move. Once the new server is fully established and the files will be FTP'd to this location anyway, so all I have to do is turn the jobs I am developing off and the rest will take care of itself.

Firstly I thought that I could use the mirror function in Robocopy. From my understanding however (please correct me if I am wrong), Robocopy compares the file on the old server to the file on the new server and if there has been a change in either the modified date or the size of the file then the job copies the file. Obviously the flaw here is that by the time the job runs again (scheduled every 1hr) then the file will have been moved to it's new location (using the pre-established and locked down process) so the file will copied again. Some of these files are huge, so this isn't practical.

I then thought that I might be able to do this using powershell. So I wrote the following:


```
$DateToCompare = (Get-date).AddDays(-0).tostring("ddMMyy") #Use when comparing todays file;
GET-CHILDITEM -PATH [URL="file://******/*****/DEPARTMENTS/"]\\******\*****\DEPARTMENTS\[/URL] | 
WHERE-OBJECT {-NOT($_.PSISCONTAINER) -AND $_.lastwritetime.tostring("ddMMyy") -eq $DateToCompare} | 
Foreach-Object{Copy-Item -path $_.fullname -destination [URL="file://\\******\******\DEPARTMENTS\LEVEL1"]\\******\******\DEPARTMENTS\LEVEL1[/URL]}
```
As I said I am a first time user of PS, so there is a distinct possibility that the above could be quite inefficient (please feel free to offer suggestions on this). The problem I have with this job is that we are using JAMS Scheduler and I can't locate an option to resubmit until successful. So then I thought what if I could add some code to this existing PS script that somehow stored the files copied that day in a text file that I could read in an exclude from any further runs, based on the name the file size and the date modified (because if a new file does come in I still want to copy it). This is where I am completely stuck.

Can someone help with this or perhaps make another suggestion?

Thank you for your consideration and help.

Regards,
Scott


----------



## Squashman (Apr 4, 2003)

Not sure why you are hooked on using PowerShell. May want to have this post moved to Software development though. You can report you post and ask to have it moved.

We had a similar question just like what you are asking not to long ago.
You may want to look at using XCOPY with the /M switch.

In the meantime I will see if I can dig up that old thread.


----------



## Scottcom4 (Apr 30, 2012)

Squashman said:


> Not sure why you are hooked on using PowerShell. May want to have this post moved to Software development though. You can report you post and ask to have it moved.
> 
> We had a similar question just like what you are asking not to long ago.
> You may want to look at using XCOPY with the /M switch.
> ...


Hi Squashman,

I was originally thinking of using the /M modifier, but the files we recieve already have the achive attribute turned off.

Do you have any further throughts?

Thank you for your help.

Regards,
Scott


----------



## Squashman (Apr 4, 2003)

So this is basically going to be one of those one off things you need to do for a short time until you migrate over to a new server?
I think I understand your predicament now but I have no solution for you. This is just one of those odd ball things that I have never heard of in my 20 years of experience.


----------



## Squashman (Apr 4, 2003)

Maybe my Google foo is good today. Just started searching for scripts to copy files modified in the last hour and found this VBscript

```
Const srcDir = "C:\HourTest"
Const destDir = "H:\HourTest"
hourAgo = DateAdd("h", -1, Now)
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder(srcDir).Files
  If f.DateLastModified > hourAgo Then _
    f.Copy destDir & "\"
Next 'f
```


----------



## ckphilli (Apr 29, 2006)

How about making your old ftp folder hot? I haven't done this sort of thing yet but it looks pretty interesting. The basic idea is that you hook into windows services, once a file hits your folder it triggers a process.

Here's one that looked promising:

http://code.google.com/p/hotfolder/

Again, I've just "heard" of hot folders, no direct experience.


----------



## Scottcom4 (Apr 30, 2012)

Hi Ckphilli,

That is a great idea.

Unfortunately I don't have any ties with IT to implement such a process and to do so would probably take them too long as I need this task completed by the weekend.

Thanks for puting the idea forward, your help is aprpeciated.

Regards,
Scott


----------

