×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

file exists doesn't work how it supposed to be

file exists doesn't work how it supposed to be

file exists doesn't work how it supposed to be

(OP)

CODE --> vbscript

Option Explicit 'Force explicit variable declaration

'declare variable
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim sourceFolder : sourceFolder = Array ("D:\sourceFolder Een","D:\sourceFolder Twee")
Dim targetFolder : targetFolder = Array("D:\Dest Een","D:\Dest Twee")
Dim targetFolderFTP : targetFolderFTP = Array ("D:\ftp1","D:\ftp2")
Dim file, fileWithOutExtension, RFcounter, sFolder, folder, files, filesIdx, ReadFolder,input, quotes, quotestwee

quotes=""""

'cals function LoopAllFiles to trigger the process
LoopAllFiles()

'Calls function to loop true the files
Function LoopAllFiles()
	FOR RFcounter = 0 to Ubound(sourceFolder)
		sFolder = sourceFolder(RFcounter) 
		Set folder = FSO.GetFolder(sFolder)
		Set files = folder.Files
		For each filesIdx IN files   
			file= filesIdx.name					
			fileWithOutExtension = Left(file, InStrRev(file,".") - 1)	
			
			'<<---- for testing, can be deleted!! ----- >>
			'wscript.echo("test1")
			'wscript.echo(dubble(sourceFolder(RFcounter) & "\" & file))
			'wscript.echo(fso.FileExists(dubble(sourceFolder(RFcounter) & "\" & file)))
			'<<---- till here --->
			
                        'HELP THIS DOESN"T WORK PROPERLY
			If (FSO.FileExists(dubble(sourceFolder(RFcounter) & "\" & file))) Then      
				deleteFilesFromtargetFolder()
			End if
			If fso.GetExtensionName(UCase(file)) = "TIF" Then
				Select Case RFCounter
				Case "0"
					methodOne()
					WScript.Sleep 2000 'wacht 2 seconde ivm de importer 
				Case "1"
					methodTwo()
					WScript.Sleep 2000 'wacht 2 seconde ivm de importer 
				End Select
			End if
			moveBestanden()
			WScript.Sleep 2000 'wacht 2 seconde ivm de importer 
		Next
	NEXT
End function

Function dubble(input)
	dubble = quotes & input & quotes
End Function

'Delete files in targetFolder if exists
Sub deleteFilesFromtargetFolder()
	wscript.echo("Deleting files...")
	If (fso.FileExists(dubble(targetFolder(RFcounter) & "\" & file))) Then 
		fso.DeleteFile(dubble(targetFolder(RFcounter) & "\" & file)) 'Deletes tif file in target folder
	End if
End Sub
 
'Movefiles
Sub moveBestanden()
	 wscript.echo("copy files")
	 fso.MoveFile dubble(sourceFolder(RFcounter) & "\" & file), dubble((targetFolder(RFcounter)) & "\" & file) 'Moves files <----- this doesn't work properly 
End Sub

Sub methodOne()
	wscript.echo("copy files to fto CK")
	fso.CopyFile dubble(sourceFolder(RFcounter) & "\" & file), dubble(targetFolderFTP(RFcounter) & "\" & file) 'Moves files <----- this doesn't work properly 
End Sub

Sub methodTwo()
	wscript.echo("copy files to ftp TH")
	fso.CopyFile dubble(sourceFolder(RFcounter) & "\" & file), dubble(targetFolderFTP(RFcounter)) & "\" & file 'Moves files <----- this doesn't work properly 
End Sub 

RE: file exists doesn't work how it supposed to be

What do you mean "doesn't work properly".
What do you expect and what does it do?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: file exists doesn't work how it supposed to be

(OP)
it loops to echo all the files in de directory's from the array sourceFolder een & souceFolder Two.
Then it has to check if the file exists in that dictory (the derectory is based on the RFcounter). when it exits it should run the sub deletefilesfromtargetFolder. however its still keeps saying the file doesn't exists (but it does exists because i first echo's all the files in the specifick directory.
because of that it also go's wrong with the process of copy the files. i have this problem because there is a white space in the directory (it cannot be changed because because more processes depents on it). the first variant works but there is no white space in the array item.

RE: file exists doesn't work how it supposed to be

FileExists is working exactly how it should. However, your code isn't ...

These lines (and the following conditional ones, if they were ever to run, which they won't)

If (FSO.FileExists(dubble(sourceFolder(RFcounter) & "\" & file))) Then
and
If (fso.FileExists(dubble(targetFolder(RFcounter) & "\" & file))) Then

are wrong. You don't need to 'dubble' anything.

RE: file exists doesn't work how it supposed to be

I think at least one of your issues is that you are adding double-quotes to the path, which isn't needed.

RE: file exists doesn't work how it supposed to be

(OP)
ok.. its seems you are wright, you don't need it to be double quoted. for the fso.fileexists. However, when you call the sub methodOne() and sub methodTwo() it doen't copy. what ever i try just the both code doesn't work:

'Movefiles
Sub moveBestanden()
fso.MoveFile sourceFolder(RFcounter) & "\" & file, targetFolder(RFcounter) & "\" & file 'Moves files <-- not work it keeps saying there is a problem with the filename
End Sub

Sub methodOne()
fso.CopyFile sourceFolder(RFcounter) & "\" & file, targetFolderFTP(RFcounter) & "\" & file 'Moves files <-- not work it keeps saying there is a problem with the filename

End Sub

Sub methodTwo()
fso.CopyFile sourceFolder(RFcounter) & "\" & file, targetFolderFTP(RFcounter) & "\" & file 'Moves files <-- not work it keeps saying there is a problem with the filename

End Sub


OR with double qoutes:
ub moveBestanden()
wscript.echo("copy files")
wscript.echo(dubble(sourceFolder(RFcounter) & "\" & file))
fso.MoveFile dubble(sourceFolder(RFcounter) & "\" & file), dubble((targetFolder(RFcounter)) & "\" & file) 'Moves files <-- not work
End Sub

Sub methodOne()
wscript.echo("copy files to fto CK")
fso.CopyFile dubble(sourceFolder(RFcounter) & "\" & file), dubble(targetFolderFTP(RFcounter) & "\" & file) 'Moves files <--- not work
End Sub

Sub methodTwo()
wscript.echo("copy files to ftp TH")
fso.CopyFile dubble(sourceFolder(RFcounter) & "\" & file), dubble(targetFolderFTP(RFcounter)) & "\" & file 'Moves files <----- not work
End Sub

AS i said i cannot change the name of the directory ( otherwise i would have already done it) thats because it is in fact not stored at my own computer but another company ( the sourcetarget en destinationtarget are now just for test but they have to copy the files to another company so thats why i can't change the name of the folder)

RE: file exists doesn't work how it supposed to be

You'll have got error messages, no doubt. What were they?

RE: file exists doesn't work how it supposed to be

Your methodOne and methodTwo subs are identical

Please post a specific error message for a specific line

And what is the value of sourceFolder(RFcounter) & "\" & file when it fails? What is the value of targetFolder(RFcounter) & "\" & file when it fails?

RE: file exists doesn't work how it supposed to be

Actually, let's go back to basics. What exactly are you trying to do? Tis seems a somewhat cumbersome method of copying some TIF files from one directory to another, and moving anything else.

RE: file exists doesn't work how it supposed to be

(OP)
ok..
first its echo's all the files in the direcorty's (that are in the array).
then it checks if the file exits and if it exits it delete's it at the targetfolder
after that its checking if the file extension is .TIF
if its a .Tif file it should run one of the two subs (methodOne & methodTwo)
wich one it should take is based on the RFcounter (this specifies the item of the array)
soo... when RFcounter is 1 it should co to methodOne and if RFcounter is 2 it go's to methodTwo
Both method's are exactly the same exept the fact that the files that should be coppied are going to a different company . methodOne go's to company 1. mehtodTwo go's to company 2.
however when i run the subs (methodOne & methodTwo) to copy the file it tells that the file or number is incorrect.

values:
dubble(sourceFolder(RFcounter) & "\" & file) = "D:\sourceFolder Een\111.TIF" < here it said the file or number are incorrect

However if i start commandpromt and run the command:

del "D:\sourceFolder Een\111.TIF"

it works fine so that means "D:\sourceFolder Een\111.TIF" should be correct syntax

RE: file exists doesn't work how it supposed to be

No. Running del from the command prompt (which DOES require the double-quotes) is not the same as using FileSystem Object methods in vbscript (which does NOT require them).

RE: file exists doesn't work how it supposed to be

(OP)
well... i already found out that the problem lies in the destinationfolder where you wan't to copy to
'
example:
fso.CopyFile dubble(sourceFolder(RFcounter) & "\" & file), dubble(targetFolderFTP(RFcounter)) & "\" & file 'Moves files
dubble(sourceFolder(RFcounter) & "\" & file) = "D:\sourceFolder Een\111.TIF"
dubble(targetFolderFTP(RFcounter)) & "\" & file = "D:\Dest Een\111.TIF"
del "D:\sourceFolder Een\111.TIF" works
del "D:\Dest Een\111.TIF" fails

RE: file exists doesn't work how it supposed to be

So, you solved your problem???

I don't understand at all what you are saying in your last post, because you are mixing vbscript and "command line" commands (like del).

RE: file exists doesn't work how it supposed to be

>ok..
No, that's a description of what (you think) your code does. I'm asking what it is that you are trying to achieve.

RE: file exists doesn't work how it supposed to be

(OP)
thnx guys, the problem is solved. it now works perfectly. here is the script wich is working:

Option Explicit 'Force explicit variable declaration

'declare variable
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim sourceFolder : sourceFolder = Array ("D:\sourceFolder CK","D:\sourceFolder TH")
Dim targetFolder : targetFolder = Array("D:\Dest CK","D:\Dest TH")
Dim targetFolderFTP : targetFolderFTP = Array ("D:\ftp1","D:\ftp2")
Dim file, fileWithOutExtension, RFcounter, sFolder, folder, files, filesIdx, ReadFolder,input, quotes, inputFile

quotes=""

'cals function LoopAllFiles to trigger the process
LoopAllFiles()

'Calls function to loop true the files
Function LoopAllFiles()
FOR RFcounter = 0 to Ubound(sourceFolder)
sFolder = sourceFolder(RFcounter)
Set folder = FSO.GetFolder(sFolder)
Set files = folder.Files
For each filesIdx IN files
file= filesIdx.name
fileWithOutExtension = Left(file, InStrRev(file,".") - 1)
If (fso.FileExists(sourceFolder(RFcounter) & "\" & file )) Then
deleteFilesFromtargetFolder(targetFolder(RFcounter) & "\" & file)
End if
If fso.GetExtensionName(UCase(file)) = "TIF" Then
Select Case RFCounter
Case "0"
ftpCK()
WScript.Sleep 2000 'wacht 2 seconde ivm de importer
Case "1"
ftpTH()
WScript.Sleep 2000 'wacht 2 seconde ivm de importer
End Select
End if
moveBestanden()
WScript.Sleep 2000 'wacht 2 seconde ivm de importer
Next
NEXT
End function

Function dubble(input)
dubble = quotes & input & quotes
End Function

'Delete files in targetFolder if exists
Sub deleteFilesFromtargetFolder(inputFile)
If (fso.FileExists(inputFile)) Then
fso.DeleteFile(inputFile)
End if
End Sub

'Movefiles
Sub moveBestanden()
fso.MoveFile sourceFolder(RFcounter) & "\" & file, dubble(targetFolder(RFcounter) & "\" & file) 'Moves files
End Sub

Sub ftpCK()
fso.CopyFile sourceFolder(RFcounter) & "\" & file, dubble(targetFolderFTP(RFcounter) & "\" & file) 'Copy files
End Sub

Sub ftpTH()
fso.CopyFile sourceFolder(RFcounter) & "\" & file, dubble(targetFolderFTP(RFcounter) & "\" & file) 'Copy files
End Sub

RE: file exists doesn't work how it supposed to be

Ok ... but the following is functionally identical to your current code and what you describe ...:

CODE

Option Explicit 'Force explicit variable declaration

'declare variable
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim sourceFolder : sourceFolder = Array ("D:\sourceFolder CK","D:\sourceFolder TH")
Dim targetFolder : targetFolder = Array("D:\Dest CK","D:\Dest TH")
Dim targetFolderFTP : targetFolderFTP = Array ("D:\ftp1","D:\ftp2")
Dim RFcounter

'cals sub LoopAllFiles to trigger the process
LoopAllFiles()

Sub LoopAllFiles()
    For RFCounter = 0 To UBound(sourceFolder)
        fso.CopyFile sourceFolder(RFCounter) & "\*", targetFolder(RFCounter)
        fso.CopyFile sourceFolder(RFCounter) & "\*.tif", targetFolderFTP(RFCounter)
        fso.DeleteFile sourceFolder(RFCounter) & "\*"
    Next
End Sub 


RE: file exists doesn't work how it supposed to be

(OP)
true it looks almost the same but there is a kind of difference. now it copies the files where it first got an error the hole time:)

RE: file exists doesn't work how it supposed to be

Is your last post a response to mine? If so, I think you may have misunderstood what I was saying.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close