How To Move Files With A Batch File

The other day I was helping a friend with a reporting tool he was using. He mentioned that the application was really slow when loading data. The application he was running had log files that where populated into a directory. When the application would try and grab new data it had to go through all the files.


We wanted to keep the older files so if he ever needed to make change or apply a filters to the data it could be reloaded. We decided to move the old files that where not needed to a new location. That way he could go back and grab the data but it would no effect the load time. We decided to move all files older then this year to a different directory. Instead of manually moving all the files we just created a batch file to do this in one click of a button. The application he used had the date the file was created in the name. This really made things very easy.


In this example I have setup two folders called original and new folder. We will move sample text files from the original folder to the new folder. In this example I will show you the code I used. I then will break it down and explain what each part does.



Below you can see are example directory we will be moving the files from.



Orginal Files Example



Let’s create a batch file to move these files

Launch Notepad

Start > All Programs > Accessories > Notepad



Start > All Programs > Accessories > Notepad



Copy and paste the code below into the new document


move /-y "D:\example\original\*2007*.txt" "D:\example\New folder\"



Your file should now look like this.(click on image see it larger)





we will need to save it as a batch file.


- Select File > Save as

- Select the location you would like to save the file

- Name the file and add “.bat” extension at the end (with out the “ “)

(Note: You may want to change the encoding to All Files)



File Save: NameofFile.bat (Note: Change Encoding to All Files)



You should now have a file that looks like this.




What the Launch Icon should look like



Let’s now take a look at what the code does.

The fist part is defining what action is going to be executed. Since we are moving the files we are using move. We could also use copy if we just wanted to copy the files.



Move - Moves the Files, You could also use copy if you don't want to move the file



The Next part is the /-Y. This is saying that you want to be prompted if there are duplicates. If you don’t care about files being overwritten in the new folder and don’t want to be prompted you can use /Y.



-Y Yes Overwrite



Explanation on -y and -Y



This part we are defining the file(s) we will be moving. So in this example this will be any file that has 2007 and .txt in its name. You will need to make sure you have the full path with “ “ around it.



The Path with Excapes



This is where we will be moving the file to. Once again make sure you have the “ “ around the full path if you have any spaces in the folder names.



Define the Destination



The last part is to add a pause. Once the file executes you will be able to see if it completed successful as well as what files where moved.



Add a Pause if you would like to see all the actions that have been taken. (Nice for troubleshooting)



Now to run the batch file you just double click on it to execute it.



Double click your new batch file to execute it

We should now see what files it has moved.



File that have been moved



This is an example of what it would look like if It didn’t run successful.



Error Example



If you need to make changes to your batch file right click on it and select edit.



Right Click and click edit



If you enjoyed this post, please share it on your favorite social network by clicking on the “Share / Save” bar below.