Qbasic, Quickbasic/Move

Advertisement


Question
Hi there. I am new to programming and I've started with QBASIC. I was thinking if there was a way to move all files of a specific type from one directory to another directory. Say I want to move all PNG type files from C:/New Folder to D:/New Folder. Is there any way to do it ? Thanks a lot.

Answer
Hi Paul,

Before I help you out, QBasic is a good but also terrible beginning language.  It's good, in terms of it being simple, but very bad, in terms of it being incredibly out-dated and limited.  You may want to look into ruby, which is similar to QBasic in terms of a very-close-to-english reading, has good coding standards, is modern, and will run on any machine that you install the ruby interpretter on.  Don't get me wrong, QBasic was my first computer language, but that was a long time ago, and even then, QBasic was defunct and no longer a desired language to learn.

Now, to answer you question, in two parts.  If you don't have access to the QBasic help menu (some versions of QBasic on the internet do not include it), you can look at the QBasic online help pages.

QBasic doesn't have a copy file function, but you can read and write files pretty easily, so this is what a copy file function would look like:


DECLARE FUNCTION CopyFile%(souceFile AS  STRING, destinationFile AS STRING)

'' Change the values of c:\some\file.txt and c:\some\copieid-file.txt strings to whatever
'' BONUS: Use a variable, ie:
'' DIM source AS STRING
'' DIM dest AS STRING
'' INPUT "Source file:", source
'' INPUT "Destination file:", dest
'' CopyFile%(source, dest)
IF CopyFile%("c:\some\file.txt", "c:\some\copied-file.txt") != 0 THEN
 PRINT "Oops, there was a problem"
 END 1
END IF

FUNCTION CopyFile%(sourceFile AS STRING, destinationFile AS STRING)
 DIM sourceHandle AS INTEGER
 DIM destinationHandle AS INTEGER
 DIM fileChunk AS STRING

 sourceHandle = -1
 destinationHandle = -1

 ON ERROR GOTO copyFail

 '' FREEFILE finds the next available file handle that we can use
 sourceHandle = FREEFILE

 '' Open the source file for RANDOM (meaning it tries to get the most permissions for reading and writing possible),
 '' and assign the file handle
 OPEN sourceFile FOR RANDOM AS #sourceHandle

 '' Now that the other file handle has been assigned and used, we can grab the next file handle available
 destinationHandle = FREEFILE

 '' Open destination for OUTPUT.  This should create the file if it isn't already created, and potentially over-write the file if it does already exist
 OPEN destinationFile FOR OUTPUT AS #destinationHandle

 '' Read through the source file until we get to the EOF (End of file)
 WHILE NOT EOF(sourceHandle)
   '' Grab a chunk of data from the file
   INPUT #sourceHandle, fileChunk
   '' Write it to the destination of the file
   PRINT #destinationHandle, fileChunk
 WEND  

 '' Close our file handles so QBasic knows these handles are free again
 CLOSE #destinationHandle
 CLOSE #sourceHandle

 ' 'Return 0 (meaning there was no problems!)
 RETURN 0

 '' When there is an error...
 copyFail:
   DIM e AS INTEGER
   '' Copy the Error Number
   e = ERR

   '' Print out the error if it's a file not found issue
   IF e = 53 THEN '' 53 is File not found, see http://forum.qbasicnews.com/index.php?topic=13328.0 for details
     PRINT "Could not find file", sourceFile
   END IF

   '' Close up the file handles if they have been set (remember how I defaulted the variables to -1?)
   IF sourceHandle != -1 THEN CLOSE #sourceHandle
   IF destinationHandle != -1 THEN CLOSE #destinationHandle

   '' Return the error number if there was a problem
   RETURN e
END FUNCTION


Hope that helps!

-Alex

Qbasic, Quickbasic

All Answers


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2016 About.com. All rights reserved.