[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Help with error trapping in a script

I'm moving some 1600 files from one server to another. The destination server has folder names that match the source file name. The files are word documents i want stuffed into the matching folder name on the destination server. It would be nice also if the directory don't exist on the destination to create it and then stuff the file.  I think the below script will work if you could trap the "file already exist" error.  If the file already exist on the destination server i would just like to skip it and go to the next file and try to move it.  Hope this makes sense.  Below is the script i am working with at this time. If you have a better way please show me.

'  MoveDataFiles.VBS
'  Moves files from one directory to another directory where the destination directory is
'  named the same as the filename of the file.  Example:
'            Source Directory ==>  c:\temp
'           Source Filename  ==>  abc123.doc
'      Destination Directory ==>  abc123
'      Destination Filename ==>  abc123.doc
'  This script assumes that the destination directory already exists.

intCount = 0
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
     intCount = intCount + 1    
     strFileName = objFile.Name
     DirectoryArray = Split(strFileName, ".", -1, 1)
     
     objFSO.MoveFile strSourcePath & strFilename, strDestinationPath & DirectoryArray(0) & "\" & strFileName

Next

Wscript.Echo "File moves completed.  " & intCount & " files moved."


'  End of MoveDataFiles.VBS
0
JAYRU
Asked:
JAYRU
  • 4
  • 2
2 Solutions
 
Ryan ChongCommented:
Try check on the FileExists method in FSO, like:

so..


intCount = 0
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
     intCount = intCount + 1    
     strFileName = objFile.Name
     DirectoryArray = Split(strFileName, ".", -1, 1)
 
'Do only file doesn't exist  
  If objFSO.FileExists(strDestinationPath & DirectoryArray(0) & "\" & strFileName) = False Then
     objFSO.MoveFile strSourcePath & strFilename, strDestinationPath & DirectoryArray(0) & "\" & strFileName
 End If

Next
0
 
JAYRUAuthor Commented:
ryancys,
I have to wait till Monday to have my friend from IT department to recompile the script.  I don't have VB on this machine.  Unless you want to compile it and email me the file.  If you do name it  MoveDataFiles.vbs.........You may have to change the file extention to something other than vbs.  Not sure if Yahoo lets vbs through.  If not i'll wait till Monday...........thanks.      jayrues@yahoo.com
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi JAYRU,

You don't compile a vbscript, it is simply a text file with the .vbs extension.  You can open up NotePad, paste the code, save the file, change the extension from .txt to .vbs and then you have your script ready to run.

Regards,

Idle_Mind
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JAYRUAuthor Commented:
hmmmmmmmm.........I did that and it did nothing with no errors.  Thats why I was thinking maybe it had to be compiled.  Now what?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is my version:

'  MoveDataFiles.VBS
'  Moves files from one directory to another directory where the destination directory is
'  named the same as the filename of the file.  Example:
'           Source Directory ==>  c:\temp
'          Source Filename  ==>  abc123.doc
'     Destination Directory ==>  abc123
'     Destination Filename ==>  abc123.doc

' Operation tracking counters
filesMoved = 0
filesSkipped = 0
foldersCreated = 0

' Source and Destination folders
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

' Create FileSystemOject, get reference to Source folder and collection of source files
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

' Process each file in Source folder
For Each objFile in colFiles
    ' Strip off extension
    strFileName = objFile.Name
    name = strFileName
    ext = objFSO.GetExtensionName(objfile)
    If ext <> "" Then
        name = Left(name, len(name) - (len(ext) + 1))
    End If
   
    ' Create destination folder if it doesn't exist
    If Not objFSO.FolderExists(strDestinationPath & name) Then
        objFSO.CreateFolder(strDestinationPath & name)
        foldersCreated = foldersCreated + 1
    End If
   
    ' Move file if it doesn't exist
    If Not objFSO.FileExists(strDestinationPath & name & "\" & strFileName) Then
        objFSO.MoveFile strSourcePath & strFileName, strDestinationPath & name & "\" & strFileName
        filesMoved = filesMoved + 1
    Else
        filesSkipped = filesSkipped + 1
    End if
Next

' Give feedback to the user
msg = filesMoved & " files moved." & vbCrLf
msg = msg & filesSkipped & " files skipped." & vbcrlf
msg = msg & foldersCreated & " folders created."
MsgBox msg, vbInformation, "File moves complete"
0
 
JAYRUAuthor Commented:
Ryancys,
New revelation.  I started over with the original script i posted and just plugged in your IF EXIST code and it did say it moved 2 files but should of moved some 1600.........something is working anyway........below is the code that did move 2 files but not 1600.

_________________________________
intCount = 0
strSourcePath = "\\jaxnt51\proceng\pms\pm\"
strDestinationPath = "\\jaxnt54\plant\machines\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSourcePath)
Set colFiles = objFolder.Files

For Each objFile in colFiles
     intCount = intCount + 1    
     strFileName = objFile.Name
     DirectoryArray = Split(strFileName, ".", -1, 1)

'Do only file doesn't exist  
  If objFSO.FileExists(strDestinationPath & DirectoryArray(0) & "\" & strFileName) = False Then
     objFSO.MoveFile strSourcePath & strFilename, strDestinationPath & DirectoryArray(0) & "\" & strFileName
end if

Next

Wscript.Echo "File moves completed.  " & intCount & " files moved."


'  End of MoveDataFiles.VBS
____________________________
0
 
JAYRUAuthor Commented:
I don't know what happened but I think it was that I didn't refresh explore but it did move all the files.  I'm  not even sure which one of your scripts i was using at the time but both scripts are very valueable to me and if you think its fair i am going to up the points to 300 and then split the points...........EE and its experts never let me down.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now