Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Wednesday, May 25, 2005

add extension to files under a folder

''''''''''''''''Append file extension name
''''''''''''''''Run in command line
''''''''''''''''cscript change.vbs D:\Images
''''''''''''''''Onega 20050525



'***************create array containing postfix of file names.
'***************Those files are not renamed
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
dict.Add dict.count , ".txt"
dict.Add dict.count , ".log"
dict.Add dict.count , ".vbs"
dict.Add dict.count , ".bat"
dict.Add dict.count , ".DCM"
On Error Resume Next
Dim fso
set fso = CreateObject("Scripting.FileSystemObject")
Dim file_changed
Dim failed_count
Dim total_file_count
total_file_count = 0
file_changed = 0
failed_count = 0
Dim ArgObj
Set ArgObj = WScript.Arguments
If ArgObj.Count>0 Then
object_folder = ArgObj(0)
'Else
' object_folder = GetScriptCurrentPathName()
End If

Set IFileSystem3 = CreateObject("Scripting.FileSystemObject")
set IFolder = IFileSystem3.GetFolder(object_folder)
clear_folder IFolder

If ArgObj.Count>0 Then
WScript.Echo "Total file count:" & CStr(total_file_count)
WScript.Echo CStr(file_changed) & " files are renamed"
WScript.Echo CStr(failed_count) & " files failed to be renamed"
WScript.Echo "Job done!"
Else
WScript.Echo "Usage: cscript change.vbs D:\Images"
End If
set fso = nothing
''''*******************End of Main Body


Function clear_folder( folder )
set IFileCollection = folder.Files
For each file in IFileCollection
total_file_count = total_file_count + 1
If not is_temp_file( file.Path ) Then
newfilename = file.Path & ".dcm"
wscript.echo newfilename
fso.MoveFile file.Path, newfilename
If Err <> 0 Then
'failed to rename
failed_count = failed_count + 1
else
file_changed = file_changed + 1
End If
End If
Next

For each sub_folder in folder.SubFolders
clear_folder sub_folder
Next
End Function



Function end_with( src_string, key)
end_with = false
If Len(key) <1 Then
end_with = false
Exit Function
End if
If Len(src_string) < Len(key) Then
end_with = false
Else
Dim temp_str
temp_str = right(src_string, Len(key) )
'vbTextCompare = 1
If StrComp( temp_str,key,vbTextCompare) = 0 Then
end_with = true
Else
end_with = false
End if
End if

End function

Function is_temp_file(path)
is_temp_file = false
For each postfix in dict.items
If end_with(path, postfix) Then
is_temp_file = true
Exit for
End if
next
End function


'********************* Get Dir containing current script file
Private Function GetScriptCurrentPathName
Dim fso1
set fso1 = CreateObject("Scripting.FileSystemObject")
GetScriptCurrentPathName = fso1.GetParentFolderName(WScript.ScriptFullName)
set fso1 = nothing
End Function

0 Comments:

Post a Comment

<< Home