简体   繁体   中英

VBscript to move files from one directory to another

I'm new to VBscript and would like some help on moving a specific number of files from one directory to another.

Right now I currently have a set of 3 files(for testing purposes) with different extensions and would like to move all of the files if there are exactly 3 files(no more and no less) inside the source directory. My code so far is:

Set fso=CreateObject("Scripting.FileSystemObject")

Set fldr=fso.getFolder("C:\folderA")
for each file in fldr.files
    If fldr.files < 3 then
            msgbox("Need more files")
    Else
            fso.MoveFile "C:\folderA\*", "C:\folderB"
    End If

Any help would be greatly appreciated. Thanks, everyone!

  1. Use fso.getFolder("C:\\folderA").Files.Count to get the number of files in C:\\folderA .
  2. Do this before - not in - a loop over the files (to move them)
  3. Consider to move (and re-create) the folder if it contains exactly 3 files
Set FSO=CreateObject("Scripting.FileSystemObject")
Set fldr= FSO.GetFolder("C:\FolderA")
Set Collec_Files= fldr.Files
For Each File in Collec_Files
    If Collec_Files.count < 3 then
      msgbox("Need more files")
    Else
      FSO.MoveFile "C:\FolderA\*", "C:\FolderB"
    End If
Next

I did the following code and it is working just fine. It is moving for the other 3 different folders looking at the file's name for it and type.

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(rootDir)
For each file in f.files
    filename = folderName & file.name

    extFile = right(file.Name, 4) 'gets the file extension .xls

    strExport = left(file.Name,6)  

    strExportAPI = left(file.Name, 3)

    Do

        If (extFile <> ".vbs") Then 
        
            strsub = "KeyWord"
            index = InStr(file.name, strsub)
        
        End If  
                    
        If (strExportAPI = "Value") Then 
            fs.CopyFile file, dirKeyWord&file.name
            fs.MoveFile file, dirKeyWord&file.name
            Exit Do             
        End if
    
        If (index > 0) Then
            fs.CopyFile file, dirKeyWord&file.name
            fs.MoveFile file, dirKeyWord&file.name
            Exit Do
        End If
    
    
        Do while (strExport = "export")
            fs.MoveFile file, dirKey3&file.name
            Exit Do 
        Loop            
    Loop While False
Next

Tai

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM