?
Solved

Help with function in VBS

Posted on 2013-05-21
6
Medium Priority
?
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 57

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 57

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 57

Accepted Solution

by:
Bill Prew earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
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…
Suggested Courses

649 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