'---------------------------------------------------------------
'-- draceplace 05/23/2017---------------------------------------
'---No Warranties implied or Vbscript is dangerous
'---Renaming files removing Bang (!)
' If then else to prevent duplicate names
'
' objStartFolder
' strMoveToFolder
' ---------------------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'set up for write file
Const ForAppending = 8
Const ForWriting = 2
Public cntFileID
strComma = ","
cntFileID = 0
cntBang = 0
cntDupe = 0
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("H:\A2\Bang_Fix1.csv", ForWriting, True) 'Log file here<---'
'enumerate file props
objStartFolder = "H:\A2\F1\" 'Folder name here<----
'rename files to to here
strMoveToFolder = "H:\A2\F1\" 'Folder name here<-----
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Msg2 = "Moving Cancelled by User "
Msg1 = "Rename - Remove Bang! from Here: " & objStartFolder & vbCrLf _
& "To Here: " & strMoveToFolder & vbCrLf
strStopGo = MsgBox(Msg1, vbOKCancel, "Utility-Move Files")
If strStopGo = vbOK Then
Set objFolder = objFSO.GetFolder(objStartFolder)
'--------------Write the tab headers-must be insync with output---
objTextFile.WriteLine ("FileID" & strComma & "File_Name" & strComma & "NewFilePath-Name")
Set colFiles = objFolder.Files
For Each objFile In colFiles
cntFileID = cntFileID + 1
'strFileID = Right(String(4, "0") & CStr(cntFileID), 4)& "_"
strFileID = "_" & Right(String(4, "0") & CStr(cntFileID), 4)
strFullPath = objFSO.GetAbsolutePathName(objFile)
strFileNameA = objFile.Name
If InStr(StrFilenameA, "!") > 0 Then 'TEST for Bang Problem
cntBang = cntBang + 1
strFileNameB = Replace(strFileNameA, "!", "")
strNewFile = strMoveToFolder & strFileNameB
If objFSO.FileExists(strNewFile) Then ' add file count to file name
cntDupe = cntDupe + 1
strNoExtension = Left(strFileNameB, InStrRev(strFileNameB, ".") - 1)
strExtension = Right(strFileNameB, Len(strFileNameB) - InStrRev(strFileNameB, "."))
strNewFileName = strNoExtension & strFileID & "." & strExtension
strNewFile = strMoveToFolder & strNewFileName
objFSO.MoveFile (strFullPath), strNewFile
Else
objFSO.MoveFile (strFullPath), strNewFile
End If
'write the file info'
objTextFile.WriteLine (Right(strFileID,4) & strComma & strFileNameA & strComma _
& strNewFile)
End IF ' TEST for Bang bypass
Next
Else
MsgBox Msg2, vbOKOnly, "OST Utility-Move Files"
End If 'stop this now
objTextFile.Close
Wscript.Echo "Wow! It Finnished - " & Now() & vbCrLf & "Records: " _
& Right(String(6, "0") & CStr(cntFileID), 6) _
& vbCrLf & "Bang Fix: " & CntBang _
& vbCrLf & "Duplicate: " & cntDupe