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

Author Topic: Batch help...  (Read 6016 times)

0 Members and 1 Guest are viewing this topic.

foo_addict

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Other
    Batch help...
    « on: January 11, 2017, 07:34:40 AM »
    Hi there,

    Although I'm familiar with the basic concepts of batch and dos I'm struggling to write a batch file to do the following...

    There are various files on my floppy disk (A:) separated into two folders A:\A\ and A:\N\ (folder A contains files starting with letters A-M, folder N contains files starting with letters N-Z)

    These files are sometimes altered on drive E:, one at a time. Once altered, I want a batch to copy the file in use, then look on drive A: for the folder it already exists in and then replace that file.

    Previously, drive A: contained no folders and the batch I used before contained the following: (where .abc is the file system in use)

    e:
    copy *.abc a:


    What script do I need to add to search each folder and overwrite the existing file?

    I have simplified the task a little bit for this topic, which is why the process might seem a little too simple to really require a batch.

    I hope I have explained my problem satisfactorily and thanks in advance,
    Adam

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch help...
    « Reply #1 on: January 11, 2017, 09:14:45 AM »
    Is this a homework problem? It looks very much like something a teacher would give a student to work on.

    Its odd that your still using a Floppy Disk which is obsolete at A: for the read/write of data. Most have moved on to USB Flash media or just do it all locally on the Hard Drive or SSD.

    My experience with heavy read/write cycling with floppy disks is that you will wear them out, so I would suggest moving away from this and instead use a design that doesn't have physical wear to media. The other issue that floppy drives have is that the negative pressure inside the case as a result of the cooling fans blowing warm air out of them cause floppy drives to collect dust and lint and this gets between the floppy disk magnetic platter and the head of the drive and it will etch a ring into the surface of the floppy disk platter and you will lose data.

    Many years ago when Hard Drives were expensive and floppies were all we pretty much had to affordably save, backup, and move/migrate data from system to system it was common to do like your doing with file manipulation on the A: or B: drive ( floppy drive letters ). These days though its safer and way faster to do all this on a USB Flash Drive, Hard Drive, or SSD.

    Maybe your system is really old and your stuck with having to do it all on the A: drive with a very small 10MB hard drive etc.

    foo_addict

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Other
      Re: Batch help...
      « Reply #2 on: January 12, 2017, 02:02:23 AM »
      From your response I presume you don't know how to help with my problem? And whilst the reason for using floppy drives has nothing to do with the problem, I will humor you and explain.

      I work with foreign coin counting and sorting machines, they remain the best available machines but they still have floppy drives installed (even the newest models). I have converted one of these machines to use a USB drive instead (in order to avoid the problems you have described with floppys), however the USB stick must still be formatted to 1.44mb to comply with the system's mainboard. This had it's own inherent problems with numbers of files in a single folder - hence why the files had to be split up in into 2 folders. Each file represents a country and it's denominations, i.e. Spain, Russia, Malta etc etc...  These files are only a few KB in size, so storage size is not an issue.

      Believe me, if it were possible/cost efficient to convert all these machines to have SSD's I'd do it in a flash.

      I'm sure it's a simple solution, but I don't know the answer. I thought that was the point of this forum? Apologies if I'm wrong.

      Hopefully you can sleep better now you know.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Batch help...
      « Reply #3 on: January 12, 2017, 09:24:39 AM »
      Once altered, I want a batch to copy the file in use,file.
      How are we supposed to know what file is in use or has been altered to copy?

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Batch help...
      « Reply #4 on: January 12, 2017, 09:50:26 AM »
      Congrats...you're already winning friends here with your comments...
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: Batch help...
      « Reply #5 on: January 12, 2017, 01:30:34 PM »
      From your response I presume you don't know how to help with my problem? And whilst the reason for using floppy drives has nothing to do with the problem, I will humor you and explain.

      Quote
      I'm sure it's a simple solution, but I don't know the answer. I thought that was the point of this forum? Apologies if I'm wrong.

      Hopefully you can sleep better now you know.

      The "point of this forum" is that it is the embodiment of the willingness of unpaid volunteers sharing their knowledge and experience, in their own time. The answers on here are not generated by emotionless robots. Since they are human beings, it is pretty likely that they will view negatively remarks like yours above, and quite possibly decide that they don't wish to provide any assistance.

      foo_addict

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Other
        Re: Batch help...
        « Reply #6 on: January 13, 2017, 03:42:16 AM »
        The "point of this forum" is that it is the embodiment of the willingness of unpaid volunteers sharing their knowledge and experience, in their own time. The answers on here are not generated by emotionless robots. Since they are human beings, it is pretty likely that they will view negatively remarks like yours above, and quite possibly decide that they don't wish to provide any assistance.


        Apologies if my reply to DaveLembke has upset anyone, but I found his comments belittling and likening my problem to something someone in school should be able solve quite offensive too, especially as I start my original post explaining that I'm struggling and end it by saying I've simplified the problem. I know how forums work and I've helped many people in fields I'm proficient in elsewhere. I don't take the help for granted, but found his post unhelpful and quite irrelevant to my post/problem. Perhaps I've misunderstood his post...

        How are we supposed to know what file is in use or has been altered to copy?

        I realise now I've omitted some key details. Lets presume the file in use is USA.abc, previously I would simply enter DOS and type the batch name, we'll call it "Save". There is only one .abc file on drive E: at anytime hence the original batch just using the *.abc to locate and copy that file to drive A:. With no folders on drive A: it would simply overwrite the matching file.

        Now with the folders it does not know where the matching file is in order to overwrite. I was hoping there may be a command that might be able to read the file name found by the *.abc command and find the same file any folders on drive A: and then overwrite the matching file. Perhaps I'm asking to much? (I have no idea!).

        Might it be easier to do the following? The batch would be executed by - in the above case - typing "Save USA" and the batch starting like this:

        e:
        copy %1.abc a:

        Although obviously it will need the correct script to find %1.abc in the relevant folder. This is where I'm stuck. Is there a find or search command that will help?

        Thanks



        Salmon Trout

        • Guest
        Re: Batch help...
        « Reply #7 on: January 13, 2017, 05:50:34 AM »
        You committed two further cardinal sins, namely "simplifying" the task required (this never helps!) and leaving information out. Are you doing this operation in true MS-DOS or Windows NT family (post Windows 2000) command prompt?

        foo_addict

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Other
          Re: Batch help...
          « Reply #8 on: January 13, 2017, 10:57:38 AM »
          Are you doing this operation in true MS-DOS or Windows NT family (post Windows 2000) command prompt?


          True MS-DOS.

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: Batch help...
          « Reply #9 on: January 13, 2017, 03:08:13 PM »
          True MS-DOS.
          Sorry.  I don't have anything to test on.  I can't remember what features did not exist in MS-DOS compared to what cmd.exe can do today.

          Salmon Trout

          • Guest
          Re: Batch help...
          « Reply #10 on: January 13, 2017, 04:20:33 PM »
          Looks like we are being asked to do somebody's paid job for them, for free... keep a seriously legacy system running... and take insults and attitude...

          patio

          • Moderator


          • Genius
          • Maud' Dib
          • Thanked: 1769
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: Batch help...
          « Reply #11 on: January 13, 2017, 07:14:59 PM »
          Other than that...perfect...
          " Anyone who goes to a psychiatrist should have his head examined. "

          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: Batch help...
          « Reply #12 on: January 13, 2017, 11:33:26 PM »
          Assuming the "simplified" example isn't excessively simplified...

          My thinking would be to effectively go through each file, then if it exists in A:\A\, copy it to A:\A, and if it exists in A:\N, copy it to A:\N

          Under MS-DOS I think this requires two batch files. One to go through the actual files, and the second which accepts each file and copies it to the appropriate folder.

          There is, of course, a caveat here, in that it will copy and overwrite files regardless of changes. This is intended to be run while E:\ is the current directory.

          copydirs.bat:
          Code: [Select]
          for %%F in (*.*) do checkflp.bat %%F
          checkflp.bat:
          Code: [Select]
          for %%D in (A N) do if exist A:\%%D\%1 copy %1 A:\%%D\%1 /y

          regarding the caveat, it is possible to expand this to only copy files that were changed. This cannot readily be done with COPY or XCOPY; however, and it also adds in a different limitation.

          MS-DOS has a file comparison tool, FC, which can compare files. However, it does not present an errorlevel to test.

          However, the output of FC will differ, so it is actually possible to get an errorlevel if the files differ by piping the output of fc to find, since find will return an errorlevel. If we change checkflp.bat to, instead of copying directly, run yet another batch file to chain things off:

          Code: [Select]
          for %%D in (A N) do if exist A:\%%D\%1 copydiff.bat %1 A:\%%D\%1

          and then create copydiff.bat:

          Code: [Select]
          fc %1 %2 | find "***">NUL
          IF ERRORLEVEL 1 COPY %1 %2 /Y

          FIND will give an errorlevel of 1 if it finds the specified text, so we use that to search the output of FC, which will only have *** if it found matches (otherwise it just says "FC:No differences encountered"). Then we can check said errorlevel to determine whether a file should be copied.

          The limitation here is that it means that the Floppy Drive must have each f ile to be copied; if E:\ receives new files, those will NOT be copied. This also presumes these files are all in the root directory (Again, could be the downfall of the simplification, as others have pointed out).

          I've not tested the above, either; so take appropriate precautions if testing/checking it!
          I was trying to dereference Null Pointers before it was cool.

          DaveLembke



            Sage
          • Thanked: 662
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: Batch help...
          « Reply #13 on: January 15, 2017, 07:22:26 AM »
          Quote
          Apologies if my reply to DaveLembke has upset anyone, but I found his comments belittling and likening my problem to something someone in school should be able solve quite offensive too, especially as I start my original post explaining that I'm struggling and end it by saying I've simplified the problem. I know how forums work and I've helped many people in fields I'm proficient in elsewhere. I don't take the help for granted, but found his post unhelpful and quite irrelevant to my post/problem. Perhaps I've misunderstood his post...

          Been watching this thread in silence ... but saw this today and thank you for apology as well as I apologize and will explain myself as to why I said what i said.

          In what i said it was never intended to bash you in any way. What I saw with read/writes to a floppy disk when you had access to a drive E: to me was like... why are you even using drive A: for your Read/Write process. If anything read in information off of A: bring it to E: and then run it through whatever processing at E: and then and end result maybe written back to A:.  Working with A: and E:  ( whats going on in C: and D: came to mind ... whats up with this E: is it a local drive or a mapped network drive etc. ) Instead of flooding you with lots of questions, I gave you just some to try to figure out what you were doing. Floppy disks are obsolete and so i also had to mention that.

          So I then touched on why an alternate means of data processing should be used vs heavy Read/Write cycling of an floppy disk at A:

          Secondly, the way that you simplified this to the point that it looked like something that we get from students looking for us to do their homework, that is why i asked if this was a homework problem. Depending on what it is we will generally help out some or give a complete solution, but sometimes not do all work for them because the best way to learn is to be pointed in the right direction and then figure some things out for yourself at times.

          Thirdly, you are not always going to get the solution to your problem in the first post from anyone. Please be open to posts such as mine that ask questions. And there was reasons why the questions were asked without providing a solution to you. You left out lots of information and I needed to know what direction we are going with this one and why that floppy disk needed to be a heavy read/write target when to me it seemed very odd.

          You are new to Computer Hope and just need to get a feel for this place and how we are, just know that none of us here ever intend to be belittling. I myself have been here for over 10 years and helped many people as well as gotten help myself from others here. Lots of smart people, and lots of great people. Your post fit the profile of other posts we have gotten over the years in the past from students looking for us to do their homework. So I needed to question it. The heavy read/write cycling on a floppy disk looked to me like something that a student would do because those with more experience would have read/write cycles on a media form ( non floppy disk ) that is better suited for that. Knowing that E: was available yet A: was being so heavily used with E: only (sometimes used) to me looked like a weak design. Stronger design being that contents from A: moved to E: and then processed at E: and then written back to A:

          Knowing now what you have for a situation we now can better assist.  :)

          foo_addict

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Other
            Re: Batch help...
            « Reply #14 on: January 16, 2017, 06:20:05 AM »
            Been watching this thread in silence ... but saw this today and thank you for apology as well as I apologize and will explain myself as to why I said what i said.

            Thanks for the follow up and apology DaveLembke. The trouble with forums are so much is lost in the way things are written. I can see that I have 'erkked' a few other people already, not my intention, but they continue to post. I'm not trying to get anyone to do my job for me, this is not my job. If it was, I'd hope I'd be at a point where I can at least have a good go myself. Just asking for help with a problem, surely that's all people on here ever ask for when they create a post such as mine.

            Assuming the "simplified" example isn't excessively simplified...

            My thinking would be to effectively go through each file, then if it exists in A:\A\, copy it to A:\A, and if it exists in A:\N, copy it to A:\N

            Under MS-DOS I think this requires two batch files. One to go through the actual files, and the second which accepts each file and copies it to the appropriate folder.

            There is, of course, a caveat here, in that it will copy and overwrite files regardless of changes. This is intended to be run while E:\ is the current directory.

            copydirs.bat:
            Code: [Select]
            for %%F in (*.*) do checkflp.bat %%F
            checkflp.bat:
            Code: [Select]
            for %%D in (A N) do if exist A:\%%D\%1 copy %1 A:\%%D\%1 /y

            regarding the caveat, it is possible to expand this to only copy files that were changed. This cannot readily be done with COPY or XCOPY; however, and it also adds in a different limitation.

            MS-DOS has a file comparison tool, FC, which can compare files. However, it does not present an errorlevel to test.

            However, the output of FC will differ, so it is actually possible to get an errorlevel if the files differ by piping the output of fc to find, since find will return an errorlevel. If we change checkflp.bat to, instead of copying directly, run yet another batch file to chain things off:

            Code: [Select]
            for %%D in (A N) do if exist A:\%%D\%1 copydiff.bat %1 A:\%%D\%1

            and then create copydiff.bat:

            Code: [Select]
            fc %1 %2 | find "***">NUL
            IF ERRORLEVEL 1 COPY %1 %2 /Y

            FIND will give an errorlevel of 1 if it finds the specified text, so we use that to search the output of FC, which will only have *** if it found matches (otherwise it just says "FC:No differences encountered"). Then we can check said errorlevel to determine whether a file should be copied.

            The limitation here is that it means that the Floppy Drive must have each f ile to be copied; if E:\ receives new files, those will NOT be copied. This also presumes these files are all in the root directory (Again, could be the downfall of the simplification, as others have pointed out).

            I've not tested the above, either; so take appropriate precautions if testing/checking it!


            Many thanks for your attempt at helping with my problem, much appreciated. I won't lie, a lot of it has gone over my head in regards to the code, but I think I understand what they are trying to do. I haven't had the chance to try it just yet but will hopefully get a chance in the next few days.

            Thanks again for your help.

            foo_addict

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Other
              Re: Batch help...
              « Reply #15 on: January 16, 2017, 07:00:04 AM »
              Ok so after reading the possible solution I had to have a go with it right now.

              The first code you posted BC_Programmer has allowed me to do exactly what I wanted. I have tweaked it a little to search for a specific file determined when the batch is run, because there are multiple files that exist on both A: and E: that are never changed and so would never need copying. Also, the /y was giving me an invalid switch error (maybe because of the changes I made?). I think this is something to do with a confirmation prompt(?), but I removed it and it worked without the need to confirm overwrite.

              The code I used:
              copydirs.bat
              Code: [Select]
              for %%F in (%1.abc) do checkflp.bat %%F
              checkflp.bat
              Code: [Select]
              for %%D in (A N) do if exist A:\%%D\%1 copy %1 A:\%%D\%1
              Many thanks again for your kind help.