Code archives/File Utilities/RenameFile

This code has been declared by its author to be Public Domain code.

Download source code

RenameFile by _PJ_2010
Very simply, this is a means to Rename file/folders from within Blitz.

These functions hijack the 'pseudo-DOS' implemented in Windows XP. They should work with NT, Windows 2000 and earlier Windows versions, but I have no idea if the DOS command interface is included anymore with the likes of Vista and Windows 7

Also, since the fake DOS environment is based on virtual 16-bit architecture, I am not sure what, if any, effects there are in Windows 64-bit versions.
Best advice is see if Start-Run-cmd brings up a command prompt interface or not. :)

I added the ability to do some bulk renaming if required, this is purely optional, but requires tracking a value which is passed in to the function. This value should be incremented each call.

To ensure against possible errors and bad calls, I added some simple (a bit sloppy to be honest) checks for the file extension and such.

I sincerely suggest that this function is tested on some unimportant files/directories before putting it to real use. JUST IN CASE!

___________________

To use all the features, call the RenameFile(path$, NewFile$,Count_Renamed) function, parameters are:

Path: Complete path and filename to the file to be renamed
NewFile: The new name for the file (If you omit the extension, the same extension as the original will be added)
Count_Renamed: If required, This is the tracking variable for consecutive batch processing.

Otherwise, for more simplistic, direct conversions, you can directly call

ExecuteRename(Path$, NewFile$)

This function is the main renaming and imply changes the name of the file / folder that path$ points to, to NewFile$. If NewFile$ already exists, then nothing happens.

__________________________

Lastly:
A return value is added which corresponds to the filetype of the newly renamed file. This is just an extra.
It may be that this fails due to the time taken for the computer to actually rename the file.
Function RenameFile(Path$="",NewFile$="",Count_Renamed=False)
	;Count_Renamed should be incremented each call for batch processing
	Local Consecutor$=""
	If (Count_Renamed) Then Consecutor$=Str(Count_Renamed)
	If (Right(path,1)=".") Then Return
	If (Path$="") Then Path$=CurrentDir()
	If (Right$(Path,1)="\") Then path$=Left(path$,Len(path$)-1)
	If (NewFile$="")
		If (FileType(Path$)=2)
			NewFile$="Renamed Folder"
			ExecuteRename(Path$,NewFile$+Consecutor$)
		Else
			If (FileType(Path$)=1)
				NewFile$="Renamed File"
			Else
				Return
			End If
		End If
	End If	
	Local Extension_separator=Instr(Path$,".")
	Local Extension$=""
	Local Path_Separators
	Local LastPath=0
	While (Path_separators)
		LastPath=Path_Separators
		Path_Separators=Instr(Path$,"\",Path_Separators+1)
	Wend
	If ((Not(Instr(NewFile$,"."))) And (LastPath<Extension_Separator))Then Extension$="."+Right(Path,(Len(Path)-Extension_Separator))
	Return ExecuteRename(Path$,NewFile$+Consecutor$+Extension$)
End Function 
Function ExecuteRename%(Path$,NewFile$)
	ExecFile(Chr(34)+"cmd"+Chr(34)+" /c RENAME "+Chr(34)+Path$+Chr(34)+" "+Chr(34)+NewFile$+Chr(34))
	Local Path_Separators
	Local LastPath=0
	While (Path_separators)
		LastPath=Path_Separators
		Path_Separators=Instr(Path$,"\",Path_Separators+1)
	Wend
	Local Parent$=Left$(Path,LastPath)+NewFile$
	Return (FileType(Parent$))
End Function

Comments

Streaksy2010
Tried it in Vista... didnt seem to work ;/


_PJ_2010
Thanks for the feedback Streaksy, I assume that the Start - Run - "cmd" doesn't invoke a command prompt interface?


Streaksy2010
Vista has cmd... I don't have it in my start menu (forgot how) but I can invoke it with New Task from in task manager. I use it for IPCONFIG like everyone else ;P


_PJ_2010
Okay, I made a small change, but it shouldn't have been anything critical, the function should work provided Blitz can run the cmd.

Perhaps there's a permissions issue?

Could/did you try different paths, just in case?


Streaksy2010
Doesn't seem to work but I'm cursed with Vista and always get weird problems. I could by a brand new pc with a fresh vista and still get mad problems nobody's ever heard of.

Guess you need another Vista user to test it ;/


_PJ_2010
No wworries, Thanks for the info anyway, if I can find a solution for you, I'll be sure to post about it :)


Streaksy2010
No bother


Nate the Great2010
works like a charm on vista here


_PJ_2010
Noticed a small error in the code.

(Fixed above now)

Last_Path was also written as LastPath in some cases. Might have been an issue in some cases.


_PJ_2010
Does the new Powershell thingy that Miscrosoft released with Windows Update have any significance here?

From what I see, it seems to be some kinda replkacement for the cmd shell?

I'm on XP so presumably it's oinly included to help compaitibilities with Win7 etc???


Code Archives Forum