Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to automate manual file conversion 5

Status
Not open for further replies.

Gaffi1

Technical User
Apr 23, 2004
70
0
0
US
Hi, we are currently recording sound files in rhet32 format and need them in wav8. We have a utility that converts the files for us, but it requires we type a command into a cmd prompt for every single audio file and this is becoming a pain staking process.

The command that we pass through the cmd prompt is:
Conv32 /it:rhet32 /ot:wav8 s0123456.123 s0123456.wav

The idea I was hoping of coming up with is a script that pulls a list of all files in a directory and if it's not a .wav file execute that command on it and move the original file to archives; then move on to the next file until non remain.

I've looked around for ideas of varying degree and none have worked so far. The two problems I have are a) getting the list of files and b) getting the conv32 command to work using variables. Example below:
set strFile = s075134j.066
set strNewFile = jamestest.wav
conv32 /it:rhet32 /ot:wav8 strfile strnewfile

This problem however passes the literal text "strfile strnewfile" to the command instead of their values.

Any suggestions on how I can bypass these two problems?
 
You can test your loop logic with the following modification:

Code:
For Each objFile in colFiles
    If objFile.Extension = "066" Then

becomes

Code:
For Each objFile in colFiles
    wscript.echo objFile.Extension
    If objFile.Extension = "066" Then

This will let you verify you are looping through the files and see if you are skipping a .066 or if it's failing instead.
 
ok, the test worked well, it looped through the files in the directory except the second 066 file, lol. I go into task manager and kill the conv32 process and it loops through the remaining 066 file and converts it and leaves another random conv32 process out there. So I think I just need to kill the conv32 process in the loop before it goes again and I'll be set. What command would I enter to do that?
 
Instead of:

Code:
            wshshell.run "conv32 /it:rhet32 /ot:wav8 " & "" & strFile & "" & " " & "" & strNewFile & "",1,true

try

Code:
            wshshell.exec "conv32 /it:rhet32 /ot:wav8 " & strFile & " " & strNewFile

? not sure about this one....note the loss of the ,1,true at the end
 
This is how to kill a process.....

Code:
Call KillProc("notepad.exe")

'===============================
Sub KillProc(strNameOfAppToKill)

Dim objWMI
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Process")

   for each Process in objWMI
     If lcase(Process.Name) = lcase(strNameOfAppToKill) Then
         Wscript.echo Process.Name
         Process.Terminate
     End If
     
   next

End Sub
'===============================

 
jerz, i don't know if you are a guy or a girl, but whatever you are, you are the BOMB! That worked and it's working perfectly now!!!

I might come back with some hypotheticals later, but I'm so glad to get this part working I'm going to go put it in production and see how it does, lol.

Thank ya, thank ya, thank ya!
 
ok, so time for the hypotheticals...

1) is it possible to list all files in a directory and its sub directories instead of just the specified folder?

[Current Usage]
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\test'} Where " _
& "ResultClass = CIM_DataFile")

2) is it possible to yank the conv32 utility from just one location instead of having to put it in every single subdir and creating a seperate script for every directory?

[Example]
C:\ConversionUtil instead of C:\SoundDir\File1

3) is it possible to convert files with any extension type as long as its not currently a wav?

[Current Usage]
If objFile.Extension = "066" Then
 
Not to beat a dead horse but if you switch to using the FSO folder object it will be easy to recurse into the subdirectories...
 
I agree with Sheco, FSO folder object is your best bet if you want to hit subdirectories.

If you have multiple folders you could have it be based off an argument. So, YourScript.exe C:\Temp would run the program for the C:\Temp directory.

strPath = Wscript.Arguments.Item(0)

Or, you could also have it prompt for a folder when you first run it.

strFolder = InputBox("Path:", "File Converter")

Another option would be to plug into the windows open file window (do a search for a thread on how to do this). That way, you can just browse to the folder you want to run the process on.
 
Oops, not an exe file:
YourScript.VBS C:\Temp
YourScript C:\Temp
 
OK, I think I've converted the code correctly for this to work with FSO but I'm still getting an error message... could use a hand...

Code:
set wshshell=createobject("wscript.shell")
Set FSo = CreateObject("Scripting.FileSystemObject")
Tmpdir= "C:\soundtest"
wshshell.currentdirectory="c:\soundtest\ConvUtil"    'set your path to conv32.exe
Set oFolder = fso.GetFolder(TmpDir)
For Each objFile In oFolder.files


	If objFile.Extension <> "wav" Then
		If objFile.Extension <> "exe" Then
    			strFile = objFile.FileName & "." & objFile.Extension
    			strNewFile = objFile.FileName & ".wav"
    			wshshell.run "conv32 /it:rhet32 /ot:wav8 " & "" & strFile & "" & " " & "" & strNewFile & "",0,false
			CommandText = "conv32 /it:rhet32 /ot:wave8 " & strFile & " " & strNewFile
			wscript.echo CommandText ' remove this line if it works to let it process without hitting keys
			wshshell.run CommandText,0,true
		End If
	End If
Next

The error message I am getting is :
Code:
Windows Script Host
Script: C:\soundtest\scripts\Autoconv32.vbs
Line: 9
Char: 2
Error: Object doesn't support this property or method: 'objFile.Extension'
Code: 800A01B6
Source: Microsoft VBScript runtime error
 
Extension and FileName aren't properties for a FileSystemObject File. Instead, you have to crop the file name so that there's no extension.

Code:
Set WshShell=CreateObject("WScript.Shell")
Set FSo = CreateObject("Scripting.FileSystemObject")
Tmpdir= "C:\soundtest"
WshShell.CurrentDirectory="c:\soundtest\ConvUtil"
Set oFolder = fso.GetFolder(TmpDir)
For Each objFile In oFolder.files
    strFile = objFile.Name
    strNewFile = Left(strFile,InStrRev(strFile,".")) & "wav"
    strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
    If strExtension <> "wav" and strExtension <> "exe" Then
        CommandText = "conv32 /it:rhet32 /ot:wave8 " & strFile & " " & strNewFile
        WScript.Echo CommandText
        WshShell.Run CommandText,0,True
    End If
Next
 
Ok, so switching over to FSO, here is where I am at....
Code:
Set WshShell=CreateObject("WScript.Shell")
Set FSo = CreateObject("Scripting.FileSystemObject")
Tmpdir= "C:\soundtest\"
WshShell.CurrentDirectory="c:\soundtest\ConvUtil"
Set oFolder = fso.GetFolder(TmpDir)

For Each objFile In oFolder.files
	strFile = objFile.Name
	strNewFile = Left(strFile,InStrRev(strFile,".")) & "wav"
	strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
	If strExtension <> "wav" Then
		If strExtension <> "exe" Then
			WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & "" & TmpDir & strFile & "" & " " & "" & TmpDir & strNewFile & "",0,false
        		CommandText = "conv32 /it:rhet32 /ot:wave8 " & TmpDir & strFile & " " & TmpDir & strNewFile
        		WScript.Echo CommandText
    		End If
	End If
Next

Now this works to an extent. The idea here is to run run the conv32 utility against all files in the tmpDir and its subdirs. With this script it will convert files in the tmpDir using the conv32 utility that is NOT located in the same directory, which was a evolution in its own. However, it doesn't touch the files in the subdirs.

When I got to playing around with this problem, I realized that I have a slight problem that will need to be resolved at the same time. To convert files in the subdirs, I need to be able to include the directory that the file is in along with the file name in the conv32 command in order for it to run. So it would look something like this:
Code:
conv32 /it:rhet32 /ot:wav8 c:\soundtest\subdir1\s0000000.123 c:\soundtest\subdir1\s0000000.wav

So to summarize:
Problem 1: It's not seeing the subdir files
Problem 2: When converting subdir files, how will we be able to read the dir that the subdir file is in so that we can add it to the conv32 command?

I should also note, that looking at some other threads I came accross the FileSystemObject faq and tested the script like this (see below) to see if this would resolve problem 1. However, instead of seeing subdir files it saw the subdirs as files and of course tried to convert them, lol.

Code:
Set WshShell=CreateObject("WScript.Shell")
Set FSo = CreateObject("Scripting.FileSystemObject")
Tmpdir= "C:\soundtest\"
WshShell.CurrentDirectory="c:\soundtest\ConvUtil"
Set oFolder = fso.GetFolder(TmpDir)

For Each objFile In oFolder.files
	strFile = objFile.Name
	strNewFile = Left(strFile,InStrRev(strFile,".")) & "wav"
	strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
	If strExtension <> "wav" Then
		If strExtension <> "exe" Then
			WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & "" & TmpDir & strFile & "" & " " & "" & TmpDir & strNewFile & "",0,false
        		CommandText = "conv32 /it:rhet32 /ot:wave8 " & TmpDir & strFile & " " & TmpDir & strNewFile
        		WScript.Echo CommandText
    		End If
	End If
Next

For Each objFile in oFolder.subFolders
	strFile = objFile.Name
	strNewFile = Left(strFile,InStrRev(strFile,".")) & "wav"
	strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
	If strExtension <> "wav" Then
		If strExtension <> "exe" Then
			WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & "" & TmpDir & strFile & "" & " " & "" & TmpDir & strNewFile & "",0,false
        		CommandText = "conv32 /it:rhet32 /ot:wave8 " & TmpDir & strFile & " " & TmpDir & strNewFile
        		WScript.Echo CommandText
    		End If
	End If
Next

Thanks again!
 
do a keyword search in this forum for recursive
 
thank you PHV, it took me a couple of hours but I finally got the recursive code to work and here it is in all its glory... but it has one problem I can't bang my head around...
Code:
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder= "C:\soundtest\"
WshShell.CurrentDirectory="c:\soundtest\ConvUtil"
Set objFolder = objFSO.GetFolder(strFolder)

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)
Set colFolders = objFolder.SubFolders

For Each objSubFolders In colFolders

	Set subobjFile = objSubFolders.Files
	For Each objFiles in subobjFile
		strFile = objFiles.Name
		StrPath = objFiles.Path
		strNewFile = Left(strPath,InStrRev(strPath,".")) & "wav"
		strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
		If strExtension <> "wav" Then
			If strExtension <> "exe" Then
				WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & "" & strPath & "" & " " & "" & strNewFile & "",0,false
        			CommandText = "conv32 /it:rhet32 /ot:wave8 " & strPath & " " & strNewFile
        			WScript.Echo CommandText
    			End If
		End If
	Next
Next
End sub

The problem is it's converting exe's and its not supposed to be... any ideas?
 
I'd try something like this:
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder= "C:\soundtest\"
WshShell.CurrentDirectory="c:\soundtest\ConvUtil"
Set objFolder = objFSO.GetFolder(strFolder)
ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)
For Each objFiles in objFolder.Files
strPath = objFiles.Path
strExtension = LCaseMid(strPath,1+InStrRev(strPath,".")))
If strExtension <> "wav" And strExtension <> "exe" Then
strNewFile = Left(strPath,InStrRev(strPath,".")) & "wav"
WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & strPath & " " & strNewFile,0,False
End If
Next
For Each objSubFolders In objFolder.SubFolders
ScanSubFolders(objSubFolders)
Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
OOps, sorry for the typo:
I'd try something like this:
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder= "C:\soundtest\"
WshShell.CurrentDirectory="c:\soundtest\ConvUtil"
Set objFolder = objFSO.GetFolder(strFolder)
ScanSubFolders objFolder

Sub ScanSubFolders(objFolder)
For Each objFiles in objFolder.Files
strPath = objFiles.Path
strExtension = LCase(Mid(strPath,1+InStrRev(strPath,".")))
If strExtension <> "wav" And strExtension <> "exe" Then
strNewFile = Left(strPath,InStrRev(strPath,".")) & "wav"
WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & strPath & " " & strNewFile,0,False
End If
Next
For Each objSubFolders In objFolder.SubFolders
ScanSubFolders objSubFolders
Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The file object has a Type property which might be an easier way to determine the file extension... it sorts depends... registered file types will come back with the friendly name (ie: txt comes back as "Text Document" or something)
 
OK, so far so good... everything is working peachy. To bypass the filetype, I simply moved the conv32 util to its own folder outside \soundtest\ and did the same for the scripts.

The last thing I'm trying to do now is copy the new wav to a secondary subdir. Basically, once the wav file is created, the script should make a copy of it and move it to the \soundtest\qa folder for our folks to listen to later on. As you will see in the script, the destination folder of the copy has been set to \soundtest\qa plus whatever the original subdir structure was for the original file. So, s12345.wav might be copied to \soundtest\qa\california\q1.

I thought doing this should be a piece of cake but I was very very wrong, lol.

Code:
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder= "C:\soundtest\"
WshShell.CurrentDirectory="c:\ConvUtil"
Set objFolder = objFSO.GetFolder(strFolder)

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)
Set colFolders = objFolder.SubFolders

For Each objSubFolders In colFolders

	Set subobjFile = objSubFolders.Files
	For Each objFiles in subobjFile
		strFile = objFiles.Name
		StrPath = objFiles.Path
		strNewFile = Left(strPath,InStrRev(strPath,".")) & "wav"
		strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
        	If strExtension <> "wav" Then
                	WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & "" & strPath & "" & " " & "" & strNewFile & "",0,false
                       	newpath = "C:\soundtest\qa\" & Right(objSubFolders,len(objSubFolders)-13)
			If objFSO.FolderExists(newpath) = False Then
         			objFSO.CreateFolder(newpath)
			End If
			objFSO.copyfile strNewFile, newpath & "\",false
			'Wscript.echo newpath
		End If
    	Next
Next
End sub

The error message I get after it spins through and does the first copy correctly is :
Code:
Windows Script Host
Script: C:\scripts\Autoconv32QApush.vbs
Line: 26
Char: 4
Error: File not found
Code: 800A0035
Source: Micorosft VBScript runtime error

Any ideas on what's wrong?
 
well, I'm a persistant little bugger and came up with a solution that works brilliantly. And in the best of my fasions, I want to take it another step, lol.....

I'm going to keep plugging away at this so please don't think I'm a freeloader because I'm not at all. Just a beginner having a hard time digest though I'm starting to move along pretty good.

My new mission is to run this script against files that it hasn't been run against yet instead of all files every single time. This is of course to improve processing speed/efficiency because the production version of this is going to be running probably every 30 minutes on hundreds of files each time. I'm really not sure how this is possible, but I imagine some of you guys have ideas. I'd love to hear all about them, :).

Here is my new script:
Code:
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder= "C:\soundtest\"
WshShell.CurrentDirectory="c:\ConvUtil"
Set objFolder = objFSO.GetFolder(strFolder)

ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)
Set colFolders = objFolder.SubFolders

For Each objSubFolders In colFolders

	Set subobjFile = objSubFolders.Files
	For Each objFiles in subobjFile
		strFile = objFiles.Name
		StrPath = objFiles.Path
		strNewFile = Left(strPath,InStrRev(strPath,".")) & "wav"
		strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
        	If strExtension <> "wav" Then
                	WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & "" & strPath & "" & " " & "" & strNewFile & "",0,false
                    	newpath = "C:\soundtest\qa\" & Right(objSubFolders,len(objSubFolders)-13) & "\"
			wshshell.run ("xcopy " & "" & strNewFile & "" & " " & "" & newpath & "" & " /Y")
			End If
	Next
Next
End sub

Call KillProc("conv32.exe")

Sub KillProc(strNameOfAppToKill)

Dim objWMI
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Process")

   for each Process in objWMI
     If lcase(Process.Name) = lcase(strNameOfAppToKill) Then
         Process.Terminate
     End If
     
   next

End Sub

Call KillProc("xcopy.exe")

Sub KillProc(strNameOfAppToKill)

Dim objWMI
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Process")

   for each Process in objWMI
     If lcase(Process.Name) = lcase(strNameOfAppToKill) Then
         Process.Terminate
     End If
     
   next

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top