Solved

File handling with ASP or VB

Posted on 2003-12-10
12
1,088 Views
Last Modified: 2010-04-17
Hi,
I am looking for a program that will read a filename (pdf files) from a large list of files in a Window's directory, parse it and then move/copy it to a different computer with a similar directory structure. There is a catch, the filename has different parts to it (e.g. state, department, Nov, 2003-12-04 hhmmss(this is the time format).pdf). We have offices is different states with different departments (for clarity purposes). This is the bare basic requirement but it will get me something to build on. My vb knowledge is kind of limited so some comments in the code will definetely be appreciated.

Thanks.
0
Comment
Question by:sayeed_seher
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 3

Expert Comment

by:sohilm
ID: 9915716
A little more detail would be helpful. Are you trying to duplicate the existing structure, or are you trying to create a brand new file & dir structure.

Thanks
Sohil
0
 
LVL 22

Expert Comment

by:cookre
ID: 9915733
All files in a specific directory, or just ones whose names match the given format?

What, exactly, is the filename format?

One presumes a move, unless, of course, the files will somehow get deleted before the next go.
0
 

Author Comment

by:sayeed_seher
ID: 9915895
The directory structure is simple. The 'State'(e.g. California) part of the filename should be parsed and all filenames with the same State should move to a folder called 'California' in a different location. Currently the files are being dumped in one folder and they are in a very large number(1000+). This is because there are different offices and those further break down to many departments. Hope this helps.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 22

Expert Comment

by:cookre
ID: 9916093
And how is the state portion of the filename identified - is a USPS 2 character abbreviation at the front of the filename, x chacters, a fully spelled out state name followed by some delimiter?  If the latter, do two word states get smooshed together or is there a blank?

So, presuming we find out how to identify the state portion of the file name, the file is copied (keeping the same file or stripping off the state name) to a child directory of SOMEDRIVE:\PARENTDIR whose name is the same as the state?

Will SOMEDRIVE: and/or PARENTDIR vary with state?  

Just as a simple example, here's a simple directory copy:



Option Explicit

Dim SrcDir As String
Dim DstDir As String
Dim NextFile As String

Sub main()
SrcDir = "C:\fromhere"
DstDir = "Y:\tohere"
NextFile = Dir(SrcDir + "\*.*")
Do While NextFile <> ""
   FileCopy SrcDir + "\" + NextFile, DstDir + "\" + NextFile
   NextFile = Dir
   Loop
End Sub
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9916209
@sayeed_seher:

> I am looking for a program that will read a filename (pdf files) from a large
> list of files in a Window's directory, parse it and then move/copy it to a
> different computer with a similar directory structure. There is a catch, the
> filename has different parts to it (e.g. state, department, Nov, 2003-12-04
> hhmmss(this is the time format).pdf). We have offices is different states with
> different departments (for clarity purposes). This is the bare basic requirement
> but it will get me something to build on. My vb knowledge is kind of limited
> so some comments in the code will definetely be appreciated.

Below is the code to a VBScript that you can use.  To use it, follow these steps:
1) Copy the code to a .VBS file
2) Edit the top part to set the source / destination paths
3) Run it.

It assumes that everything before the first COMMA in the file name is the name of the state.
For example, if the file name is "California, Whatever, Nov. 2003-12-04 125514.pdf", then it will be copied to DEST_PATH\California.

Is that what you want?  I also made it easy to switch between moving the files and copying the files.

Hope That Helps,
Dex*

Here is the code:
-----------------
      ' state, department, Nov, 2003-12-04 hhmmss(this is the time format).pdf
      OPTION EXPLICIT

      ' Declare Constants
      CONST SOURCE_PATH = ".\"
      CONST DEST_PATH = ".\"
      CONST FILE_EXT = ".pdf"

      ' Declare variables
      Dim fso, objSourceFolder, objFile, strState, strDestPath, strFinalPath

      ' Create Objects
      Set fso = CreateObject("Scripting.FileSystemObject")

      ' Get the source folder
      Set objSourceFolder = fso.GetFolder( SOURCE_PATH )

      ' Set the destination path
      strDestPath = DEST_PATH
      If ( Right(strDestPath, 1) <> "\" ) Then
            ' Make sure it ends in \
            strDestPath = strDestPath & "\"
      End If

      ' Process each File in the Folder
      For Each objFile In objSourceFolder.Files
            ' Check the Extension
            If ( Right(objFile.Name, Len(FILE_EXT)) = FILE_EXT ) Then
                  ' Get the "State"
                  strState = Left( objFile.Name, InStr(objFile.Name & ",", ",")-1 )
                  ' Get the final path to store the files
                  strFinalPath = strDestPath & strState & "\"
                  ' Check to see if the directory exists
                  If Not fso.FolderExists( strFinalPath ) Then
                        ' Create it if it doesn't exist
                        fso.CreateFolder( strFinalPath )
                  End If
                  
                  ' Copy/Move the file
                  objFile.Copy( strFinalPath )
                  'objFile.Move( strFinalPath )
            End If
      Next
0
 

Author Comment

by:sayeed_seher
ID: 9916222
The 'State' can be one word ('Texas') or multiple words ('New York') followed by a comma delimeter. When the file is copied to it's respective folder, I would like to strip the 'State' from the filename and also the date at the end just keeping the 'Department' part. The structure can be similar to:

C:\New York\Sales.pdf
C:\New York\Accounting.pdf
C:\Texas\Sales.pdf
C:\Texas\Accounting.pdf
etc...

Hope this helps

Thanks!
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9916236
@sayeed_seher:

One more thing, at the top, you'll see these lines:
     CONST SOURCE_PATH = ".\"
     CONST DEST_PATH = ".\"

If your original files are in "T:\Data\Whatever", and you want everything moved to "Q:\Other Data\WhateverAgain", then you would edit the file so those two lines look like this:
     CONST SOURCE_PATH = "T:\Data\Whatever\"
     CONST DEST_PATH = "Q:\Other Data\WhateverAgain\"

I hope that's clear to you.

Dex*
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 500 total points
ID: 9916301
Okay, here it is again, modified to remove the state from the destination file name...

      ' state, department, Nov, 2003-12-04 hhmmss(this is the time format).pdf
      OPTION EXPLICIT

      ' Declare Constants
      CONST SOURCE_PATH = ".\"
      CONST DEST_PATH = ".\"
      CONST FILE_EXT = ".pdf"

      ' Declare variables
      Dim fso, objSourceFolder, objFile, strDestPath, strFinalPath, nPos

      ' Create Objects
      Set fso = CreateObject("Scripting.FileSystemObject")

      ' Get the source folder
      Set objSourceFolder = fso.GetFolder( SOURCE_PATH )

      ' Set the destination path
      strDestPath = DEST_PATH
      If ( Right(strDestPath, 1) <> "\" ) Then
            ' Make sure it ends in \
            strDestPath = strDestPath & "\"
      End If

      ' Process each File in the Folder
      For Each objFile In objSourceFolder.Files
            ' Check the Extension
            If ( Right(objFile.Name, Len(FILE_EXT)) = FILE_EXT ) Then
                  ' Get the "State"
                  nPos = InStr(objFile.Name & ",", ",")
                  
                  ' Get the final path to store the files
                  strFinalPath = strDestPath & Left( objFile.Name, nPos-1 ) & "\"
                  
                  ' Check to see if the directory exists
                  If Not fso.FolderExists( strFinalPath ) Then
                        ' Create it if it doesn't exist
                        fso.CreateFolder( strFinalPath )
                  End If
                  
                  ' Remove the state from the file name
                  strFinalPath = strFinalPath & Trim(Mid(objFile.Name, nPos+1))
                  
                  ' Copy/Move the file
                  objFile.Copy( strFinalPath )
                  'objFile.Move( strFinalPath )
            End If
      Next
0
 

Author Comment

by:sayeed_seher
ID: 9916516
Thanks for the code! I will try it and keep you updated! A couple more things, how would I clean out the Destination folder for each state before I move new files in there if I move them on a monthly basis and also, how can I put this on an ASP page where all the user has to do is click a link say "move all files" and it does its thing. Finally, is there a way that the user can be notified via email that the files have been copied?

Thanks again!
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9916757
@sayeed_seher:

1) You can delete the folder before you move anything into it.  The FSO object has a "DeleteFolder" method.

2) You should be able to copy/paste that code into ASP page, and it will execute whenever that page is viewed.  However, you will either have to login to the web server, or give the IUSR_ access to the directories in question.

You may consider scheduling a script to run once a month, instead of requiring someone to visit a web page to activate it.

3) There are several ways of adding code to make it send an e-mail.  But it is tricky because it usually requires external objects that aren't always installed.  Maybe we could use CDO?

To be honest, I really think that exceeds the scope of your original question.  I noticed you also asked the EXACT same question in VB topic area.  Maybe you should close out this question, post my code to that other thread, and let someone else handle that part of it.  Just a suggestion.

Dex*
0
 

Author Comment

by:sayeed_seher
ID: 9916850
never mind, found it. :)
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9916877
So...  Did you get your answer to your question, or is there still more to do on this one?

Peace,
Dex*
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question