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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For...Next Loop & concatenating a variable name through it?

Status
Not open for further replies.

MISMCSA

MIS
Jun 24, 2004
43
US
I could use some suggestions here.

What I am trying to do is set up a loop that will check to see if these files exist. Rather than hardcode these files into an If...Then logic fest. The loop below is supposed to check and see if a file exists starting with TargetFile4 and working down to TargetFile0. The "& i" idea does not work and I'm wondering if something can make this work.

TargetFile0 = "c:\asrunimp\asrunimp.log"
TargetFile1 = "c:\asrunimp\asrunimp1d.log"
TargetFile2 = "c:\asrunimp\asrunimp2d.log"
TargetFile3 = "c:\asrunimp\asrunimp3d.log"
TargetFile4 = "c:\asrunimp\asrunimp4d.log"

For i = 4 to 0 Step -1
If fso.fileexists(TargetFile & i) Then ...
 
Something like this ?
TargetFile = Split( _
"c:\asrunimp\asrunimp.log", _
"c:\asrunimp\asrunimp1d.log", _
"c:\asrunimp\asrunimp2d.log", _
"c:\asrunimp\asrunimp3d.log", _
"c:\asrunimp\asrunimp4d.log")
For i = 4 to 0 Step -1
If fso.fileexists(TargetFile(i)) Then ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I tried that earlier and just tried that again,

It gives me a type mismatch error: 'TargetFile'
 
The value of i has to become part of the TargetFile name, so it calls the correct parth and file.
 
What is the actual name of one of the files that you want to check for?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hello MISMCSA,

Try something like:
If fso.fileexists(eval(TargetFile & i)) Then ...

regards - tsuji
 
It gives me a type mismatch error: 'TargetFile'
Have you tried this ?
Dim TargetFile
TargetFile = Split( _
"c:\asrunimp\asrunimp.log", _
"c:\asrunimp\asrunimp1d.log", _
"c:\asrunimp\asrunimp2d.log", _
"c:\asrunimp\asrunimp3d.log", _
"c:\asrunimp\asrunimp4d.log", ",")
For i = 4 to 0 Step -1
If fso.fileexists(TargetFile(i)) Then ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
asrunimp4d.log is the name of one of the actual files.

What I was trying to do was concatenate the value of i onto TargetFile. I thought by doing this it would check the file in path TargetFile4, then TargetFile3, etc. as the value of i decreased.
 
Something like:
For i = 1 to nFileCount
If oFSO.FileExists(strPath & "\asrunimp" & i & "d.log") Then
...

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I did it that way also. It does work. However, I didn't like it because it required me to hardcode a path and file name in. I thought by having it refer to a variable then I could just change that variable when I need to make an adjustment. I have a couple ways that work, but none that I'm absolutley satisfied with.

Thanks for the help guys.
 
MISMCSA, just to know, does the array solution works ?
 
The one I was trying to do in my first post, were I just had to change the paths in the target files, did not work. It won't recognize the valuse of i along with TargetFile. I tried TargetFile(i) and TargetFile & i. If I could get this to work it would be ideal, but I have hit a wall.

This is what I am doing.


Dim fso, SourceFile, TargetFile

Set fso = CreateObject("Scripting.FileSystemObject")

SourceFile = "\\mmsfile5\global\asrunimp.log"
TargetFile = "\\mmsfile5\global\secured\hotline\asrunimp\asrunimp?d.log"


If fso.fileexists(SourceFile) Then
If fso.fileexists(Replace(TargetFile,"?",5)) Then
fso.deletefile Replace(TargetFile,"?",5),True
End If

For i=4 to 0 Step -1
If fso.fileexists(Replace(TargetFile,"?",i)) Then
fso.MoveFile Replace(TargetFile,"?",i),_
Replace(TargetFile,"?",i+1)
End If
Next

fso.CopyFile SourceFile, Replace(TargetFile,"?","0")

End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top