How Can I Move Files Based on Their File Extension?

How Can I Move Files Based on Their File Extension?

  • Comments 7
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a folder with a bunch of files in it. I need to move all those files; the only problem is that the files need to be moved to different folders depending on their file extensions. For example, I want all the .log files to go here, all the .bak files to go there, etc. How can I do that using a script?

-- SH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SH. There are at least three ways to do this that we know of: you can use WMI; you can use the FileSystemObject; or you can use the Shell object. Is any one method better than the other? Well, ultimately all three will accomplish the task; the one advantage WMI has over the other two is that WMI can accomplish this task just as easily on remote computers as it can on the local computer. Of course, the one disadvantage WMI has is that it doesn’t actually have a built-in method for moving files. But, hey, we’ve never let a little thing like that stop us before, have we?

Let’s take a look at a script that retrieves a list of all the files in the C:\Payroll folder, and then moves any file with a .log file extension to the folder D:\Operation Logs:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Payroll'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile in colFiles
    If objFile.Extension = "log" Then
        strCopy = "D:\Operation Logs\" & objFile.FileName _
            & "." & objFile.Extension
        objFile.Copy(strCopy)
        objFile.Delete
    End If
Next

Important. If you run this script, make sure you use valid paths. Suppose you try copying files to D:\Operation Logs, only you don’t actually have a D drive on your machine. In that case, the Copy command will fail, but the Delete command will succeed. Thus your file will be deleted without being copied anywhere.

We begin by connecting to the WMI service, then use an ASSOCIATORS OF query to retrieve all the files found in the folder C:\Payroll. We then walk through the collection of files, checking to see if any of the files have the file extension log. That’s what we do here:

If objFile.Extension = "log" Then

Notice that we’re not looking for files with a .log file extension; make sure you leave the dot (.) out.

And what happens if we find a file that has a log file extension? Well, as we mentioned earlier, WMI doesn’t have a built-in method for moving files. We’re going to work around this by copying the file from the C:\Payroll folder to the D:\Operation Logs folder. For a brief moment we’ll have two copies of the file: one in C:\Payroll and one in D:\Operation Logs. But don’t worry: in the very next line of code we’ll delete the copy found in C:\Payroll. When we do that we’ll have only a single copy of the file, the one found in D:\Operation Logs. In effect, we’ve “moved” the file even though we never used a Move method of any kind.

Yes, very sneaky.

The only tricky thing here is that WMI requires a complete path name when copying a file. Suppose we find a file named March.log. To copy this file we need to use the complete path: D:\Operation Logs\March.log. So we use this line of code to construct that path:

strCopy = "D:\Operation Logs\" & objFile.FileName _
    & "." & objFile.Extension

All we’re doing here is assigning a value to the variable strCopy. That value happens to consist of D:\Operation Logs\ plus the name of the file (March) plus a period (.) plus the file extension (log). Put them all together and it spells D:\Operation Logs\March.log. And that’s exactly the path we need to pass to the Copy method.

And, yes, as you probably noticed, the file extension is a separate property from the file name. That’s why we have to combine the FileName property plus a period plus the Extension property. It’s a bit of a hassle in this script, but it does make it easier to write other scripts, like a script that returns a list of all the files on a computer that have a log file extension.

As soon as we’ve constructed the path we can copy the file to drive D and then delete the original file from drive C. That’s what we do in these two lines of code:

objFile.Copy(strCopy)
objFile.Delete

And that’s it: the script loops around until it has checked each file in the folder, and until it has moved all the files with a log file extension to D:\Operation Logs.

In order to keep the sample script as short as possible, we check for only a single file extension. However, it’s easy to check for additional file extensions and to move those files to the appropriate folders. Here’s a revised script that also checks for .bak files and copies them to D:\Operation Backups:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Payroll'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile in colFiles
    If objFile.Extension = "log" Then
        strCopy = "D:\Operation Logs\" & objFile.FileName _
            & "." & objFile.Extension
        objFile.Copy(strCopy)
        objFile.Delete
    End If
    If objFile.Extension = "bak" Then
        strCopy = "D:\Operation Backups\" & objFile.FileName _
            & "." & objFile.Extension
        objFile.Copy(strCopy)
        objFile.Delete
    End If
Next

If you want to check for even more file extensions, then just add some additional If-Then statements. That’s all there is to it.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • hey scripting guy. I need a script that scans the my music folder on win 7 for all .m4a files and then moves them to an empty folder on my desktop. Can you help me?

  • Hugo, Unless there are further requirement that I am missing you should be able to use this very same script, just with no need to check for the second file type.

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colFiles = objWMIService.ExecQuery _

       ("ASSOCIATORS OF {Win32_Directory.Name='C:\Users\Hugo\Music'} Where " _

           & "ResultClass = CIM_DataFile")

    For Each objFile in colFiles

       If objFile.Extension = "m4a" Then

           strCopy = "C:\Users\Hugo\Desktop\" & objFile.FileName _

               & "." & objFile.Extension

           objFile.Copy(strCopy)

           objFile.Delete

       End If

    Next

  • If i want to call this script via a scheduled task what you should i name the file that the script is stored in?

  • File Mover is a small application designed to move or copy a group of files, based on the file extension, from one directory to another. It will also allow you to delete empty directories afterwards.

    www.techfeb.com/file-mover-copy-or-move-files-with-specific-extension-in-windows

  • I know this is really old but I want to do this and I keep getting error 424. I want to run the script from computer 1. Move *.asc from a remote computer 2 (V) to another remote computer 3 (T). Computer 1 is has mapped drives to both drives.

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='V:\ceridian-ach'} Where " & "ResultClass = CIM_DataFile")

    For Each objFile in colFiles
    If objFile.Extension = "asc" Then
    strCopy = "T:\CashMGMT\Ceridian\" & objFile.FileName & "." & objFile.Extension
    objFile.Copy(strCopy)
    objFile.Delete
    End If
    Next

    IF you believe the issue is elsewhere in script I am happy to paste all of it.

  • Read this blog post and tried it, but ended up using the .bat file as de described in this video: https://www.youtube.com/watch?v=Pkz8jQYGcd8 instead. In my opinion a bit easier to follow than the method described here.

  • If i want to move zero bytes files instead of log files means how to edit this code, please help me.