September 18, 2015 at 7:08 am
I need to create a package to do the following
the package will have 3 input parameters
1) Startdate
2) Enddate
3) RecordCount
I'm executing a sql task which reads an 'Orders' table and extracts as many records as meets the Start - End date selection, part of the data extracted is a URL to a document location. Using the URL I then need to copy the document to a folder called 'Orders' which will potentially have subfolders. These subfolders will be created if the number of records retrieved is greater than the RecordCount parameter, so if the parameter is set to 1000, then a new subfolder should be created for every 1000 records, these subfolders will be incrementally named so I'll end up with a folder structure something like
Orders
|___|--> 1
|--> 2
|--> 3
|--> 4
How can I script the folder creation, I assume I'll need a couple of variables to hold the current record count and the folder number, but I've no idea how to script this. Any help would gratefully appreciated
September 18, 2015 at 7:23 am
rather than an arbitrary count of 1000 or some other static value, would it make more sense to put files in folders based on the dates?
you could group by month, for example, and create any folders with the month name,and logically stick orders in the proper month.
for me, it would be a lot more intuitive. if i new there was an invoice from June, i'd have to poke and hunt through folders, but with an intuitive date name, i could drill in quicker.
code wise, it's just two lines a script task:
//determine the right assignment to the variable via code?
string dir ="2015_01"
if (!System.IO.Directory.Exists(dir))
{System.IO.Directory.CreateDirectory(dir);
--copy files too this directory?
}
Lowell
September 18, 2015 at 7:49 am
The documents are being migrated to a new application, so at the moment the date of the order is unimportant as all documents will eventually be moved
September 18, 2015 at 7:57 am
Lowell (9/18/2015)
rather than an arbitrary count of 1000 or some other static value, would it make more sense to put files in folders based on the dates?you could group by month, for example, and create any folders with the month name,and logically stick orders in the proper month.
for me, it would be a lot more intuitive. if i new there was an invoice from June, i'd have to poke and hunt through folders, but with an intuitive date name, i could drill in quicker.
code wise, it's just two lines a script task:
//determine the right assignment to the variable via code?
string dir ="2015_01"
if (!System.IO.Directory.Exists(dir))
{System.IO.Directory.CreateDirectory(dir);
--copy files too this directory?
}
+1
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 20, 2015 at 3:44 am
Do I just create a variable to hold the record count, in the same way as you created the directory variable, if so how do I reset to 0 once the folder size limit (1000) is reached
September 20, 2015 at 10:28 am
Newbie36037 (9/18/2015)
The documents are being migrated to a new application, so at the moment the date of the order is unimportant as all documents will eventually be moved
These types of applications normally skyrocket with files. I recommend you make a plan to figure out a meaningful directory structure now.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 21, 2015 at 1:20 am
The files will only be extracted once, I'm putting them in subdirectories as I don't know the process that the 3rd party software will use to reload them
September 21, 2015 at 7:10 am
the sub folders would server no purpose then,except make the processing a little more difficult. processing a single folder vs lots of subfolders just adds to teh complexity of later processing.
i'd just stick everything in a single directory.
'if you just HAVE to have them in sub folders, i would make the process stick them all in a single folder, and then use a script task to move files into sub folders based on a counter after the fact.
this is my untested guess at the code:
public void processFolder()
{
string TargetDirectory = "C:\\Data";
string FileExtensionsToProcess = "*.xls";
int FileCounter = 1;
int FolderName = 1;
foreach (string FileName in System.IO.Directory.GetFiles(TargetDirectory, FileExtensionsToProcess))
{
string FileOnly = System.IO.Path.GetFileName(FileName);
try
{
if (!Directory.Exists(Path.Combine(TargetDirectory, FolderName.ToString())))
{
Directory.CreateDirectory(Path.Combine(TargetDirectory, FolderName.ToString()));
}
File.Move(FileName, Path.Combine(TargetDirectory, Path.Combine(FolderName.ToString(), FileOnly)));
}
catch
{
}
FileCounter += 1;
if ((FileCounter % 1000) == 0)
{
FolderName += 1;
}
}
}
Lowell
September 21, 2015 at 10:16 am
Cheers Lowell,
With a bit of tweaking that gave me what I needed, and saved me hours of frustration
September 21, 2015 at 10:22 am
Newbie36037 (9/21/2015)
Cheers Lowell,With a bit of tweaking that gave me what I needed, and saved me hours of frustration
what did you have to tweak? your answer might save someone else in the future.
Lowell
September 21, 2015 at 11:40 pm
Made a couple of changes to the directory locations, added a source & target directory and converted to VB code as I'm using 2005 and it doesn't like c#
Dim SourceDirectory As String = "C:\Data"
Dim TargetDirectory As String = "E:\CN"
Dim FileExtensionsToProcess As String = "*.pdf"
Dim FileCounter As Integer = 1
Dim FolderName As Integer = 1
For Each FileName As String In System.IO.Directory.GetFiles(SourceDirectory, FileExtensionsToProcess)
Dim FileOnly As String = System.IO.Path.GetFileName(FileName)
Try
If Not IO.Directory.Exists(IO.Path.Combine(TargetDirectory, FolderName.ToString())) Then
IO.Directory.CreateDirectory(IO.Path.Combine(TargetDirectory, FolderName.ToString()))
End If
IO.File.Copy(FileName, IO.Path.Combine(TargetDirectory, IO.Path.Combine(FolderName.ToString(), FileOnly)))
Catch
End Try
FileCounter += 1
If (FileCounter Mod 1000) = 0 Then
FolderName += 1
End If
Next
Dts.TaskResult = Dts.Results.Success
September 23, 2015 at 1:05 am
xc
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply