Page 1 of 1
Rename Directories
Posted: Tue Jun 02, 2015 5:03 pm
by blueb
It appears as if RenameFile() works only on directories
and not on sub-directories.
I need to remove spaces from directories.
Any ideas?
Example:
Code: Select all
OriginalName$= "E:\fuji 550" ; <<< works
;OriginalName$= "E:\AA_CameraPics\AA Temporary_Photos\fuji 550" ; <<< fails even on first sub-level
Procedure.s NoSpacesDirectoryName(OriginalName$)
NoSpacesName$ = ReplaceString(OriginalName$, " ", "_")
Debug NoSpacesName$ + " <<< idea is to change folder name so it has no spaces"
x = RenameFile(OriginalName$, NoSpacesName$)
Debug "--------------------"
Debug "Did RenameFile() work?"
Debug Str(x) + " (success = 1)"
EndProcedure
; ================================
Debug "--------------------"
Debug OriginalName$ + " <<< original folder name"
NoSpacesDirectoryName(OriginalName$)
Re: Rename Directories
Posted: Tue Jun 02, 2015 5:19 pm
by ts-soft
Make a recursive search, many source in this forum, and add the directories to a list. Then rename all directories.
Re: Rename Directories
Posted: Tue Jun 02, 2015 6:32 pm
by Thade
Code: Select all
;OriginalName.s= "E:\fuji 550" ; <<< works
OriginalName.s= "D:\AA_CameraPics\AA Temporary_Photos\fuji 550" ; <<< fails even on first sub-level
Procedure NoSpacesDirectoryName(OriginalName.s)
NoSpacesName.s = ReplaceString(OriginalName.s, " ", "_")
Debug NoSpacesName.s + " <<< idea is to change folder name so it has no spaces"
Iter=CountString(OriginalName.s, "\")+1
Debug Iter
Temp.s=StringField(OriginalName.s, 1, "\")
For i=2 To Iter
Temp.s+"\"+StringField(OriginalName.s, i, "\")
Debug Temp.s
NoSpacesName.s=ReplaceString(Temp.s, " ", "_")
Debug NoSpacesName.s
If Temp.s<>NoSpacesName.s
Debug "renaming "+Temp.s+" ---> "+NoSpacesName.s
RenameFile(Chr(34)+Temp.s+Chr(34), NoSpacesName.s)
;RenameFile(Temp.s, NoSpacesName.s)
EndIf
Temp.s=ReplaceString(Temp.s, " ", "_")
Debug Temp.s
Debug "----"
Next
ProcedureReturn FileSize(NoSpacesName.s)
EndProcedure
; ================================
Debug "--------------------"
Debug OriginalName.s + " <<< original folder name"
Debug FileSize(OriginalName.s)
Debug NoSpacesDirectoryName(OriginalName.s)
Debug "Should be -2"
This should (actually must) work - but it doesn't - Bug?
Re: Rename Directories
Posted: Tue Jun 02, 2015 6:49 pm
by kenmo
Remove the Chr(34)'s and it works on my Windows 7 system...
Code: Select all
;RenameFile(Chr(34)+Temp.s+Chr(34), NoSpacesName.s)
RenameFile(Temp.s, NoSpacesName.s)
When you are typing paths with spaces into (for example) the command terminal, then yes you must enclose it in quotes.
But when you are passing a path string into a function, it doesn't require quotes (at least most PB functions don't).
EDIT: Here's another version:
Code: Select all
Procedure.i RenameWithoutSpaces(Path.s)
If FileSize(Path) <> -1 ; Works with folders OR files
Result = #True
While (FindString(Path, " "))
Path = RTrim(Path, "\")
If (Not RenameFile(Path, GetPathPart(Path) + "\" + ReplaceString(GetFilePart(Path), " ", "_")))
Result = #False
Break
EndIf
Path = GetPathPart(Path)
Wend
EndIf
ProcedureReturn Result
EndProcedure
Debug RenameWithoutSpaces("C:\my folder\a subfolder\")
Debug RenameWithoutSpaces("C:\my folder 2\a subfolder\New Text Document.txt")
Re: Rename Directories
Posted: Tue Jun 02, 2015 7:41 pm
by Thade
kenmo wrote:Remove the Chr(34)'s and it works on my Windows 7 system...
On my Windows 7 both (with or without Chr(34)) it is not working.
But your Program does ... clever use of a workaround as it seems ... well done
Re: Rename Directories
Posted: Wed Jun 03, 2015 11:48 am
by DK_PETER
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
Re: Rename Directories
Posted: Wed Jun 03, 2015 4:29 pm
by blueb
Thanks for all the help guys...
kenmo
Adding the ability to handle filenames with a space is a bonus... thanks.
Your solution appears to work... but I'll need to do some additional testing.
I've got a few small utilities that can't deal with spaces in the folder names, so this will help.
While I don't mind renaming my folders, I have to be able to reverse the process (leave the folder names as found), so I don't upset the user.
blueb