Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: speedlemon on July 20, 2005, 09:28:51 PM



Title: How To Copy And Rename Files Using Free Basic
Post by: speedlemon on July 20, 2005, 09:28:51 PM
good evening ladies,
how can i copy a file to another directory and rename the exact same file. oh by the way, im working under the FB.


Title: How To Copy And Rename Files Using Free Basic
Post by: Sterling Christensen on July 21, 2005, 12:46:48 AM
Brilliant title! With only initials to work with, the phase "How To Copy And Rename Files Using Free Basic" somehow just sprang out at me - it's like telepathy!

Seriously though, I don't think it does a very good job of describing the topic when you have to read the message body to understand the title :roll:

I guess the simplest way would be to open both the source and destination, and then copy all the data. Or shell "copy blah blah2", if you don't care about portability.


Title: How To Copy And Rename Files Using Free Basic
Post by: speedlemon on July 21, 2005, 12:47:56 AM
i dont care about portability.
i tried using shell copy but it didnt work.
but id like to use that method.


Title: How To Copy And Rename Files Using Free Basic
Post by: Sterling Christensen on July 21, 2005, 12:55:27 AM
Sorry no offense intended, ok?

Here's some code:
Code:
const bufferSize = 1024
old$ = "\old_folder\old_name.blah"
new$ = "\new_folder\new_name.blah"

' If new$ exists, delete it:
if len(dir$(new$)) then kill new$

open old$ for binary access read as #1
open new$ for binary access write as #2

' Assuming FB's input$ works like QB's, and assuming
' FB's put has no chr$(0) trouble, this should work:
do
   buffer$ = input$(bufferSize, #1)
   put #2, , buffer$
loop until len(buffer$) < bufferSize

close #1, #2


EDIT: Method #2 2.0 - the "I forgot filenames can have spaces" release:
Code:
#ifdef __FB_LINUX__
const copyCommand = "cp"
#else
const copyCommand = "copy"
#endif

shell copyCommand + " " + chr$(34) + old$ + chr$(34) + " " + chr$(34) + new$ + chr$(34)


Title: How To Copy And Rename Files Using Free Basic
Post by: speedlemon on July 21, 2005, 12:57:21 AM
Quote from: "Sterling Christensen"
Sorry no offense intended, ok?

sorry for being rude :oops:


Title: How To Copy And Rename Files Using Free Basic
Post by: Sterling Christensen on July 21, 2005, 01:07:25 AM
It's ok.

I wish I could come up with something better. Both solutions I gave are ugly in their own ways. You'd think copying a file would be easier.


Title: How To Copy And Rename Files Using Free Basic
Post by: Dio on July 21, 2005, 02:58:10 AM
strange, i got:

How To Catch A Rubber Fish Using Fake Bait.

i guess we all got our own interpretations/problems....


Title: How To Copy And Rename Files Using Free Basic
Post by: Benedicto on July 21, 2005, 06:06:55 AM
just shell using the command MOVE, put the new name as the second parameter
Code:
SHELL "MOVE  NEW.TXT  SUBDIR\THING\OLD.TXT"


Title: How To Copy And Rename Files Using Free Basic
Post by: whitetiger0990 on July 21, 2005, 02:24:04 PM
Quote from: "Benedicto"
just shell using the command MOVE, put the new name as the second parameter
Code:
SHELL "MOVE  NEW.TXT  SUBDIR\THING\OLD.TXT"

In linux it's "mv" just so you know.


Title: How To Copy And Rename Files Using Free Basic
Post by: Z!re on July 21, 2005, 06:18:54 PM
Ey!

Speedlemon?

You back!? :D


Title: How To Copy And Rename Files Using Free Basic
Post by: arpgme on July 22, 2005, 08:59:07 PM
This doesn`t belong here.


Title: How To Copy And Rename Files Using Free Basic
Post by: Anonymous on July 22, 2005, 09:31:29 PM
you dont belong here. all in favor say AYE


Title: How To Copy And Rename Files Using Free Basic
Post by: whitetiger0990 on July 22, 2005, 09:31:41 PM
Quote from: "arpgme"
This doesn`t belong here.

*shrugs* meh the question was answered anyways =P


Title: How To Copy And Rename Files Using Free Basic
Post by: Sumo Jo on July 22, 2005, 09:34:08 PM
Quote from: "arpgme"
This doesn`t belong here.


This forum although being intended for qb questions doesn't specifically say it.  

Also, with as many rules as your posts have been breaking, who are you to become a backseat mod?  Please leave the worries of where things  go to the moderators/admin.


Title: How To Copy And Rename Files Using Free Basic
Post by: Z!re on July 23, 2005, 06:42:10 AM
arpgme, you just got SERVED!.. BOOOH YA!




*Ya, i know he's banned.. i just feelt like saying booh ya


Title: How To Copy And Rename Files Using Free Basic
Post by: rpgfan3233 on July 23, 2005, 11:00:11 AM
Quote from: "Z!re"
arpgme, you just got SERVED!.. BOOOH YA!




*Ya, i know he's banned.. i just feelt like saying booh ya

Who are you and what have you done with my Z!re?


Title: How To Copy And Rename Files Using Free Basic
Post by: Z!re on July 23, 2005, 11:15:16 AM
:???:


Title: How To Copy And Rename Files Using Free Basic
Post by: dumbledore on July 23, 2005, 03:57:47 PM
Quote
Who are you and what have you done with my Z!re?


...you have a z!re? :P


Title: How To Copy And Rename Files Using Free Basic
Post by: whitetiger0990 on July 23, 2005, 06:32:16 PM
well... I do =P


Title: How To Copy And Rename Files Using Free Basic
Post by: rpgfan3233 on July 23, 2005, 06:57:25 PM
Yeah, I have a Z!re and a whitetiger0990 and even a didjeridoo so they can play together!


Title: How To Copy And Rename Files Using Free Basic
Post by: Z!re on July 23, 2005, 09:13:32 PM
I suddenly feel very... objectified..


Title: How To Copy And Rename Files Using Free Basic
Post by: Rattrapmax6 on July 23, 2005, 09:57:00 PM
I saw a hand held computer called "zire" at Radio Shack... For a second, I thought I was in a 3D forum..  :o .. But then I noticed, it was spelled Z-I-R-E, not Z-!-R-E...  :P


Title: How To Copy And Rename Files Using Free Basic
Post by: DrV on July 23, 2005, 11:44:28 PM
(http://images.google.com/images?q=tbn:lfZnyk7fFb8J:www.brighthand.com/images/zire_front.jpg)Palm Zire


Title: How To Copy And Rename Files Using Free Basic
Post by: Z!re on July 24, 2005, 06:55:24 AM
I'm so famous they named a Palm after me.. kind of old news though :P


Title: How To Copy And Rename Files Using Free Basic
Post by: Rattrapmax6 on July 24, 2005, 10:51:04 AM
Yeah.. I don't get out much.. just saw it last week or so..  :P


Title: How To Copy And Rename Files Using Free Basic
Post by: Moneo on July 24, 2005, 06:50:34 PM
Quote from: "Sterling Christensen"
.....Here's some code:
Code:
const bufferSize = 1024
old$ = "\old_folder\old_name.blah"
new$ = "\new_folder\new_name.blah"

' If new$ exists, delete it:
if len(dir$(new$)) then kill new$

open old$ for binary access read as #1
open new$ for binary access write as #2

' Assuming FB's input$ works like QB's, and assuming
' FB's put has no chr$(0) trouble, this should work:
do
   buffer$ = input$(bufferSize, #1)
   put #2, , buffer$
loop until len(buffer$) < bufferSize

close #1, #2


EDIT: Method #2 2.0 - the "I forgot filenames can have spaces" release:
Code:
#ifdef __FB_LINUX__
const copyCommand = "cp"
#else
const copyCommand = "copy"
#endif

shell copyCommand + " " + chr$(34) + old$ + chr$(34) + " " + chr$(34) + new$ + chr$(34)


I've had bad experiences copying files from within a QB program by reading the first file and writing to the second. Opening the files for binary helps most of the time.

But, if the file is foreign to you, that is, you didn't create it, you could run into the following problems:
1) Non-standard end-of-record delimiters. (ok if open binary)
2) chr$(0) versus chr$(255) problems.
3) No end of record delimiters at all, which could cause input buffer overflow. (ok if open binary)
4) A zero-length-file which will probably cause an error on input.

All of the above problems go away if you SHELL to a COPY or MOVE command.

Also, if the input file does not exist, the COPY or MOVE will flash an error and continue, which may be good or bad for you. If your program opens the non-existant file, it hangs up on the error.
*****


Title: How To Copy And Rename Files Using Free Basic
Post by: speedlemon on July 24, 2005, 10:54:58 PM
thanks benedicto for that solution.
i put this in the right section... wtf arpgme?


Title: How To Copy And Rename Files Using Free Basic
Post by: Z!re on July 25, 2005, 08:18:57 AM
Code:
open file1$ for binary as #1
ll = lof(1)
redim fbuff(ll-1) as ubyte
get #1, 1, fbuff()
close #1

kill file2$
open file2$ for binary as #1
put #1, 1, fbuff()
close #1
redim fbuff(0) as ubyte

Enjoy


Title: How To Copy And Rename Files Using Free Basic
Post by: speedlemon on July 25, 2005, 11:57:00 AM
"Enjoy"
thanks, i will.

by the way, fieldview is looking hot. 8)


Title: How To Copy And Rename Files Using Free Basic
Post by: Z!re on July 25, 2005, 12:21:22 PM
heh, thanks.. if we could only finish it so people could see how hot it really is :P


Title: How To Copy And Rename Files Using Free Basic
Post by: Moneo on July 26, 2005, 03:27:55 PM
Quote from: "Z!re"
Code:
open file1$ for binary as #1
ll = lof(1)
redim fbuff(ll-1) as ubyte
get #1, 1, fbuff()
close #1

kill file2$
open file2$ for binary as #1
put #1, 1, fbuff()
close #1
redim fbuff(0) as ubyte

Enjoy


Z!re,

Your routine is amazing when compiled and run with Freebasic!

Old problems in QB are handled correctly and just go away, like:
- When input file does not exist.
- When input file is a zero-length-file.
- Doing a KILL to a non-existent file.
- other subtleties.

Nice work!
*****