Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Continued xcopy script

Reply
Thread Tools Display Modes

Continued xcopy script

 
 
Bond
Guest
Posts: n/a

 
      01-12-2009
Hi again!

....Continuing from my previous post. Since it spawned such a good
conversation I can't figure out how I can add a loop counter to my
checking for an empty folder (which BTW I already had in my original code)

In the example below you will see what I am trying to do unsuccessfully.

For /l %%x in (1,1,10) do (
For /d %%i in (\\%1\%3\%4) do (
if exist %%i\*.xml goto :copy
)
choice /c YN /t 5 /d N /m "Do you want to stop processing of this
batch?"
set /a Count+=1
echo %Count% times
)



 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-12-2009

"Bond" <> wrote in message
news:ufgZ8$...
> Hi again!
>
> ...Continuing from my previous post. Since it spawned such a good
> conversation I can't figure out how I can add a loop counter to my
> checking for an empty folder (which BTW I already had in my original code)
>
> In the example below you will see what I am trying to do unsuccessfully.
>
> For /l %%x in (1,1,10) do (
> For /d %%i in (\\%1\%3\%4) do (
> if exist %%i\*.xml goto :copy
> )
> choice /c YN /t 5 /d N /m "Do you want to stop processing of this
> batch?"
> set /a Count+=1
> echo %Count% times
> )


Try this:
@echo off
setlocal EnableDelayedExpansion
set count=0
For /l %%x in (1,1,10) do (
For /d %%i in (\\%1\%3\%4) do (
if exist %%i\*.xml goto :copy
)
choice /c YN /t 5 /d N /m "Do you want to stop processing of this
batch?"
set /a Count+=1
echo !Count! times
)
Your script has two problems:
- You never initialise the %count% variable. Sometimes you can get away with
this, sometimes you can't.
- All environmental variables inside the "For" loop are scanned and resolved
exactly once: When the whole "For" construct, up to the closing bracket, is
read by the command processor. If your code changes them on the fly then you
must force a rescan, which you do with the "EnableDelayedExpansion" and the
use of exclamation marks around your variables.

By the way - is it your intention to ask the same question ten times?


 
Reply With Quote
 
Bond
Guest
Posts: n/a

 
      01-12-2009

"Pegasus (MVP)" <> wrote in message
news:...
> Your script has two problems:
> - You never initialise the %count% variable. Sometimes you can get away
> with this, sometimes you can't.


I actually do have this defined, missed the line when I posted.

> - All environmental variables inside the "For" loop are scanned and
> resolved exactly once: When the whole "For" construct, up to the closing
> bracket, is read by the command processor. If your code changes them on
> the fly then you must force a rescan, which you do with the
> "EnableDelayedExpansion" and the use of exclamation marks around your
> variables.


I did ty the !count! variable before but it didn't work for me. Once I
added the EnableDelayedExpansion it did - Never put the two together before
so thank you for that.

>By the way - is it your intention to ask the same question ten times?


Well, I haven't come up with a better way yet to give the option of stopping
the batch file. You see I'm waiting for a file or folder to show up at
which time I want to move it to a different location. I don't know the
filename or the foldername and I don't know exactly when the either will
show up so I am looping for up to 2 hours before I consider the file a no
show and error out of the script and send a message.



 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-12-2009

"Bond" <> wrote in message
news:...
>
> "Pegasus (MVP)" <> wrote in message
> news:...
>> Your script has two problems:
>> - You never initialise the %count% variable. Sometimes you can get away
>> with this, sometimes you can't.

>
> I actually do have this defined, missed the line when I posted.
>
>> - All environmental variables inside the "For" loop are scanned and
>> resolved exactly once: When the whole "For" construct, up to the closing
>> bracket, is read by the command processor. If your code changes them on
>> the fly then you must force a rescan, which you do with the
>> "EnableDelayedExpansion" and the use of exclamation marks around your
>> variables.

>
> I did ty the !count! variable before but it didn't work for me. Once I
> added the EnableDelayedExpansion it did - Never put the two together
> before so thank you for that.
>
>>By the way - is it your intention to ask the same question ten times?

>
> Well, I haven't come up with a better way yet to give the option of
> stopping the batch file. You see I'm waiting for a file or folder to show
> up at which time I want to move it to a different location. I don't know
> the filename or the foldername and I don't know exactly when the either
> will show up so I am looping for up to 2 hours before I consider the file
> a no show and error out of the script and send a message.
>


There is, of course, more than one way to skin this particular cat. Instead
of using a batch file and the legacy "ask" utility, you could use a VB
Script solution and get a nice GUI interface thrown in free of charge. Have
a look at this one:
01. Set oFSO = CreateObject("Scripting.FileSystemObject")
02. Set oArgs = WScript.Arguments
03. Set oShell = WScript.CreateObject("WScript.Shell")
04. sSource = Replace(oArgs(0) & "\", "\\", "\")
05. sTarget = Replace(oArgs(1) & "\", "\\", "\")
06.
07. Const iWait = 120 'minutes
08.
09. dEndTime = DateAdd("n", Now(), iWait)
10. On Error Resume Next
11. Do
12. i = oFSO.CopyFile(sSource & "*.xml", sTarget)
13. If Err.number = 0 then Exit Do
14. If oShell.Popup("Continue waiting?", 10, "Copy Process", 4) = 7 _
15. Then WScript.Quit
16. if now() > dEndTime then wscript.quit
17. Loop
18. On Error Goto 0

The script will do this:
- Copy all .xml files from the source to the target, then exit.
- If it cannot find any .xml files then it will display a pop-up
panel for 10 seconds, giving the user an opportunity to bail out.
- After 10 seconds it tries another copy process.
- After 120 minutes it gives up.
Adding an EMail function would be a trivial process.

To run the script, save the code to c:\VBSCopy.vbs, then remove
the line numbers. Now start a Command Prompt and invoke it like so:

cscript //nologo c:\VBSCopy.vbs "\\Server\Share\Folder" "d:\Target Folder"


 
Reply With Quote
 
Bond
Guest
Posts: n/a

 
      01-15-2009

"Pegasus (MVP)" <> wrote in message
news:...
> There is, of course, more than one way to skin this particular cat.
> Instead of using a batch file and the legacy "ask" utility, you could use
> a VB Script solution and get a nice GUI interface thrown in free of
> charge. Have a look at this one:


I was under the impression VBS wasn't very good for moving network files
around and that I should use WMI to do it instead which is why I went to a
bat file to begin with. I haven't tried this script yet, but what will be
the affect if I change the target to be another UNC name and if I change the
FSO.CopyFile toFSO.MoveFile?



 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-15-2009

"Bond" <> wrote in message
news:...
>
> "Pegasus (MVP)" <> wrote in message
> news:...
>> There is, of course, more than one way to skin this particular cat.
>> Instead of using a batch file and the legacy "ask" utility, you could use
>> a VB Script solution and get a nice GUI interface thrown in free of
>> charge. Have a look at this one:

>
> I was under the impression VBS wasn't very good for moving network files
> around and that I should use WMI to do it instead which is why I went to a
> bat file to begin with. I haven't tried this script yet, but what will be
> the affect if I change the target to be another UNC name and if I change
> the FSO.CopyFile toFSO.MoveFile?


Your impression is incorrect. The File System Object can handle networked
files as easily as local files. Why don't you give it a try to make sure?


 
Reply With Quote
 
Bond
Guest
Posts: n/a

 
      01-19-2009

"Pegasus (MVP)" <> wrote in message
news:eOm%...

> Your impression is incorrect. The File System Object can handle networked
> files as easily as local files. Why don't you give it a try to make sure?


Hi Pegasus,

I tried your script but unfortunately it doesn't work. I recive the Copy
Process dialog box to continue or not but answering yes or no makes no
difference to the outcome and the XML file(s) are not copied.

My command line is:
cscript VBSCopy.vbs //nologo \\server1\out$ \\server2\archive$


 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-19-2009

"Bond" <> wrote in message
news:...
>
> "Pegasus (MVP)" <> wrote in message
> news:eOm%...
>
>> Your impression is incorrect. The File System Object can handle networked
>> files as easily as local files. Why don't you give it a try to make sure?

>
> Hi Pegasus,
>
> I tried your script but unfortunately it doesn't work. I recive the Copy
> Process dialog box to continue or not but answering yes or no makes no
> difference to the outcome and the XML file(s) are not copied.
>
> My command line is:
> cscript VBSCopy.vbs //nologo \\server1\out$ \\server2\archive$


Since the code worked when I tested it, I have to respond with my standard
reply in such cases: Please post the VB code you use.


 
Reply With Quote
 
Bond
Guest
Posts: n/a

 
      01-22-2009

"Pegasus (MVP)" <> wrote in message
news:...
> reply in such cases: Please post the VB code you use.


Here you go.

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oArgs = WScript.Arguments
Set oShell = WScript.CreateObject("WScript.Shell")
sSource = Replace(oArgs(0) & "\", "\\", "\")
sTarget = Replace(oArgs(1) & "\", "\\", "\")

Const iWait = 1 'minutes

dEndTime = DateAdd("n", Now(), iWait)
On Error Resume Next
Do
i = oFSO.CopyFile(sSource & "*.xml", sTarget)
if Err.number = 0 then Exit Do
If oShell.Popup("Continue waiting?", 10, "Copy Process", 4) = 7 _
Then WScript.Quit
if now() > dEndTime then wscript.quit
Loop
On Error Goto 0


 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      01-22-2009

"Bond" <> wrote in message
news:...
>
> "Pegasus (MVP)" <> wrote in message
> news:...
>> reply in such cases: Please post the VB code you use.

>
> Here you go.
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oArgs = WScript.Arguments
> Set oShell = WScript.CreateObject("WScript.Shell")
> sSource = Replace(oArgs(0) & "\", "\\", "\")
> sTarget = Replace(oArgs(1) & "\", "\\", "\")
>
> Const iWait = 1 'minutes
>
> dEndTime = DateAdd("n", Now(), iWait)
> On Error Resume Next
> Do
> i = oFSO.CopyFile(sSource & "*.xml", sTarget)
> if Err.number = 0 then Exit Do
> If oShell.Popup("Continue waiting?", 10, "Copy Process", 4) = 7 _
> Then WScript.Quit
> if now() > dEndTime then wscript.quit
> Loop
> On Error Goto 0
>


What you report shakes the very foundations of VB Scripting. You claim that
it makes no difference whether you click "Yes" or "No" in thedialog box -
let's put it to the test!

Set oShell = WScript.CreateObject("WScript.Shell")
x = oShell.Popup("Continue waiting?", 10, "Copy Process", 4)
MsgBox "Return code is " & x

Do this:
- Paste the above code into c:\Test.vbs.
- Execute the code the same way as you did before.
- Post the return codes you see.

By the way: The command line you used is not really robust. Instead of
running

cscript VBSCopy.vbs //nologo \\server1\out$ \\server2\archive$
you should run this:
cscript c:\VBSCopy.vbs //nologo \\server1\out$ \\server2\archive$

If you omit the drive and folder information then you might be executing any
program and possibly not the one you think you're running!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: XCopy in login script mtstream Windows Server 0 02-05-2007 10:31 PM
Re: XCopy in login script Glenn Clark Windows Server 0 02-05-2007 04:45 PM
Speeding up XCOPY script-possible?? Courtney R Scripting 2 12-22-2005 03:51 AM
XCOPY Script Error Ben Windows Server 6 10-29-2005 04:36 AM
Xcopy script with a GUI prompt Serge Ayotte Scripting 2 02-11-2005 02:15 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59