Batch File to Move/Del Files of Specific Extension

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Benjabeanz
Posts: 3
Joined: 12 May 2013 07:50

Batch File to Move/Del Files of Specific Extension

#1 Post by Benjabeanz » 13 May 2013 16:25

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#2 Post by foxidrive » 13 May 2013 16:51

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"

psychoid69
Posts: 18
Joined: 02 May 2013 06:09

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

#3 Post by psychoid69 » 14 May 2013 01:21

Code: Select all

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


Worked for me.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#4 Post by !k » 14 May 2013 04:23

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\

Benjabeanz
Posts: 3
Joined: 12 May 2013 07:50

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

#5 Post by Benjabeanz » 14 May 2013 05:48

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!

Post Reply