Page 1 of 1

Batch File to Move/Del Files of Specific Extension

Posted: 13 May 2013 16:25
by Benjabeanz
I apologize if this topic seems a little basic, but for the life of me, I can not get such a seemingly simple task to work!

Here's the scenario:

I have a folder WITH SPACES in its name. We'll call this folder "D:\Source Folder With Spaces." This folder also has many different sub-folders. Within these subfolders, I have files of different file extensions, let's say just .dat and .exe files. I am trying to write a batch file that will search through "Source Folder With Spaces" and ALL of it's sub-folders for .exe files and move OR del (I'd be happy with either outcome) ONLY .exe files (regardless of file name) to another folder location. Also, some of the files that need to be moved/deleted may even contain spaces in their names. Now, if "D:\Source Folder With Spaces" were only named "D:\Source," I would very easily be able to tackle this task with the following code:

Code: Select all

FOR /R D:\Source %%f in (*.mp3) DO MOVE %%f "D:\Destination


As soon as I add a space to the folder name, though, like so:

Code: Select all

FOR /R "D:\Source Folder With Spaces" %%f in (*.mp3) DO MOVE %%f "D:\Destination Folder That Might Also Have Spaces In Name"


It apparently sees these spaces as such a predicament, such a burden, that the batch file does nothing more when opened than opening and immediately closing with no action whatsoever taken in between. I've done exhaustive amounts of research to find virtually no answer to this confounded puzzle.

NOTE: PLEASE do not respond by simply saying, "Just rename the folders so they don't contain spaces in the names," because if that were an okay solution for me, well, then I wouldn't be here posting.

OS Info: I'm running Windows 7 64-bit

Re: Batch File to Move/Del Files of Specific Extension

Posted: 13 May 2013 16:51
by foxidrive
These should work.

Code: Select all

FOR /R "D:\Source" %%f in (*.mp3) DO MOVE "%%f" "D:\Destination"


Code: Select all

FOR /R "D:\Source" %%f in (*.mp3) DO del "%%f"

Re: Batch File to Move/Del Files of Specific Extension

Posted: 14 May 2013 01:21
by psychoid69

Code: Select all

FOR /R "D:\111 222 333" %%f in (*.mp3) DO MOVE "%%f" "D:\444 555 666"


Worked for me.

Re: Batch File to Move/Del Files of Specific Extension

Posted: 14 May 2013 04:23
by !k

Code: Select all

for /f "delims=" %%f in ('dir /b/s/a-d "D:\111 222 333\*.mp3"') do move "%%f" "D:\444 555 666"

Processing files depending on extensions http://forum.wincmd.ru/viewtopic.php?t=13951 (on Russian)
http://translate.google.com/translate?sl=ru&tl=en&ie=UTF-8&u=forum.wincmd.ru%2Fviewtopic.php%3Ft%3D13951 (Googlenglish)
"%L" is file with list of folders like

Code: Select all

d:\path\
e:\path 2\path 3\

Re: Batch File to Move/Del Files of Specific Extension

Posted: 14 May 2013 05:48
by Benjabeanz
psychoid69 wrote:

Code: Select all

FOR /R "D:\111 222 333" %%f in (*.mp3) DO MOVE "%%f" "D:\444 555 666"


Worked for me.


@psychoid69 WOW! Thank you! All I was missing was a simple set of quotations around the %%f! I knew I was overlooking something so unbelievably simple. Thank you everyone for all of your help!