Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get a variable to maintain its original information

Status
Not open for further replies.

rnpIII

Technical User
May 2, 2001
61
US
Hello All,

I have been battling with this for a bit.
I am new to vbScript so please be patient.
Here is the scenario:
Say I get a file count of a folder and assign it to a variable.

Set Folder = Ofso.GetFolder(I)
FleCnt = Folder.Files.Count

Then I go and move files to and from this folder. Maybe even remove all files from the folder.
When I access the variable again, it has changed. Normally this is what I want but I have some cases where I need to store the original number/information and keep it the same.
What am I missing?
I don't understand how to use the Const var to do this so maybe that's it and I just need a little direction.

Any suggestions will be appreciated.
rnpIII
 
Hello rnpIII,

>When I access the variable again, it has changed...

It wouldn't change if you access the variable FleCnt rather than the right-hand-side. Something like this conceptually.
[tt]
Set Folder = Ofso.GetFolder(I)
original_FleCnt = Folder.Files.Count
'do some move, copy, delete etc
present_FleCnt = Folder.Files.Count
[/tt]
original_FleCnt is the data at that time ('present' at that time-but that is symantic), while present_FleCnt is the data at the time you want to know how many files are there. Or I miss the point...

regards - tsuji
 
Hi Tsuji,

I think I am confused about the "right- hand- side" bit, I have seen that before and don't really understand it.
Anyway, the issue is accessing FleCnt regardless of when.

I have about 500 lines of code that works well enough, but this particular piece comes up with some strange counts because if I, say,
- get the original count of say 10 files,
- then FleCnt = 10
- manipulate the directory, let's delete the files
- FleCnt should still equal 10, but now it equals 0.
Basic Scenario:
Set Folder = Ofso.GetFolder(I)
original_FleCnt = Folder.Files.Count
'do some move, copy, delete etc

' most of the time, by the time
' I get here, it already is something else
If NOT original_FleCnt = 0 Then
Do something
End If


I need to make it constant, but I will never know what that number is, could be one, could be 100.

While I find this dynamic change advantageous at times, there are times, such as this, when I can't have it change.

I also note the script walking all over itself. It will not wait for one process to finish before going to another process, I don't see any complaining about this anywhere so I assume I am doing something wrong.
Say for instance, I copy some files into a dir and then zip them, then FTP them.
Most of the time it will start zipping before the copying process is done, missing files, then it will FTP an incomplete zip.
I have put sleeps in place to try and alleviate this, and tried some code for files being in use (with negative results).
If you have any insight on this, I would greatly appreciate it, as well.

Thank you for your response and assistance.
If you have any ideas, I would appreciate it.

rnpIII
 
As far as the 'stepping on itself', you can use the following syntax to force external process to wait on one another:
Code:
Set objShell = CreateObject("WScript.Shell")
set running = objShell.exec ("%COMSPEC% /k DosCommandStringHere)
Do While running.Status = 0
     WScript.Sleep 100
Loop

This will loop until the dos command completes, then continue the rest of the script.

The variables aren't dynamic, as far as I've ever heard. If you say X=a+b and a=1 & b=2, X=3 and remains so until you set X=something else, regardless of what happens to a or b afterwards. Please post your code so we can see the logic used to set this 'dynamic' variable. Are you certain there is no way you're looping back up to reset it after you've deleted the files & before you're checking it the second time?

You can also get the VB debugger and run this in the debugger. It will allow you to step through your code line-by-line and verify variable contents at any point you choose.
 
Hello Jerz,

Appreciate the advice, this may help on the zip piece.
As for the aforementioned code, see below:

Begin Code:------------------------------------------------------
Sub CopyIT (I, II) ' Possible vars - I,II BilDir, WorkDir
' FinDir, WorkDir
Dim WrkCnt
WrkCnt = 0

If I = BilDir Then ' If we are copying bills then also get file from
Set Folder = Ofso.GetFolder(ManDir) ' the manuals dir.
FleCntI = Folder.Files.Count
If FleCntI > 0 Then ' only if there are files in the dir.
Ofso.CopyFile ManDir &"\*.txt", II
End If
End If

' Get a file count in the I dir
Set Folder = Ofso.GetFolder(I)
FleCntII = Folder.Files.Count
' If There are no files then exit the function and
' check the next directory
If FleCntI + FleCntII <= 0 Then
Exit Sub
End If
If FleCntII > 0 Then
Ofso.CopyFile I & "\*.txt" , II
End If
' Get a file count in the work dir.
Set Folder = Ofso.GetFolder(II)
For Each File In Folder.Files ' recurse through the dir making sure we
vExt = Ofso.GetExtensionName(File) ' are only counting txt files
Select Case vExt
Case "txt"
WrkCnt = WrkCnt + 1
Case "TXT"
WrkCnt = WrkCnt + 1
Case "Txt"
WrkCnt = WrkCnt + 1
End Select

Next
' If the count after the copy does not match then error out.
' **********************************************************************************
If Not FleCntI + FleCntII = WrkCnt Then ' This is the piece that will change
' WrkCnt will change if I just use Folder.Files.Count, so I use the above For loop
' to make sure it will not change.
' also sometimes I will have zip files in there and this will throw off the count
' but when I noticed it, the count was zero and should have been 50 something.
' the dir was empty. It was just writing the variable to a file for logging
' farther down the code.
' **********************************************************************************
Wscript.Echo "All Files were not transfered from "& I & Chr(13) & _
" to "& II & Chr(13) & _
"Please verify and try again." & Chr(13) & _
"The script is quiting"
FleInfo.WriteLine "File count did NOT match for "& I &" and "& II &"."
FleInfo.WriteLine "Quiting script!"
Wscript.Quit
Else
' Write the log if all is well
FleInfo.WriteLine "File count for "& I &" is "& FleCntII
If I = BilDir Then
FleInfo.WriteLine "File count for "& ManDir &" is "& FleCntI
End If
FleInfo.WriteLine "Total files to be copied is "& FleCntI + FleCntII
' *********
FleInfo.WriteLine "File count for the Work directory is "& WrkCnt &" after the copy."
' *********
' I decided to try to use this variable for some checking later on in the script but couldn't
' rely on it, now with the way I have it written, it won't change.

FleInfo.WriteLine "File count matches for "& I &" and "& II &"."
If I = BilDir Then ' special for bills and manuals, makes the log prettier
FleInfo.WriteLine "Deleting files from "& I &" and or "& ManDir &" if applicable."
Else
FleInfo.WriteLine "Deleting files from "& I
End If
FleInfo.WriteBlankLines(1)

If FleCntII > 0 Then ' Clear out the Home dir for more files
Ofso.DeleteFile(I &"\*.txt")
End If
If I = BilDir Then ' for bills, if there are bills, then there are probably
Set Folder = Ofso.GetFolder(ManDir) ' manuals, this will clear them out
FleCnt = Folder.Files.Count
If FleCnt > 0 Then
Ofso.DeleteFile(ManDir &"\*.txt")
End If
End If
End If

Select Case I
Case BilDir
vTyp = "Bills"
Case ManDir
vTyp = "Bills"
Case FinDir
vTyp = "Finals"
End Select

DiffChk(vTyp) ' Check for duplicate files
TotalIT(vTyp) ' Get the numbers for billing to a log file
PrepIT(vTyp)

End Sub
End Code:------------------------------------------------------
Please pardon the mess. I am a novice at vbscript and am probably going about this all wrong. Any suggestions/criticisms you "will" have for cleaning it up will be appreciated.

As for the editor, I sprung $30.00 bucks for Adersoft's vbEditor, it works pretty well.

Thanks for your assistance.
rnpIII
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top