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

file exists doesn't work how it supposed to be

Status
Not open for further replies.

masterofc

Programmer
Sep 8, 2015
8
NL
Code:
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
 
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.
 
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.
 
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)

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

are wrong. You don't need to 'dubble' anything.
 
I think at least one of your issues is that you are adding double-quotes to the path, which isn't needed.
 
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)
 
You'll have got error messages, no doubt. What were they?
 
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?

 
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.
 
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

 
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).
 
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
 
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).
 
>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.
 
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
 
Ok ... but the following is functionally identical to your current code and what you describe ...:

Code:
[blue]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[/blue]


 
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:)
 
Is your last post a response to mine? If so, I think you may have misunderstood what I was saying.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top