Author Topic: File manipulation commands  (Read 3676 times)

0 Members and 1 Guest are viewing this topic.

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
File manipulation commands
« 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 :-)
Oh look - a sig file :-)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: File manipulation commands
« Reply #1 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
Re: File manipulation commands
« Reply #2 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 :-)
Oh look - a sig file :-)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: File manipulation commands
« Reply #3 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: File manipulation commands
« Reply #4 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.

« Last Edit: May 13, 2020, 02:30:46 pm by Petr »

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
Re: File manipulation commands
« Reply #5 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.   
Oh look - a sig file :-)

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: File manipulation commands
« Reply #6 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 :)


« Last Edit: May 13, 2020, 03:05:55 pm by Petr »

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
Re: File manipulation commands
« Reply #7 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.  
Oh look - a sig file :-)

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: File manipulation commands
« Reply #8 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 :)