Link to home
Start Free TrialLog in
Avatar of cheryl9063
cheryl9063Flag for United States of America

asked on

SSIS script task

How do you write a VB script that looks in a folder and finds only the files that where added today?
Avatar of knightEknight
knightEknight
Flag of United States of America image

In VBScript you can do something like this:

dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
dim objFile:Set objFile = objFSO.GetFile("c:\MyFile.txt")
IF objFile.DateCreated = Today THEN
  Wscript.Echo objFile.FileName
Avatar of cheryl9063

ASKER

thanks..what does that mean..Echo.objFileName? Can I put that before my FileSystemTask? My fileSystemTask uses variables that have the source and destination folders and look for *.* files.. How should I do this? This is all in a For Loop Container
Sorry, here is a working version:

dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
dim objFile:Set objFile = objFSO.GetFile("x.vbs")
IF left(objFile.DateCreated, instr(objFile.DateCreated, " ")-1) = left(Now(), instr(Now(), " ")-1) THEN
  Wscript.Echo objFile.Name & " Created Today."
END IF
instead of GetFile("x.vbs") you would use the filename from your FOR loop, so...

for MyNextFileName ...
  dim objFile:Set objFile = objFSO.GetFile(MyNextFileName)
thanks.. see my question before.. Sorry just little experience with this
then I connect it to the File system task?
Post your script and I will modify it to find only files created on today's date.  (Change any sensitive stuff like IPs or passwords before you post it here though.)
ok, stupid question.. How do I send it to you? It has the extension of dtsx which is not in the list of accepted extensions on this site..
oh, I thought this was a VBScript question, no?  I don't know what .dtsx is either.  If it is VBScript, just copy/paste the portion that retrieves the *.* files (in the FOR loop) here.  No need to attach a file, just copy/paste will do.
Sorry.. its a SSIS package,, there is no code.. its just a Forloop container that has a variable for the source and one for destination with a file system task in it.
I get all kinds of errors in the integration services script task,,

      Public Sub Main()
        Dim objFSO : objFSO = CreateObject("Scripting.FileSystemObject")
        Dim objFile : objFile = objFSO.GetFile("x.vbs")
        If Left(objFile.DateCreated, InStr(objFile.DateCreated, " ") - 1) = Left(Now(), InStr(Now(), " ") - 1) Then
            Wscript.Echo(objFile.Name & " Created Today.")
        End If

            Dts.TaskResult = ScriptResults.Success
      End Sub
the errors are variable declarations without an As clause type of object assumed.. this is on the declarations.. then on the Wscript part I get name not declared... Are you doing this for SSIS?
What I posted was just a sample VBS script to show generically how to identify files created on the current date.
Particularly, the Wscript.Echo statement just does what it says, it echos the matching filenames -- it is a sample statement and has no use in your SSIS code.
Also, you can't use  "x.vbs"  , that was just my sample file name.  In its place (in a real VBScript) you would put the variable that contains the filename like GetFile(myFileVariable)
Finally, I'm guessing the declaration messages are just warnings.

Sorry, my initial understanding of your question was limited in scope and I did not take SSIS into account:

"How do you write a VB script that looks in a folder and finds only the files that where added today?"
You may want to have this question deleted and re-post a new one with more details about what you need.
User Script task.

Or have some other tool to compare to folders. Using that get the new files and copy them using File System Task.
That's true - you can have a batch script or VB script that copies today's files to a separate folder, then your SSIS package can use that folder.  Let us know if you need help with this.
Yes I do need help.. How do I copy only the files I need from one folder to another? The files I need are only the ones created today.. for each day..It would be awesome if someone could create a sample one to show me.
Where are the files now (what folder are they in)?  ... and where would you like to copy the files created today? (to what folder)
Folder1 has hundreds of files with an extension like .P10121302 etc..every day new files are added with different extensions.. I need to move the files created today to a new folder called folder 2.
Folder1 has hundreds of files with an extension like .P10121302 etc..every day new files are added with different extensions.. I need to move the files created today to a new folder called folder 2.
understood, but if you provide the full path the script I give you will require less modification.  Would you be able to translate from the generic to the specific if I used this in the script?

C:\Parent Folder\Folder1
C:\Parent Folder\Folder2


Absolutely...thank you spool much
Absolutely...thank you sooo much!
Before I post I need to verify your default system date format.  Please run the following command from a command prompt and post the result here:

  echo %date%

thanks
Assuming the results of the above is this (U.S.):  Fri 06/03/2011
... then the following code should be saved as a .bat file and scheduled to run automatically just before your SSIS task runs:

@echo off
 setlocal

 set SOURCEFOLDER="C:\Parent Folder\Folder1"
 set DESTINATIONFOLDER="C:\Parent Folder\Folder2"
 set FILESPEC="*.txt"

 cd/d  %SOURCEFOLDER%
 for /f "tokens=5,*" %%F in ('dir/tw/a-d %FILESPEC% ^| findstr "%date:~4%"') do  copy  "%%F"  %DESTINATIONFOLDER%
change the value of the three variables to suite your needs.
Thanks! Sorry its taking some time.. got pulled away but I sincerely appreciate your help.

Fri 06/03/2011
I saved the below as a .bat file. It has the real folder names now and then I ran it.. The files from today did not move?

@echo off
 setlocal

 set SOURCEFOLDER="G:\DataProcessing\Plt15\Lexicent\Tickets\temp"
 set DESTINATIONFOLDER="G:\ADP\Plt15\Lexicent\Tickets\testing"
 set FILESPEC="*.txt"

 cd/d  %SOURCEFOLDER%
 for /f "tokens=5,*" %%F in ('dir/tw/a-d %FILESPEC% ^| findstr "%date:~4%"') do  copy  "%%F"  %DESTINATIONFOLDER%
Did you change the FILESPEC variable?  What type if files are in the SOURCE folder?
I did.. I'm new here (third day) but I think its a CSV.. Im attaching one (with a .txt) on the end so I can attach it to this site but its not a .txt.. So take the .txt off and that is what the file is.
LEXICENT.P11060301.txt
Its an unknown file type.. used by a web page..
so I changed it to *.* and now it works.. So i just need to add that to the Forloop and forget the script task correct?
The idea is to run the .bat file separately to move today's files to Folder2, and then run the SSIS package which operates on the files in Folder2.  Once the files have been processed they should be deleted from Folder2 (presumably).  Or, if there is an SSIS task that can run .bat scripts, then you can include this as part of the SSIS package.

Also, if you want to "move" the files instead of "copy" them, just change the word copy to move in the .bat script.
Ok so you are awesome!..when I get done rebooting I will close this and give points. Thanks so much..you saved my first week on the job!
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect solution