March 01, 2021, 10:47:44 PM

Author Topic: [bb] RenameFile by _PJ_ [ 1+ years ago ]  (Read 571 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] RenameFile by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : RenameFile
Author : _PJ_
Posted : 1+ years ago

Description : 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.


Code :
Code: BlitzBasic
  1. Function RenameFile(Path$="",NewFile$="",Count_Renamed=False)
  2.         ;Count_Renamed should be incremented each call for batch processing
  3.         Local Consecutor$=""
  4.         If (Count_Renamed) Then Consecutor$=Str(Count_Renamed)
  5.         If (Right(path,1)=".") Then Return
  6.         If (Path$="") Then Path$=CurrentDir()
  7.         If (Right$(Path,1)="") Then path$=Left(path$,Len(path$)-1)
  8.         If (NewFile$="")
  9.                 If (FileType(Path$)=2)
  10.                         NewFile$="Renamed Folder"
  11.                         ExecuteRename(Path$,NewFile$+Consecutor$)
  12.                 Else
  13.                         If (FileType(Path$)=1)
  14.                                 NewFile$="Renamed File"
  15.                         Else
  16.                                 Return
  17.                         End If
  18.                 End If
  19.         End If 
  20.         Local Extension_separator=Instr(Path$,".")
  21.         Local Extension$=""
  22.         Local Path_Separators
  23.         Local LastPath=0
  24.         While (Path_separators)
  25.                 LastPath=Path_Separators
  26.                 Path_Separators=Instr(Path$,"",Path_Separators+1)
  27.         Wend
  28.         If ((Not(Instr(NewFile$,"."))) And (LastPath<Extension_Separator))Then Extension$="."+Right(Path,(Len(Path)-Extension_Separator))
  29.         Return ExecuteRename(Path$,NewFile$+Consecutor$+Extension$)
  30. End Function
  31. Function ExecuteRename%(Path$,NewFile$)
  32.         ExecFile(Chr(34)+"cmd"+Chr(34)+" /c RENAME "+Chr(34)+Path$+Chr(34)+" "+Chr(34)+NewFile$+Chr(34))
  33.         Local Path_Separators
  34.         Local LastPath=0
  35.         While (Path_separators)
  36.                 LastPath=Path_Separators
  37.                 Path_Separators=Instr(Path$,"",Path_Separators+1)
  38.         Wend
  39.         Local Parent$=Left$(Path,LastPath)+NewFile$
  40.         Return (FileType(Parent$))
  41. End Function


Comments :


Streaksy(Posted 1+ years ago)

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


_PJ_(Posted 1+ years ago)

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


Streaksy(Posted 1+ years ago)

 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_(Posted 1+ years ago)

 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?


Streaksy(Posted 1+ years ago)

 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_(Posted 1+ years ago)

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


Streaksy(Posted 1+ years ago)

 No bother


Nate the Great(Posted 1+ years ago)

 works like a charm on vista here


_PJ_(Posted 1+ years ago)

 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_(Posted 1+ years ago)

 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???


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal