Rename Directories

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Rename Directories

Post 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$)
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Rename Directories

Post by ts-soft »

Make a recursive search, many source in this forum, and add the directories to a list. Then rename all directories.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: Rename Directories

Post 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?
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: Rename Directories

Post 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")
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: Rename Directories

Post 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
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rename Directories

Post 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
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Rename Directories

Post 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
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Post Reply