Copy into Dated directory ever 5 min ...

by Lahru at 2012-10-12 09:53:56

I’m continuing with a process if anyone here has read previous posts from me …
I’m running the following script every 5 minutes :
##########################################################################################################
#-----The purpose of this script is to copy/move items from a fax line’s dropbox ($source) … ----------#
#-----… to the folder that a program looks to for Importing these files ($destination) & … ----------#
#-----… also to a backup folder ($Bkp). All copy/move actions are logged to “copy.log”. -------------#
##########################################################################################################

#-----Set Debugging Level-----#
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = “Continue”

#-----Set Variables-----#
$Source = “\UNC\path\goes\here”
$Destination = “\UNC\path\goes\here”
$Bkp = “\UNC\path\goes\here”
$Time = GET-DATE

#-----Create If/Else statement to copy/move items from source to destination & backup…--------#
#-----…write a Success.txt file to be used by a CheckTimeStamp script. This section…-------#
#-----…will also append certain text between the “” to the copy.log file for tracking.--------#
if(Test-Path -Path $Source* -include *.pdf, *.tif)
{
New-Item -Path $Monitor -name SUCCESS.txt -Value “Success @ $time!” –itemtype file -force
Add-Content -Path $Source\copy.log -value “BEGIN ACTION @ $time
Copy-Item -path $Source* -include .pdf,.tif -destination $Destination -passthru | format-table -property LastWriteTime,Name -autosize | out-file $Source\copy.log -Append -Encoding ascii
Move-Item -path $Source*.pdf -destination $Bkp
Move-Item -path $Source*.tif -destination $Bkp
Add-Content -Path $Source\copy.log -value “STOP ACTION @ $time
Add-Content -Path $Source\copy.log -value “================================================”
Copy-Item $Source\copy.log $Bkp\copy.log
}
ELSE
{
Add-Content -Path $Source\copy.log -value “*BEGIN ACTION @ $time
Add-Content -Path $Source\copy.log -value “0 Files Copied”
Add-Content -Path $Source\copy.log -value “STOP ACTION @ $time
Add-Content -Path $Source\copy.log -value “================================================”
}


Here is my issue:
The files dropped in $Source are user named. (“ugh!”) This means that the backup destination invariably ends up with duplicate file names & the MOVE action will fail for those files with the same name in both locations.
I thought that adding the following line …:
$Bkp = New-Item -Name $((Get-Date).ToString(‘MM.dd.yyyy’)) -Type Directory -Path "\UNC\path\goes\here"
… would resolve this, except that testing shows it will only work for the 1st time the script is run on any given day.

So it occurs to me, "I should run another script to create this folder at the start of each day."
and that’s fine, but …
How do I get THIS script to use a folder JUST created TODAY and named by the date?
by DonJ at 2012-10-12 11:21:35
Well, I don’t think you need to break this into two scripts. Not if I’m following what you’re trying to do. Just have your script check to see if today’s folder already exists (Test-Path) before trying to create it. So, in Pseudo-code:

1. Define folder name (today’s date)
2. Check if it exists.
3. If it doesn’t, make it.
4. Proceed with copy/move/whatever.

Step 2 is what you’re missing right now, which is why you’re finding that it only works the first time. Every other time after that each day, the folder already exists so New-Item fails.

But I’m not sure having a unique folder per day will help if you’re running this every 5 minutes, right? You still could end up with duplicate filenames within a single day, correct?

There’s a few ways you could probably approach this. For example, don’t let the cmdlets copy/move the files in batches as you’re currently doing. Manually enumerate through the source files, one at a time. That way, you can check for a duplicate filename (Test-Path) before trying to move each file. If you detect a duplicate, just add a sequence number or something to the end of the destination filename (e.g., “MyFile 1.txt,” “MyFile 2.txt”).

Alternately, rather than creating a folder that uses the date, create one that uses the date and time. That way, your New-Item line will produce a unique folder name each time your script runs.
by Lahru at 2012-11-14 12:35:32
[quote]But I’m not sure having a unique folder per day will help if you’re running this every 5 minutes, right?[/quote]Actually It will help in my situation, its extremely unlikely that the file names will duplicate within the same day.[quote]If you detect a duplicate, just add a sequence number or something to the end of the destination filename (e.g., “MyFile 1.txt,” “MyFile 2.txt”).[/quote]This is a GREAT idea and I may use it in the future, however in this current situation I don’t think it’s necessary.
Here is the final code I ended up using:
##########################################################################################################
#-----The purpose of this script is to copy/move items from a dropbox type folder ($source) … ---------#
#-----… to the folder that SomeApp looks to for Importing these files ($destination) & … ---------#
#-----… also to a backup folder ($Bkp). All copy/move actions are logged to “copy.log”. ---------#
##########################################################################################################

#-----Set Debugging Level-----#
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = “Continue”

#-----Set Variables-----#
#-----Set Source Directory-----#
$Source = “\UNC\Path\Goes\Here”

#-----Set Destination/Import Directory-----#
$Destination = “\Here\goes\unc\path”

#-----Set Backup Directory-----#
$Bkp = “\Path\to\directory$((Get-Date).ToString(‘MM.dd.yyyy’))”

#-----Set which files to include in the file count-----#
$FilesToCount = get-childitem $source* -include *.jpg, *.pdf, *.tif

#-----Set property to use for count in conjunction with immediately previous object-----#
$Count = $FilesToCount.count

#-----Gather the current system time and store it in an object-----#
$Time = GET-DATE

#-----Create If/Else statement to copy/move items from $source to $destination & $backup.-------#
#-----This section will append certain text between the “” to the copy.log file for tracking.—#
#-----If no files are processed the ‘Else’ statement logs such and sends and e-mail alert.------#
if(Test-Path -Path $Source* -include *.pdf, *.jpg, .tif)
{
Add-Content -Path $Source\copy.log -value “BEGIN ACTION @ $time
Add-Content -Path $Source\copy.log -value “”
Add-Content -Path $Source\copy.log -value “Files to be processed = $Count”
Copy-Item -path $Source* -include .pdf,.tif,
.jpg -destination $Destination -passthru | format-table -property LastWriteTime,Name -autosize | out-file $Source\copy.log -Append -Encoding ascii
Move-Item -path $Source*.pdf -destination $Bkp
Move-Item -path $Source*.tif -destination $Bkp
Move-Item -path $Source*.jpg -destination $Bkp
Add-Content -Path $Source\copy.log -value “STOP ACTION @ $time
Add-Content -Path $Source\copy.log -value “================================================”
Add-Content -Path $Source\copy.log -value “”
Copy-Item $Source\copy.log $Bkp\copy.log
}
ELSE
{
Add-Content -Path $Source\copy.log -value “*BEGIN ACTION @ $time
Add-Content -Path $Source\copy.log -value “”
Add-Content -Path $Source\copy.log -value “0 Files Copied”
Add-Content -Path $Source\copy.log -value “”
Add-Content -Path $Source\copy.log -value “STOP ACTION @ $time
Add-Content -Path $Source\copy.log -value “================================================”
Add-Content -Path $Source\copy.log -value “”
Send-MailMessage -From PS@thisplace.com -To “Recipient <Recipient@thisplace.com” -Subject “Subject” -Body “Body of the message” -SmtpServer smtpserver.thisplace.com
}
#-----End of Script-----#