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

Can't use variable for source name when using MoveFile 1

Status
Not open for further replies.

wachuna

Technical User
Mar 10, 2004
14
0
0
US
I'm trying to move a file from one directory to another. When running the script, I'd get "Bad File name or number". Can you use variable for the file name when using MoveFile method? It will work with *.* but I need to move the correct file name.

CODE:

'--According to filename type and length, this script will copy from input folder
'--to appropriate destination folders, cci or cyber.

Dim FSO, sourcefld, destfld1, destfld2, sExt, sPath, filname, lngth
Set FSO = CreateObject("Scripting.FileSystemObject")

sourcefld = ("C:\harlequinrips\rip4\bw\")
destfld1 = ("C:\harlequinrips\rip4\cci\")
destfld2 = ("C:\harlequinrips\rip4\cyber\")
sExt = "pdf"
s = DoSearch(sourcefld, sExt)
filname = FSO.GetFileName(s)
lngth = Len(filname)

If s <> "" And lngth > 23 Then
Const OverwriteExisting = True
Set FSO = CreateObject("Scripting.FileSystemObject")
MsgBox sourcefld & filname & "" & destfld1 & filname
FSO.MoveFile sourcefld & filname, destfld1 & filname
else
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.MoveFile sourcefld & filname, destfld2 & filname
End If

'--///////////////////////////////////////////////////
'-- This Function searches the files in FolPath For a match with sExtName.
'-- It Then calls itself For Each subfolder found.
'-- This can work because Each instance of the Function is a separate
'-- operation that just happens to be called from within the Function.
'-- That is, the variables Fol, SubPath, etc. are separate variables in
'-- Each instance of the Function.

Function DoSearch(sourcefld, sExt)
Dim SubPath, Fol, s1, sList, oFol, Fils, oFil, s, sPath, Fols, LExt
LExt = Len(sExt)
sExt = LCase(sExt)
Set oFol = FSO.GetFolder(sourcefld)
Set Fils = oFol.Files
If Fils.count > 0 Then
For Each oFil in Fils
If LCase(Right(oFil.name, LExt)) = sExt Then
sList = sList & oFil.Path & vbcrlf
End If
Next
End If
Set Fols = oFol.SubFolders
If Fols.count > 0 Then
For Each Fol in Fols
SubPath = Fol.Path
s1 = dosearch(SubPath, sExt)
If s1 <> "" Then
sList = sList & s1
End If
Next
End If
Set Fols = Nothing

Set Fils = Nothing
Set oFol = Nothing

DoSearch = sList
End Function

Thank You
 
If I'm not missing something, then DoSearch is returning a string that is a list of files so that

s= "File1.ext
File2.ext
File3.ext
.
.
. "

If this is the case, then there is no way to just hand this to MoveFile and expect it to know what you mean. Instead, you probably need to split s into an array then pass each element of the array to MoveFile.
 
Hello wachuna,

I doubt your script as it stands goes far before encounter error.

[1] You do not put const (ForOverwrite) inside a loop. This is guarantee of runtime error of repetitive declaration.
[2] You must validate that destination filename does not exist, else you'll have error.
[3] There is no overwrite option for movefile.
[4] Movefile does not have rollback in case error along the move. Sometime, it is safer to use copyfile then delete. But, it's up to you.
[5] s<>"" and lngth>23 would it be good enough to be the condition? I separate into two---guessing the intention. In any case if s(i)="", nothing should be done.

Code:
'codes above
dim sfilestring, i
sfilestring = DoSearch(sourcefld, sExt)
s=split(sfilestring,vbcrlf)
Set FSO = CreateObject("Scripting.FileSystemObject")
For i=0 to ubound(s)
  filname = FSO.GetFileName(s(i))
  lngth = Len(filname)

  If s(i) <> "" Then
    If lngth > 23 Then
      MsgBox sourcefld & filname & " " & destfld1 & filname
      If FSO.FileExists(destfld1 & filname) Then
        FSO.DeleteFile destfld1 & filname
      End If
      FSO.MoveFile sourcefld & filname, destfld1 & filname
    else
      If FSO.FileExists(destfld2 & filname) Then
        FSO.DeleteFile destfld2 & filname
      End If
      FSO.MoveFile sourcefld & filname, destfld2 & filname
  End If
Next
Set FSO = nothing
'codes below
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top