Computer Hope

Software => Computer programming => Topic started by: trueparadox on April 13, 2010, 10:29:45 AM

Title: Writing a Batch File to Copy and Open File on CD
Post by: trueparadox 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?
 :-\

Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: T.C. 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? 
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout 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.




Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: T.C. 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"
)




Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout 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%\"
)
)
)
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: T.C. 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.   
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout 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:\

Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout 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"
)
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: T.C. 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]
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: ghostdog74 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

Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: T.C. 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.
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: BC_Programmer 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. 

 ::)
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout 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.
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: T.C. 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.

Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout 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...  :)






Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: ghostdog74 on April 19, 2010, 10:16:56 PM
Actually you're not; if you read the code, fsutil is called once for each drive letter that exists.
one don't normally (normal circumstances) have that much drives letters being used up , but however it that does occur, you would still need to go through that loop and test for each one. ie (if "Z" is really the drive letter for CDROM).
I really should have said that he need to exit the loop once the CDROM drive letter is found so that he won't go through the rest of the letters.
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout on April 20, 2010, 12:16:04 AM
one don't normally (normal circumstances) have that much drives letters being used up , but however it that does occur, you would still need to go through that loop and test for each one. ie (if "Z" is really the drive letter for CDROM).
I really should have said that he need to exit the loop once the CDROM drive letter is found so that he won't go through the rest of the letters.

My CD-ROM drive letters are X on one machine and Z on another.

If 26 letters take around 250 mS, why worry?

Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: ghostdog74 on April 20, 2010, 01:00:45 AM
If 26 letters take around 250 mS, why worry?
well, that's fine if you are measuring against a simple task such as find a drive letter and that's all. In general if there are other intermediate tasks inside the for loop that needs to be performed, its still better to use a more efficient approach. I know, not everyone is a speed freak like me... :)
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: BC_Programmer on April 20, 2010, 01:32:47 AM
well, that's fine if you are measuring against a simple task such as find a drive letter and that's all. In general if there are other intermediate tasks inside the for loop that needs to be performed, its still better to use a more efficient approach. I know, not everyone is a speed freak like me... :)

a Speed freak that prefers interpreted languages?
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: ghostdog74 on April 20, 2010, 02:39:15 AM
a Speed freak that prefers interpreted languages?
almost what we do involves interpretation. You write a batch file, and its interpreted by cmd.exe. same as vbscript, Python ,Perl. etc.  When i mention "speed freak" i am talking about doing things in the most efficient way, short of writing a real C program (or what the heck, assembly) myself. I want the power and fast development time of an interpreted language as well. got that?
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout on April 20, 2010, 11:04:32 AM
doing things in the most efficient way

That phrase is capable of so many differing interpretations
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: ghostdog74 on April 20, 2010, 05:28:27 PM
That phrase is capable of so many differing interpretations
like?
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout on April 21, 2010, 12:05:11 AM
think about it.
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: ghostdog74 on April 21, 2010, 12:27:27 AM
think about it.

i have already stated my case. doing thing the most efficient way,
1) speed of development. with good data structures and ease of use of my tools, i can create programs to do my task in less than a few minutes.
2) with a good tool, I can have better control of the things i want to do, reducing the chances of writing redundant code. With less redundant code, my program/script will run faster.

If you are not going to say what you meant, then what's your point in posting?
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: Salmon Trout on April 21, 2010, 12:03:54 PM
well, that's fine if you are measuring against a simple task such as find a drive letter and that's all.

That's what this thread is about.
Title: Re: Writing a Batch File to Copy and Open File on CD
Post by: ghostdog74 on April 21, 2010, 06:18:28 PM
That's what this thread is about.
yes, this thread is about that. But my replies all started with TC's comment on his batch's slowness,  not directly meant for the OP, trueparadox.