Joe
Joe

Reputation: 13

Move multiple files within in a single Windows command

I have a folder which contains various tif files.I am using a simple move command which reads as

move "Source path/File name" "Destination path/" 

I have tried a command which reads as:

move "Source path/File 1, File 2. File 3" "Destination Path/"

I am looking for a similar formula which can help me out. The reason why this code is important for me

In a scenario where iam moving only one file to destination folder In another scenario, I have to choose 3 or more files to another destination folder. If I do this with generic formula it takes a longer time to accomplish.

Please do suggest on this.

move "Source path/File 1, File 2. File 3" "Destination Path/"

In a scenario where I am moving only one file to destination folder. In another scenario, I have to choose 3 or more files to another destination folder. Example: Sample

If I do this with generic formula it takes a longer time to accomplish.

Upvotes: 0

Views: 3632

Answers (1)

Variatus
Variatus

Reputation: 14383

You might try the code below.

Sub MoveFiles()

    Dim DestinationPath As String
    Dim SourcePath As String
    Dim FileNames As String
    Dim Sp() As String
    Dim i As Integer

    SourcePath = Environ("USERPROFILE") & "\Desktop"
    DestinationPath = "H:\TestFolder"
    FileNames = "File1.txt,File2.txt,File3.txt"

    If Right(SourcePath, 1) <> "\" Then SourcePath = SourcePath & "\"
    If Right(DestinationPath, 1) <> "\" Then DestinationPath = DestinationPath & "\"
    If Len(FileNames) Then
        Sp = Split(FileNames, ",")
        For i = 0 To UBound(Sp)
            Sp(i) = Trim(Sp(i))
            If Len(Dir(SourcePath & Sp(i))) Then
                Name SourcePath & Sp(i) As DestinationPath & Sp(i)
            End If
        Next i
    End If
End Sub

Set the Source and Destination paths according to your system. Enter as many or as few file names in one comma separated string. All named files will be moved if they exist at the SourcePath. If the destination path doesn't exist an error will occur.

Upvotes: 1

Related Questions