QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Mad Axeman on May 12, 2020, 04:31:08 pm

Title: File manipulation commands
Post by: Mad Axeman on May 12, 2020, 04:31:08 pm
I've just been looking through the keyword list and surprised that there doesn't seem to be some basic file handling commands available. I want to copy some files from one directory to another. I know I can do a simple shell command but that would only work on Windows machines. Wouldn't it be so much nicer if there was a 'copy' keyword to copy a file from one directory to another or a 'move' keyword to move a file??
Just a thought for the next version - unless those keywords are there already and I just can't find them :-)
Title: Re: File manipulation commands
Post by: Pete on May 12, 2020, 05:19:18 pm
1) Open your original file for BINARY as file number #1.
2) Set len(a$) = LOF(1)
3) GET #1,,a$ ' Gets the entire file.
4) Copy it to a new file, by opening a new file for BINARY as #2
5) PUT #2,,a$
6) Close both files.

Pete
Title: Re: File manipulation commands
Post by: Mad Axeman on May 13, 2020, 02:00:50 am
Hi Pete

I realise you could create a little function to copy files, either the way you say or using shell, but don't you think QB64 should have some basic file handling commands? We've already got the directory commands from quickbasic so it would be nice to have matching file commands. Just 'copy frompath$ topath' and 'move frompath$ topath$' would be lovely :-)
Title: Re: File manipulation commands
Post by: Pete on May 13, 2020, 02:44:57 am
QB64 also preserved the QBasic NAME AS command. NAME AS allows users to change the name of a file, or move it to another drive/directory with the same or different name.

My hunch is that since there are several simple ways to do these tasks, and very limited development of the language going forward, that although I agree these commands would be nice additions to the language, they are probably not a priority. Now that stated, if you get your wish, thank Murphy and his law.

Pete
Title: Re: File manipulation commands
Post by: Petr on May 13, 2020, 02:29:17 pm
All what you need, QB64 contains.


Code: QB64: [Select]
  1. Copy "qb64.exe", "qb64backup.exe"
  2.  
  3.  
  4.  
  5. SUB Copy (fileA AS STRING, fileB AS STRING)
  6.     IF _FILEEXISTS(fileA) THEN
  7.  
  8.         IF _FILEEXISTS(fileB) THEN
  9.             PRINT fileB; " exists. Owerwrite? (Y/N)"
  10.             DO UNTIL i$ = "y" OR i$ = "n"
  11.                 i$ = LCASE$(INKEY$)
  12.             LOOP
  13.             IF i$ = "n" THEN EXIT SUB
  14.             KILL fileB$
  15.         END IF
  16.  
  17.         ffA = FREEFILE
  18.         OPEN fileA FOR BINARY AS ffA
  19.         ffB = FREEFILE
  20.         OPEN fileB FOR BINARY AS ffB
  21.  
  22.         ffbc$ = SPACE$(LOF(ffA))
  23.         GET ffA, , ffbc$
  24.         PUT ffB, , ffbc$
  25.  
  26.         CLOSE ffA
  27.         CLOSE ffB
  28.     ELSE
  29.         PRINT "Source file "; fileA; " not exists!"
  30.     END IF
  31.  
You can also in this easy way do a program to move files.

Title: Re: File manipulation commands
Post by: Mad Axeman on May 13, 2020, 02:40:55 pm
I had forgotten about 'NAME'. Pity you can't use it to make a copy of the file in a different directory while keeping the original. Looks like I'll have to resort to open, get, put, close etc. I need to copy large files from a USB stick onto the hard drive.   
Title: Re: File manipulation commands
Post by: Petr on May 13, 2020, 02:53:14 pm
You can use my COPY SUB. Just add corect paths there. If you need first create files list to copy, it is possible using

1) DIR to file                               (system command)
2) Windows libraries                    (thanks to Eoredson)
3) SteveMcNeill´s perfect library  (direntry.h)

https://www.qb64.org/forum/index.php?topic=321.msg2875#msg2875


And reply for MOVE SUB :)
Just add this row

Kill FileA$

to end in SUB COPY and rename it to MOVE :)


Title: Re: File manipulation commands
Post by: Mad Axeman on May 13, 2020, 03:08:18 pm
I'll be using a simplified version of your post. I know the files will exist and what the file names are so all I'll need is something like below but I'll simplify that even more for what I will be doing.

Code: QB64: [Select]
  1.  
  2.         OPEN fileA FOR BINARY AS xx
  3.         OPEN fileB FOR BINARY AS xy
  4.  
  5.         ffbc$ = SPACE$(LOF(xx))
  6.         GET xx, , ffbc$
  7.         PUT xy, , ffbc$
  8.  
  9.         CLOSE xx
  10.         CLOSE xy
  11.  
Title: Re: File manipulation commands
Post by: Petr on May 13, 2020, 03:24:22 pm
It is up to you. I write all similar things as SUB or FUNCTION modules. Sometimes it's useful and it's easy to add it to another program :)