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