Solved

VBScript: moving files and FTP

Posted on 2004-09-20
11
3,551 Views
Last Modified: 2008-01-09
Hi all

I'm a complete newcomber to vbs and I'm learning as I go.

I cobbled together some code I found on the net (partly from here).  It looks in a directory for .mpg and .mpeg files, changes the extension to .mp2, then runs some software that converts the files to wav, which is able to place the result on a network drive.

There are varying numbers of files in the source directory, and the names change, but once the script is run all the results end in .wav

I'd like to do two more things.  The first is convert them locally them move them to the new location rather than move them on-the-fly.

I think this is just a case of understanding what I'm doing !   I've tried a few times but end up in a mess with For, Next, If and Then, and repeating Dim commands !

The second is more complex and deserves another thread and more points - so I'll post that once this one is solved!

Here's the current script.  I appreciate it's messy - I've probably repeated things I don't need to - but it does work !

Thanks

Simon

' Converts MPEG files to 44.1 stereo linear WAVs

call waveconvert("mycnv")
Sub WaveConvert(directory)

' Rename files to .mp2

Dim fso, fdr, filex, fcount
Dim sNewName
Set fso = CreateObject("Scripting.FileSystemObject")
Set fdr = fso.GetFolder("c:\sourcefiles\")
Set fcount = fdr.Files

'  Do .mpeg files
For Each filex In fcount
      If Right(filex.Name, 5) = ".mpeg" Then
      sNewName = "c:\sourcefiles\" + Left(filex.Name, InStrRev(filex.Name, ".")) + "mp2"
      fso.MoveFile "c:\sourcefiles\" + filex.Name, sNewName
      End If
Next

'  Do .mpg files
For Each filex In fcount
      If Right(filex.Name, 4) = ".mpg" Then
      sNewName = "c:\sourcefiles\" + Left(filex.Name, InStrRev(filex.Name, ".")) + "mp2"
      fso.MoveFile "c:\sourcefiles\" + filex.Name, sNewName
      End If
Next

Set filex = Nothing
Set fcout = Nothing
Set fdr = Nothing

' Set up dMC - this is the external app that does the conversion

' Create dMC Object
Set dMC = CreateObject("dMCScripting.Converter")

' Set options (Volume norm off, ID Tag Preservation On, No delete Source files)
dMC.VolumeNormalize = False
dMC.PreserveTags = True
dMC.DeleteSourceFiles = False

dMC.ConvertToFolder = True ' DO NOT to put results in original folder - this would be changed to False
dMC.ToFolder = "\\server\resultfiles\" ' Files will be sent here - this line would be removed to keep results in same folder

' Dim fso removed from here because it was defined earlier

Dim fldr, fls, fl, fldrPath, fPath, fExt, i
i=0

' Set where files come from, and what the extension should be
fldrPath = "c:\sourcefiles\"
fExt = ".mp2"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder(fldrPath)
Set fls = fldr.Files

For Each fl In fls

' check for file type
If Right(fl.Name, 4) = fExt Then

' build full path of file
fPath = fldrPath + fl.Name

' ===========================================================================
' Add files to convert
Call dMC.AddFromFile(fPath)
' ===========================================================================

End If
i = i + 1
Next

Call dMC.GoConversion("Wave", True, True, True, False)

End Sub

'  Then I need to move the results - which whould be  " c:\sourcefiles\*.wav " to "\\server\audioout\"
0
Comment
Question by:SimonUK
  • 6
  • 5
11 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 12102555
Hi, SimonUK.  

From your post I'm not quite sure what you're looking for.  If all you want to do is move all the .wav files after the conversion,
then this will do that.  I also took the liberty of straightening up a few things.  I hope you don't mind.

' Converts MPEG files to 44.1 stereo linear WAVs
Dim objFSO, objFolder, strSourcePath, strDestinationPath
strSourcePath = "C:\SourceFiles\"
strDestinationPath = "\\Server\Share\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call WaveConvert
Wscript.End

Sub WaveConvert()
    Dim objFile, objFiles, strNewName, fPath, fExt, i

    ' Rename files to .mp2
    Set objFolder = objFSO.GetFolder(strSourcePath)
    Set objFiles = objFolder.Files

    '  Do .mpeg and .mpg files
    For Each objFile In objFiles
        If Right(objFile.Name, 5) = ".mpeg" Or Right(objFile.Name, 4) = ".mpg" Then
            strNewName = strSourcePath & Left(objFile.Name, InStrRev(objFile.Name, ".")) & "mp2"
            objFSO.MoveFile objFile.Path, strNewName
        End If
    Next

    Set objFile = Nothing
    Set objFiles = Nothing
    Set objFolder = Nothing

    ' Set up dMC - this is the external app that does the conversion
    ' Create dMC Object
    Set dMC = CreateObject("dMCScripting.Converter")

     Set options (Volume norm off, ID Tag Preservation On, No delete Source files)
    dMC.VolumeNormalize = False
    dMC.PreserveTags = True
    dMC.DeleteSourceFiles = False

    dMC.ConvertToFolder = False  ' DO NOT to put results in original folder - this would be changed to False

    i = 0

    ' Set where files come from, and what the extension should be
    fExt = ".mp2"
    Set objFolder = objFSO.GetFolder(strSourcePath)
    Set objFiles = objFolder.Files

    For Each objFile In objFiles
        ' check for file type
        If Right(objFile.Name, 4) = fExt Then
            ' build full path of file
            fPath = objFolder & objFile.Name
            ' ===========================================================================
            ' Add files to convert
            Call dMC.AddFromFile(fPath)
            ' ===========================================================================
        End If
        i = i + 1
    Next
    Call dMC.GoConversion("Wave", True, True, True, False)
   
    'Move the files to the destination location
    For Each objFile In objFiles
        If Right(objFile.Name, 4) = ".wav" Then
            objFSO.MoveFile objFile.Path, strDestinationPath & objFile.Name
        End If
    Next
End Sub
0
 
LVL 1

Author Comment

by:SimonUK
ID: 12102857
Hi BlueDevilFan

No I don't mind at all - I can see where some of your tidying is and why - many thanks !

There's a  '  missing from

Set options (Volume norm off, ID Tag Preservation On, No delete Source files)

That's actually meant to be a comment - I guessing using the word "Set" wasn't very clever...

Anyway I fixed that, which was causing an 'expected )' error but now when the script gets as far as the conversion, the converter software it can't open the files because they are named   "c:\Sourcefilesfilename.mp2" etc  - the trailing backslash in the source path has been lost somewhere.  I've doubled checked and it is present in the initial strSourcePath so is it getting lost somewhere in the statement that builds the build full path of each file?

            fPath = objFolder & objFile.Name

Unfortunately I'm not an expert so I'm not sure what's missing.

The script does rename all the .mpg and .mpeg files to mp2 successfully.

Many thanks


Simon
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12103092
Sorry, I had to comment out all the code that dealt with the actual conversion, the dMC object, and I apparently missed uncommenting it before posting.  Yes, I can see why the path is being run together.  That's another bit of code I didn't pay attention to because it was part of the conversion.  You can fix it by replacing this line

    fPath = objFolder & objFile.Name

with

    fPath = objFolder & "\" & objFile.Name

The reason is that objFolder returns the path to the folder as a string not terminated with a \.
0
 
LVL 1

Author Comment

by:SimonUK
ID: 12103253
OK I can see what happened and I also understand the need for "\" now...

So now, it all works great except for one thing.  I get an error box right at the end that says

"Object doesn't support this property or method: 'Wscript.End' "

Thanks for all your help so far !

Simon

0
 
LVL 76

Expert Comment

by:David Lee
ID: 12103741
Oops, that should be Wscript.Quit instead of Wscript.End
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:SimonUK
ID: 12104005
OK great - that all works fine - except (!!!) that if the files already exist, the script fails with a 'file already exists' error.

Forgot to mention that the conversion routine version I originally used has a switch which tells it to overwrite at all times.

Obviously now we're moving them, that ability needs to exist (with no prompt).

Sorry to add to the project - this is the last item, promise !

Simon
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12104155
That's alright, I don't mind.  So you want to just flatly overwrite a file of the same name if it already exists?  Does it need to do this both on the local machine and on the server?
0
 
LVL 1

Author Comment

by:SimonUK
ID: 12104364
Yes - got it in one !

In theory it'll never need to overwrite locally because the local copies are moved away each time... but it might as well, just in case!

Simon
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 12104489
Ok.  To do that we have to abandon the MoveFile method, which cannot overwrite, and use the CopyFile method, which can.  Replace

    objFSO.MoveFile objFile.Path, strNewName

with

    objFSO.CopyFile objFile.Path, strNewName, True

and

    objFSO.MoveFile objFile.Path, strDestinationPath & objFile.Name

with

    objFSO.CopyFile objFile.Path, strDestinationPath & objFile.Name, True

The True on the end of both lines tells the CopyFile method to overwrite automatically.  Immediately after each of these copy commands add the following line of code

    objFSO.DeleteFile objFile.Path

What we end up doing to accomplish this is to copy the file and then delete the original.  That's the same thing a move does, it just does it in one command whereas we have to use two because the move won't overwrite.

0
 
LVL 1

Author Comment

by:SimonUK
ID: 12109379
Fantastic !  All works absolutely fine now!

Will award points now and start another thread for the next bits... which are to do with generating dialog boxes so the user knows what's going on and getting source files automatically from an ftp site.

Many thanks !


Simon
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12110523
You're welcome, Simon.  Cheers.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 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

22 Experts available now in Live!

Get 1:1 Help Now