blueb wrote:It appears as if RenameFile() works only on directories
and not on sub-directories.
I need to remove spaces from directories.
Any ideas?
Hey blueb
So..If I understand you correctly, you wish to rename numerous folders in one go...
Perhaps something like this will be useful:
Do some carefull testing before using it on something important.
I can't test it myself right now.
Code:
Code: Select all
;Use with care!
;And test...test...and TEST on useless folders first!!!!!
Declare.s Slash(Fname.s = "")
Declare.i ExamineDir(dirname.s, Pattern.s = "*")
Declare.i RenameDir(Dir.s, FindChars.s = "", ReplaceWith.s = "")
Structure _DirectoryData
OldFolder.s
newFolder.s
OldFullPath.s
NewFullPath.s
EndStructure
Global NewList dirs._DirectoryData()
Procedure.s Slash(Fname.s = "")
If Right(Fname,1) <> "\"
ProcedureReturn Fname + "\"
Else
ProcedureReturn Fname
EndIf
EndProcedure
;Full recurse
Procedure.i ExamineDir(dirname.s, Pattern.s = "*")
Protected dir.i, Filename.s
dir = ExamineDirectory(#PB_Any, dirname, Pattern)
If dir > 0
While NextDirectoryEntry(dir)
Filename = DirectoryEntryName(dir)
If DirectoryEntryType(dir) = #PB_DirectoryEntry_Directory And Filename <> "." And Filename <> ".."
AddElement(dirs())
dirs()\OldFolder = Filename
dirs()\OldFullPath = dirname
ExamineDir(Slash(dirname) + Filename)
EndIf
Wend
FinishDirectory(dir)
EndIf
ProcedureReturn #True
EndProcedure
Procedure.s IsolateMainFolder(fullPath.s, ReturnDrive.i = #False)
Protected NewFolder.s = "", count.i
If Right(fullPath,1)<> Slash() ; No Trailing slashes
count = CountString(Slash(fullPath), Slash())
Else ;Trailing slashes
count = CountString(fullPath, Slash())
EndIf
If ReturnDrive = #False
ProcedureReturn StringField(fullPath, Count, Slash())
Else ;Drive only
ProcedureReturn Slash(StringField(fullPath, 1, Slash()))
EndIf
EndProcedure
Procedure.i RenameDir(Dir.s, FindChars.s = "", ReplaceWith.s = "")
Protected ret.i,GetLastFolder.s, NewFolder.s
GetLastFolder = IsolateMainFolder(Dir, #False)
NewFolder = ReplaceString(GetLastFolder, FindChars, ReplaceWith)
ret = ExamineDir(dir, "*") ;Run through all folders
For p = ListSize(dirs())-1 To 0 Step -1
SelectElement(dirs(), p)
dirs()\newFolder = ReplaceString(dirs()\OldFolder, FindChars, ReplaceWith)
RenameFile(Slash(dirs()\OldFullPath) + dirs()\OldFolder,Slash(dirs()\OldFullPath) + dirs()\newFolder)
Next p
RenameFile(Dir, ReplaceString(Dir, GetLastFolder, NewFolder))
EndProcedure
RenameDir("Q:\This is test", " ", "") ;Recurse and remove all folder spacing