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

Author Topic: Writing a Batch File to Copy and Open File on CD  (Read 15152 times)

0 Members and 1 Guest are viewing this topic.

trueparadox

  • Guest
Writing a Batch File to Copy and Open File on CD
« on: April 13, 2010, 10:29:45 AM »
Hello,

I'm working on making a spreadsheet automatically copy to the "My Documents" folder from a CD and open up from that location immediately after copying. Does anyone have any suggestions?

This is what I was able to make so far after some great help from someone. This is for the copy part of it.

@echo off
echo Copying Files from CD...
echo Please Wait...
::variables
set cdcopy=xcopy /s /c /e /h /i /r /y /d
set dest=%userprofile%\My Documents
set files=filename.ods


%cdcopy% "D:\%files%" "%dest%\"
%cdcopy% "E:\%files%" "%dest%\"
%cdcopy% "F:\%files%" "%dest%\"
%cdcopy% "G:\%files%" "%dest%\"
%cdcopy% "H:\%files%" "%dest%\"
%cdcopy% "I:\%files%" "%dest%\"
%cdcopy% "J:\%files%" "%dest%\"
%cdcopy% "K:\%files%" "%dest%\"
%cdcopy% "L:\%files%" "%dest%\"
%cdcopy% "M:\%files%" "%dest%\"
%cdcopy% "N:\%files%" "%dest%\"
%cdcopy% "O:\%files%" "%dest%\"
%cdcopy% "P:\%files%" "%dest%\"
%cdcopy% "Q:\%files%" "%dest%\"
%cdcopy% "R:\%files%" "%dest%\"
%cdcopy% "S:\%files%" "%dest%\"
%cdcopy% "T:\%files%" "%dest%\"
%cdcopy% "U:\%files%" "%dest%\"
%cdcopy% "V:\%files%" "%dest%\"
%cdcopy% "W:\%files%" "%dest%\"
%cdcopy% "X:\%files%" "%dest%\"
%cdcopy% "Y:\%files%" "%dest%\"
%cdcopy% "Z:\%files%" "%dest%\"

cls
echo Copy Complete...
Exit


I've started using this and this is is my problem.

It copies the file but afterward's displays this alert.
"There is no disk in the drive. Please insert a disk into drive \Device\Harddisk1\DR1,"
It then repeats it with "Harddisk2\DR2, Harddisk 3\DR3, and Harddisk 4\DR4"

Any suggestions?
 :-\


T.C.



    Beginner

    Thanked: 13
    Re: Writing a Batch File to Copy and Open File on CD
    « Reply #1 on: April 16, 2010, 02:53:35 AM »
    Quote
    It copies the file but afterward's displays this alert

    I'm intrigued.  You appear to be trying to Xcopy Filename.ods from 23 different sources (drives D: thru' Z:), is that a feature of Win 7, with which I'm unfamiliar, or XCopy? 

    Salmon Trout

    • Guest
    Re: Writing a Batch File to Copy and Open File on CD
    « Reply #2 on: April 18, 2010, 01:36:46 AM »
    I'm intrigued.  You appear to be trying to Xcopy Filename.ods from 23 different sources (drives D: thru' Z:), is that a feature of Win 7, with which I'm unfamiliar, or XCopy? 

    No; it is a feature of a script prepared by somebody who does not know how to find out which drive letter the CD drive is using, except by trying all the letters of the alphabet! This approach leads to the problem described.





    T.C.



      Beginner

      Thanked: 13
      Re: Writing a Batch File to Copy and Open File on CD
      « Reply #3 on: April 18, 2010, 03:24:33 AM »
      Thank you S.T., I had guessed that was so.  I'm surprised you didn't render a lesson on the use(s) of Fsutil, a lot of us could learn from your input.

      I'll start with this, perhaps you will add to it, or show more sophisticated coding, which could be applied to the OP's original request or is specific to Win 7:

      Code: [Select]
      @echo off
      cls

      for %%1 in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
          fsutil fsinfo drivetype %%1: | find /i /v "no such"
      )





      Salmon Trout

      • Guest
      Re: Writing a Batch File to Copy and Open File on CD
      « Reply #4 on: April 18, 2010, 04:10:39 AM »
      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion

      @echo off
      echo Copying Files from CD...
      echo Please Wait...
      ::variables
      set cdcopy=xcopy /s /c /e /h /i /r /y /d
      set dest=%userprofile%\My Documents
      set files=filename.ods

      for /f "delims=" %%D in ('fsutil fsinfo drives') do (
      set DriveString=%%D
      set DriveString=!DriveString:~8!
      for %%L in (!DriveString!) do (
      fsutil fsinfo drivetype %%L | find "CD-ROM">nul && (
      if exist "%%L%files%" (
      %cdcopy% "%%L%files%" "%dest%\"
      )
      )
      )

      T.C.



        Beginner

        Thanked: 13
        Re: Writing a Batch File to Copy and Open File on CD
        « Reply #5 on: April 18, 2010, 05:33:17 AM »
        Thanks for that S.T.

        On behalf of the OP, who seems to have vanished, I must advise I can't get your script to run.  In the For loop Fsutil Fsinfo Drives appears to return only Drives: A:\ not the string displayed when that combo is entered at the command prompt.

        I post this amended extract from your script to show what I mean:
        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion
        cls

        for /f "delims=" %%D in ('fsutil fsinfo drives') do (
            set DriveString=%%D
            echo Drivestring=!drivestring!
            echo.
            set DriveString2=!DriveString:~8!
            echo Drivestring2=!drivestring2!
            )

        The output is Drivestring=Drives: A:\   and  Drivestring2=A:\

        Perhaps this is a peculiarity of my system (XP Home SP.3) - advice appreciated.   

        Salmon Trout

        • Guest
        Re: Writing a Batch File to Copy and Open File on CD
        « Reply #6 on: April 18, 2010, 06:10:35 AM »
        Hmm...

        Windows 7 Professional

        Code: [Select]
        Drivestring=Drives: C:\ D:\ E:\ F:\ H:\ L:\ S:\ X:\

        Drivestring2=C:\ D:\ E:\ F:\ H:\ L:\ S:\ X:\

        XP Professional SP3

        Code: [Select]
        Drivestring=Drives: A:\

        Drivestring2=A:\


        Salmon Trout

        • Guest
        Re: Writing a Batch File to Copy and Open File on CD
        « Reply #7 on: April 18, 2010, 06:50:59 AM »
        This method works on Windows 7 Pro and XP Pro SP3

        Code: [Select]
        @echo off
        set DriveString=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
        for %%L in (%DriveString%) do (
        if exist "%%L:\" fsutil fsinfo drivetype %%L:\ | find "CD-ROM Drive"
        )

        T.C.



          Beginner

          Thanked: 13
          Re: Writing a Batch File to Copy and Open File on CD
          « Reply #8 on: April 18, 2010, 05:03:42 PM »
          S.T. - I tried your latest offering on Win XP Pro SP.3 and got into an endless loop (or lockup) with the Windows error "Windows - No Disk Exception Process message c0000013 Parameters etc...." (see attachment).  This was caused by floppy drive A: not being ready. As soon  as a disk was inserted, or A was removed from DriveString,  your coding ran but didn't produce an output for a second cdrom if two physical drives were installed.  Apart from that - nice one - thanks.

          With help from a T. Salmi site I've generated the following script which checks for more than one cdrom and copies a file from whichever drive the cd is inserted.  Only problem is that it is exceedingly slow to execute, need to exit the loop when copying is complete.

          Code: [Select]
          @echo off
          cls
          setlocal enabledelayedexpansion

          for %%1 in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
              cmd /c if exist %%1:\ (
              fsutil fsinfo drivetype %%1:|find /i "cd-rom">nul && set cdrom=%%1
              if exist !cdrom!:\readme.txt copy !cdrom!:\readme.txt g:\ > nul
              set cdrom=
                    )
            )
           

          If the OP ever returns he/she can decide which script to use and change paths\filenames accordingly.

          Thanks for your input and guidance.




          [recovering disk space - old attachment deleted by admin]

          ghostdog74



            Specialist

            Thanked: 27
            Re: Writing a Batch File to Copy and Open File on CD
            « Reply #9 on: April 18, 2010, 06:58:56 PM »
            Only problem is that it is exceedingly slow to execute, need to exit the loop when copying is complete.

            Code: [Select]
            @echo off
            cls
            setlocal enabledelayedexpansion

            for %%1 in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
                cmd /c if exist %%1:\ (
                fsutil fsinfo drivetype %%1:|find /i "cd-rom">nul && set cdrom=%%1
                if exist !cdrom!:\readme.txt copy !cdrom!:\readme.txt g:\ > nul
                set cdrom=
                      )
              )
             

            its slow because you are calling fsutil + find 26 times due to 26 letters of the alphabet. That's the problem of using batch. you don't have much control over the tools you use, leading to redundant code. Why can't fsutil have an option to specify what drivetype you are looking for, something like eg fsutil fsinfo drivetype cdrom and then showing you which drive letter it is ?


            Code: [Select]
            Const CDROM = 4
            Set objFS=CreateObject("Scripting.FileSystemObject")
            Set shell = CreateObject("Shell.Application")
            For Each drives in objFS.Drives
              If drives.DriveType = CDROM Then
                cd=drives.DriveLetter & ":\"   
                WScript.Echo cd 
              End If
            Next


            T.C.



              Beginner

              Thanked: 13
              Re: Writing a Batch File to Copy and Open File on CD
              « Reply #10 on: April 18, 2010, 09:27:33 PM »
              Thanks for dropping in G.D.

              Quote from: G.D.
              its slow because you are calling fsutil + find 26 times due to 26 letters of the alphabet

              Yes, that's appreciated, should exit the For loop as soon as the Copy is complete then there'd be no need to interrogate the rest of the var.

              Quote from: G.D.
              Why can't fsutil have an option to specify what drivetype you are looking for, something like eg fsutil fsinfo drivetype cdrom and then showing you which drive letter it is ?

              OMG you had me diving into the fsutil specs to see if it was possible.   As to your question, totally agree, the next time I'm invited to a soirée at Redmond I'll mention your suggestion at the highest level.   Look for it on patch Tuesday but don't hold your breath.

              Thanks for the VBscript, yet another option if the OP ever returns to claim his/her prize.  But then the call is for a Batch script.

              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: Writing a Batch File to Copy and Open File on CD
              « Reply #11 on: April 18, 2010, 09:31:32 PM »
              the next time I'm invited to a soirée at Redmond I'll mention your suggestion at the highest level. 

               ::)
              I was trying to dereference Null Pointers before it was cool.

              Salmon Trout

              • Guest
              Re: Writing a Batch File to Copy and Open File on CD
              « Reply #12 on: April 19, 2010, 12:19:45 AM »
              S.T. - I tried your latest offering on Win XP Pro SP.3 and got into an endless loop (or lockup) with the Windows error "Windows - No Disk Exception Process message c0000013 Parameters etc...." (see attachment).  This was caused by floppy drive A: not being ready. As soon  as a disk was inserted, or A was removed from DriveString,  your coding ran but didn't produce an output for a second cdrom if two physical drives were installed.  Apart from that - nice one - thanks.

              It is in fact pointless to test drive letters A or B (and probably C as well) to see if they are CD-ROM drives.

              Why are you doing this?

              cmd /c if exist %%1:\ (

              In fact the code I posted should, in the case of multiple CD-ROM drives, give the drive letter of the last one.

              T.C.



                Beginner

                Thanked: 13
                Re: Writing a Batch File to Copy and Open File on CD
                « Reply #13 on: April 19, 2010, 01:13:25 AM »
                Quote
                Why are you doing this?

                cmd /c if exist %%1:\ (

                It seemed a good idea at the time but is, of course, totally superfluous.


                Salmon Trout

                • Guest
                Re: Writing a Batch File to Copy and Open File on CD
                « Reply #14 on: April 19, 2010, 11:30:34 AM »
                Only problem is that it is exceedingly slow to execute

                On a 2 month old AMD Phenom II X4 3.0 GHz machine running Windows 7, running the loop through 26 drive letters, 5 of which correspond to actual volumes,  took...

                Code: [Select]
                start 18:16:34.75
                end   18:16:35.02

                270 milliseconds; slightly over a quarter of 1 second.

                On a 6 year old P4 3.0 GHz machine with 4 drive letters in use...

                Code: [Select]
                start 18:25:09.45
                end   18:25:09.78

                330 milliseconds; slightly slower; about one third of a second.

                I think your slowness must be due to something else.

                Quote from: The Ghost Dog
                ts slow because you are calling fsutil + find 26 times due to 26 letters of the alphabet.

                Actually you're not; if you read the code, fsutil is called once for each drive letter that exists.

                Quote from: PhantomHound
                That's the problem of using batch. you don't have much control over the tools you use

                It helps to understand the code...  :)