Solved

Help with function in VBS

Posted on 2013-05-21
6
314 Views
Last Modified: 2013-05-22
The other problem I am having is that the last two functions don't seem to work together.  The last function works (the " " is replaced with "_".  The previous function where the "#" is to be replaced with "_" seems to be skipped over.  

I have attached the file.
0
Comment
Question by:tommy777
  • 3
  • 3
6 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39185426
Nothing is attached...

~bp
0
 

Author Comment

by:tommy777
ID: 39185430
attaching file
replace-b.vbs
0
 

Author Comment

by:tommy777
ID: 39185453
Actually, I think I corrected it:

Set objFSO = CreateObject("Scripting.FileSystemObject")
nCount = 0
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
set rootFolder = objFSO.GetFolder(currentDirectory)
renameFiles rootfolder
enumFolders rootFolder
wscript.echo nCount & " Files have been renamed."
function enumFolders(folder)
   for each sFolder in folder.subfolders
      renameFiles sFolder
      enumFolders sFolder
   next
end function
function renameFiles(nFolder)
   for each oFile in nFolder.files  
 if instr(oFile.name,"#") > 0 then
         newFileName = replace(oFile.name,"#","_")  
         newFilePath = left(oFile.path,len(oFile.path) - len(oFile.name)) & newFileName
         objFSO.MoveFile oFile.path, newFilePath
end if
next
for each pFile in nFolder.files  
if instr(pFile.name," ") > 0 then
         newFileName = replace(pFile.name," ","_")  
         newFilePath = left(pFile.path,len(pFile.path) - len(pFile.name)) & newFileName
         objFSO.MoveFile pFile.path, newFilePath
nCount = nCount+1      
end if
next
end function
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 39185562
Which I think could be simplified a bit to:

Set objFSO = CreateObject("Scripting.FileSystemObject")
currentDirectory = Left(WScript.ScriptFullName, (Len(WScript.ScriptFullName)) - (Len(WScript.ScriptName)))
Set rootFolder   = objFSO.GetFolder(currentDirectory)

nCount= 0
renameFiles rootfolder
enumFolders rootFolder
Wscript.Echo nCount & " Files have been renamed."

Function enumFolders(folder)
    renameFiles sFolder

    For Each sFolder in folder.subfolders
        enumFolders sFolder
    Next

End Function

Function renameFiles(nFolder)

    For Each oFile in nFolder.files

        If InStr(oFile.name, "#") > 0 Or InStr(oFile.name, " ") > 0 Then
            newFileName = Replace(Replace(oFile.name, "#", "_"), " ", "_")
            objFSO.MoveFile pFile.path, oFile.ParentFolder & "\" & newFileName
            nCount = nCount + 1
        End If

    Next

End Function

Open in new window

~bp
0
 

Author Comment

by:tommy777
ID: 39186147
Error message:

Line 21
Char 5

Object Required 'nFolder'
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39186334
Okay, a bit more cleaning u, this should get you there...

Set objFSO = CreateObject("Scripting.FileSystemObject")
strCurrentDirectory = Left(WScript.ScriptFullName, (Len(WScript.ScriptFullName)) - (Len(WScript.ScriptName)))
Set objRootFolder = objFSO.GetFolder(strCurrentDirectory)

intCount = 0
enumFolders objRootFolder
Wscript.Echo intCount & " Files have been renamed."

Function enumFolders(objFolder)
    renameFiles objFolder

    For Each objSubFolder in objFolder.Subfolders
        enumFolders objSubFolder
    Next

End Function

Function renameFiles(objFolder)

    For Each objFile in objFolder.Files

        If InStr(objFile.Name, "#") > 0 Or InStr(objFile.Name, " ") > 0 Then
            newFileName = Replace(Replace(objFile.Name, "#", "_"), " ", "_")
            objFSO.MoveFile objFile.Path, objFile.ParentFolder & "\" & newFileName
            intCount = intCount + 1
        End If

    Next

End Function

Open in new window

~bp
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 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

12 Experts available now in Live!

Get 1:1 Help Now