Solved

File handling with ASP or VB

Posted on 2003-12-10
12
1,083 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
Comment Utility
@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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 19

Expert Comment

by:Dexstar
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
never mind, found it. :)
0
 
LVL 19

Expert Comment

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

Peace,
Dex*
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
withoutTen challenge 14 88
How to split this in C++ 4 76
count7 challenge 12 66
parse convert xml feed to text (python) 2 27
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now