Home All Groups Group Topic Archive Search About
Author
12 Jan 2009 4:08 PM
Bond
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
)

Author
12 Jan 2009 4:26 PM
Pegasus (MVP)
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:ufgZ8$MdJHA.2096@TK2MSFTNGP04.phx.gbl...
> 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?
Author
12 Jan 2009 5:10 PM
Bond
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:eHQ8JKNdJHA.3708@TK2MSFTNGP06.phx.gbl...
> 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.
Author
12 Jan 2009 11:15 PM
Pegasus (MVP)
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:OzxssiNdJHA.4124@TK2MSFTNGP06.phx.gbl...
>
> "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> news:eHQ8JKNdJHA.3708@TK2MSFTNGP06.phx.gbl...
>> 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"
Author
15 Jan 2009 10:46 PM
Bond
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:OuvwiuQdJHA.552@TK2MSFTNGP06.phx.gbl...
> 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?
Author
15 Jan 2009 11:16 PM
Pegasus (MVP)
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:eexhwM2dJHA.1916@TK2MSFTNGP02.phx.gbl...
>
> "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> news:OuvwiuQdJHA.552@TK2MSFTNGP06.phx.gbl...
>> 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?
Author
19 Jan 2009 2:29 PM
Bond
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:eOm%23dd2dJHA.1528@TK2MSFTNGP03.phx.gbl...

> 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$
Author
19 Jan 2009 2:44 PM
Pegasus (MVP)
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:O9KiaJkeJHA.3856@TK2MSFTNGP06.phx.gbl...
>
> "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> news:eOm%23dd2dJHA.1528@TK2MSFTNGP03.phx.gbl...
>
>> 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.
Author
22 Jan 2009 3:10 PM
Bond
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:ew0ysRkeJHA.3788@TK2MSFTNGP06.phx.gbl...
> 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
Author
22 Jan 2009 7:29 PM
Pegasus (MVP)
Show quote Hide quote
"Bond" <b***@james.com> wrote in message
news:OeoUDOKfJHA.5844@TK2MSFTNGP05.phx.gbl...
>
> "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> news:ew0ysRkeJHA.3788@TK2MSFTNGP06.phx.gbl...
>> 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!
Author
23 Jan 2009 1:28 PM
Bond
"Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
news:OBWpIfMfJHA.5496@TK2MSFTNGP02.phx.gbl...
> 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!

Sorry, I don't mean to shake anything... :)  and I do appologize for not
being more detailed in my explaination.
When I said "it makes no difference" I was referring to the fact that the
XML file is not copied to the destination whether I click on "Yes" or "No"
or let it time out.

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

Clicking yes returns code 6, clicking no returns code 7 and not clicking on
anything returns -1
Author
23 Jan 2009 3:25 PM
Pegasus (MVP)
You previously wrote "I recive the Copy Process dialog box to continue or
not but answering yes or no makes no difference to the outcome". From your
new reply I gather that this is not correct: When you click the "No" button
it does make a difference - the script ends immediately. The VB Script
foundations appear to be intact . . .

This allows us to concentrate on the failure to copy files. My original
script did things "on the cheap": It attempted to copy *.xml files until it
would find some but only for a fixed period of time. To allow the script to
continue if no files were found, I suppressed all possible error messages.
This is not the most elegant way of doing things and it promptly tripped you
up. Here is a more robust way of doing things. It will show you all error
that occur.

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

LF = Chr(10)
Const iWait = 1 'minutes
dEndTime = DateAdd("n", iWait, Now())

On Error Resume Next
If Not oFSO.FolderExists(sTarget) then oFSO.CreateFolder(sTarget)
If Err > 0 Then
MsgBox "Problem creating the folder """ & sTarget _
  & """" & LF & Err.Description
WScript.Quit
End If
On Error Goto 0

Do
bFound = False
WScript.Echo "Checking for the existence of .xml files . . ."
For Each oFile In oFSO.GetFolder(sSource).Files
  If ucase(right(oFile.Name, 4)) = ".XML" then bFound = True
Next

If bFound Then
  On Error Resume Next
  oFSO.CopyFile sSource & "*.xml", sTarget
  If Err > 0 Then MsgBox "Problem copying files:" _
   & LF & Err.Description
   On Error Goto 0
  WScript.Quit
End If

If Now() > dEndTime _
Then If oShell.Popup("Continue waiting?", 10, "Copy Process", 4) = 7 _
Then WScript.quit
WScript.Sleep 20000
Loop




Show quoteHide quote
"Bond" <b***@james.com> wrote in message
news:OyeC%235VfJHA.5540@TK2MSFTNGP06.phx.gbl...
>
> "Pegasus (MVP)" <I.***@fly.com.oz> wrote in message
> news:OBWpIfMfJHA.5496@TK2MSFTNGP02.phx.gbl...
>> 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!
>
> Sorry, I don't mean to shake anything... :)  and I do appologize for not
> being more detailed in my explaination.
> When I said "it makes no difference" I was referring to the fact that the
> XML file is not copied to the destination whether I click on "Yes" or "No"
> or let it time out.
>
>> Set oShell = WScript.CreateObject("WScript.Shell")
>> x = oShell.Popup("Continue waiting?", 10, "Copy Process", 4)
>> MsgBox "Return code is " & x
>
> Clicking yes returns code 6, clicking no returns code 7 and not clicking
> on anything returns -1
>
>