Author |
Topic: Renaming files (Read 609 times) |
|
Orpheus2000
New Member
member is offline


Posts: 3
|
 |
Renaming files
« Thread started on: Sep 25th, 2011, 10:09am » |
|
I want to rename batches of files in a folder, e.g. all file names beginning with XYZ to be preceded with a number, so that the files will be renamed 001 XYZ...., 002 XYZ...., etc.
I cannot find how to do this, but I'm sure it's possible.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Renaming files
« Reply #1 on: Sep 25th, 2011, 1:31pm » |
|
on Sep 25th, 2011, 10:09am, Orpheus2000 wrote:I want to rename batches of files in a folder, e.g. all file names beginning with XYZ to be preceded with a number, so that the files will be renamed 001 XYZ...., 002 XYZ...., etc. |
|
Loop through each of the files in turn, renaming them as you go. For example you could adapt the routine PROCcopyfiles() which is listed in the Help documentation under 'Operating System interface... *COPY':
http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin8.html#copy
Then you only have to change the line which copies the file to rename the file instead, for example:
Code: SYS "MoveFile", src$+$fpt%, dst$+$fpt% TO res%
IF res% = 0 PRINT "File rename failed" To achieve your 'incrementing serial number' you would need to use STR$ to convert the number to a string:
Code: serial% += 1
serial$ = RIGHT$("000"+STR$(serial%),3)
SYS "MoveFile", src$+$fpt%, dst$+serial$+$fpt% TO res%
IF res% = 0 PRINT "File rename failed" Richard.
|
|
Logged
|
|
|
|
Orpheus2000
New Member
member is offline


Posts: 3
|
 |
Re: Renaming files
« Reply #2 on: Sep 28th, 2011, 10:43am » |
|
Thank you for your reply, Richard. As it happened I managed to work out how to do it soon after writing. Here is my solution. Not elegant, but it did the job.
Regards
Alan
REM Prefix batches of files with number series REM e.g. All filenames beginning with "Photograph" to become REM "0041 Photograph......." REM "0042 Photograph.......", etc. filestart$=" " REM initialise string holding *CHDIR E:\PHOTOTEST REM files are held here WHILE LEN(filestart$) < 5 REM 5 characters are required for uniqueness INPUT "First 5 or more characters of file name: ", filestart$ ENDWHILE N% = LEN(filestart$) INPUT "Prefix: ",prefix% prefix% = prefix% + 10000 REM preserve leading zeroes DIM dir% 1066 SYS "FindFirstFile", "*", dir% TO sh% IF sh% <> -1 THEN REPEAT oldfilename$ = $$(dir%+44) IF LEFT$(oldfilename$,N%) = filestart$ THEN REM Create new name, losing leading "1" newfilename$ = RIGHT$(STR$(prefix%),4)+" "+oldfilename$ SYS "MoveFile",oldfilename$,newfilename$ REM PRINT $$(dir%+44) prefix% = prefix% + 1 ENDIF SYS "FindNextFile", sh%, dir% TO res% UNTIL res% = 0 SYS "FindClose", sh%
|
|
Logged
|
|
|
|
KenDown
Full Member
member is offline


Posts: 181
|
 |
Re: Renaming files
« Reply #3 on: Nov 10th, 2011, 09:20am » |
|
The main problem you are going to face is the limit in OSCLI of 256 characters. If the filename (including the path name) is longer than about 123 then the command
OSCLI("RENAME "+old$+" "+new$)
is going to exceed the 256 character limit and you will get an error message. It's very frustrating.
I've bodged it by having two RENAMEs
OSCLI("RENAME "+old$+" D:temp."+type$) OSCLI("RENAME D:temp."+type$+" "+new$)
You'll have to find type$, but that is simply RIGHT$(old$,3)
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Renaming files
« Reply #4 on: Nov 10th, 2011, 10:42am » |
|
on Nov 10th, 2011, 09:20am, KenDown wrote:I've bodged it by having two RENAMEs
OSCLI("RENAME "+old$+" D:temp."+type$) OSCLI("RENAME D:temp."+type$+" "+new$)
You'll have to find type$, but that is simply RIGHT$(old$,3) |
|
Surely type$ can be anything (e.g. tmp), but a much simpler solution is to use the Windows API:
Code:SYS "MoveFile", old$, new$ I almost invariably use that in preference to OSCLI "RENAME".
Richard.
|
|
Logged
|
|
|
|
Orpheus2000
New Member
member is offline


Posts: 3
|
 |
Re: Renaming files
« Reply #5 on: Nov 10th, 2011, 2:37pm » |
|
[quote]You'll have to find type$, but that is simply RIGHT$(old$,3) [quote]
This isn't always true, e.g. .docx, .html
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Renaming files
« Reply #6 on: Nov 10th, 2011, 4:18pm » |
|
on Nov 10th, 2011, 2:37pm, Orpheus2000 wrote: Quote:You'll have to find type$, but that is simply RIGHT$(old$,3) |
|
This isn't always true, e.g. .docx, .html |
|
It will still work though. Since type$ can be anything, if it's ocx instead of docx, or tml instead of html, it won't make any difference. However it's obviously easier to set it to something constant like tmp than to bother with deriving it from the original filename. After all, the temporary file only exists for a very short time.
But using SYS "MoveFile" avoids the use of a temporary file altogether.
Richard.
|
|
Logged
|
|
|
|
|