Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C++ Floppy to C: tool issues  (Read 3436 times)

0 Members and 1 Guest are viewing this topic.

nixie

    Topic Starter


    Beginner

    Thanked: 7
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
C++ Floppy to C: tool issues
« on: December 30, 2011, 10:25:35 AM »
I have a large amount of floppy disks that I used between 1988 and around 2000 when I finally got a CD Burner. I started manually copying them via xcopy all contents to my C: drive, but after getting through 25 disks in 1 box, I thought there has to be an easier method of passing my disks contents to C: drive.

So I started to write a program in C++ which I want it to create numeric directories on the C: drive at C:\Floppies\   and each time it is executed it first reads in a text file called lastfilename which holds a single integer of the last directory created and written to, so after I have copied disk #25 to the C:\Floppies\25\ it wrote the integer 25 to this text file. So the next time it runs it first loads in this value and increments it via ++ to the integer variable, so that it now creates a directory named 26 at C:\Floppies\26\ and then xcopy's the contents of A: to C:\Floppies\26\ via system("XCOPY A:\*.* c:\Floppies\26\*.* /y ");

Problem is that it does not like system("XCOPY A:\*.* c:\Floppies\26\*.* /y ");

Also I am having issues directly concatenating the 26 from the incremental variable into C:\Floppies\26\*.* /y, and C++ does not like directly passing the string variable into system(" ");

In the end I want to clean this up and have it basically say its running, copy all contents to C:\Floppies\ and then display COPY COMPLETE in the end and ask the user to insert another disk and enter 1 to run again or 0 to quit.

Any suggestions? I will post my code if needed.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: C++ Floppy to C: tool issues
« Reply #1 on: December 30, 2011, 01:53:08 PM »
If you're going to execute system() calls, use a batch file. I've hardly ever seen a case where the main functionality of a so-called C/C++ program used system() and couldn't be made into a far more terse batch.

As to your issues: they are all based on not knowing enough about C/C++. Which probably explains why you would choose it for something like this.

Anyway, I tried to make a C/C++ app but apparently stream i/o won't let me input or output a char*, and I couldn't be bothered to figure out why.

So here's a C# program. haha.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace floppinc
{
    class Program
    {
       //equivalent to C's System() call. ugh.
        private static int System(String commandline)
        {
            String[] splitarg = commandline.Split(new char[]{' '},2);
           
            Process getprocess = Process.Start(splitarg[0],splitarg[1]);
            getprocess.WaitForExit();
            return getprocess.ExitCode;
        }
        /// <summary>
        /// returns integer equivalent of contents of file +1, or 1, if the file doesn't exist.
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        static int SequenceValue(String filename)
        {
            int intvalue =0;
            //open the file.
            if (File.Exists(filename))
            {
                FileStream fsr = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
                StreamReader sr = new StreamReader(fsr);
                //read the first line.
                String lineread = sr.ReadLine();
                //parse it to an integer.
                intvalue = lineread==null?1:int.Parse(lineread) + 1;
                fsr.Close();
            }
            else
            {
                intvalue = 1;
            }
            String strvalue = intvalue.ToString();
            //reopen for truncate.
            FileStream fsout = new FileStream(filename, FileMode.Create);
            StreamWriter sw = new StreamWriter(fsout);
            sw.Write(strvalue);
            sw.Close();
            return intvalue;
        }
        private static String ChoiceInput(String Prompt, String[] valid)
        {
            string currinput=null;
            bool isvalid=false;
            while (!isvalid)
            {
                Console.WriteLine(Prompt + "[" + String.Join("/", valid) + "]");
                currinput = Console.ReadLine();
                isvalid = valid.Any((w) => w.Equals(currinput, StringComparison.OrdinalIgnoreCase));
                if (!isvalid)
                {
                    Console.WriteLine("Invalid option-" + currinput + ".");
                }
            }
            return currinput;
        }
        private static readonly String sequencefile="D:\\sequence.dat";
        private static readonly String commandformat = "XCOPY A:\\*.* C:\\Floppies\\{0}\\*.* /S /Y";
        static void Main(string[] args)
        {
            bool exitloop=false;
            while (!exitloop)
            {
                int sequenceval = SequenceValue(sequencefile);
                String execcmd = String.Format(commandformat, sequenceval);
                System(execcmd);
                exitloop = (ChoiceInput("Copy Another?", new string[] { "Y", "N" }).Equals("N",StringComparison.OrdinalIgnoreCase));
            }
        }
    }
}

I rather like the ChoiceInput() function, myself.

This would be a very short batch file, though, I just don't like messing with batch files.

EDIT: fixed line endings. That's what happens when you copy text files from windows to Linux...
« Last Edit: December 30, 2011, 02:07:31 PM by BC_Programmer »
I was trying to dereference Null Pointers before it was cool.

nixie

    Topic Starter


    Beginner

    Thanked: 7
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: C++ Floppy to C: tool issues
« Reply #2 on: January 01, 2012, 09:22:19 PM »
Thanks BC... will use your C# program. Should be able to compile it with Express 2008 or 2010 and run it. I have a book on C#, but haven't gotten into it yet other than making your own browser by typing code out of book and then modifying the appearance, but it looks more like a custom IE vs whole new browser.

I was thinking originally batch, but figured C++ or another language (such as C# as you posted) would be better at reading/writing from file and incrementing the folder name 1,2,3,4,5 and so on. And then pick up with 7,8,9,10 on future executions from the original executed location to avoid data merging or overwriting.

Now to compile and try your program out as well as check out your source code in detail. Might have some questions about it since I am new to C#. Many thanks and Happy New Year!