Solved

Help with function in VBS

Posted on 2013-05-21
6
315 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 53

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 53

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 53

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 94
Extract field headers and values between quotes 12 51
SSRS troubles 4 50
Powerpoint 2013: Change cell reference in excel link 3 12
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

770 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