• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1005
  • Last Modified:

Batch script to move files from multiple folders to a single folder

Hi,

I have multiple folders with 2 different file extensions: .txt & .key (but the file names are unique)

Here is the structure:

Folder1
file_x.txt
file_y.txt
file_z.txt
Sub-Folder1
file1.txt
file2.txt
file3.key
file4.key

Sub-Folder2
file1a.txt
file2b.txt
file3c.key
file4d.key


Folder2
Sub-Folder1
file5.txt
file6.txt
file7.key
file8.key

Folder3
Sub-Folder1
file9.txt
file10.txt
file11.key
file12.key

I am looking for a script which will move all these files based on the file extension to 2 different folder:

Folder_key (which will contain all .key files)
Folder_txt (which will contain all .txt files)
0
nainil
Asked:
nainil
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
Steve KnightIT ConsultancyCommented:
This should do it.. not chance to check but should be OK.  Try it on a test structure first of course.

Steve

@Echo off
set key=C:\folder_key
set txt=c:\folder_txt
cd /d c:\startdir
for /r %%f in (*.txt) do copy /y "%%~f" "%txt%"
for /r %%f in (*.key) do copy /y "%%~f" "%key%"
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
I had this working: (cd in start folder first)

for /f %%a in ('dir /a /s /b *.key') do move "%%a" folder_key
for /f %%a in ('dir /a /s /b *.txt') do move "%%a" folder_txt

@steve: change your copy to move instead :D
0
 
Steve KnightIT ConsultancyCommented:
Good point there, I didn't want him to move all the files from his drive having started in the wrong dir by mistake so thought copy was safest to start with, should have said though!

If they are unique names then you shouldn't need the /y on copy, but you can also add /y to the move command if any filenames ARE duplicates in which case the last copy/moved file will "win".

Steve
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Paul TomasiCommented:
Back on this one again eh?....

Are Folder1, Folder2 and Folder3 subfolders of another folder or do they appear in the root of the drive along with other folders?

Using 'C:\' as an example:

C:\
    Folder1
        Sub-Folder1
        Sub-Folder2
    Folder2
        Sub-Folder1
    Folder3
        Sub-Folder1
    Other-folders
(that you DON'T want to process)


Or...

C:\
    ParentFolder
(Contains ONLY folders that you DO want to process)
        Folder1
            Sub-Folder1
            Sub-Folder2
        Folder2
            Sub-Folder1
        Folder3
            Sub-Folder1
0
 
nainilAuthor Commented:
@paultomasi

This is the correct structure:

C:\
    ParentFolder (Contains ONLY folders that I do want to process)
        Folder1
            Sub-Folder1
            Sub-Folder2
        Folder2
            Sub-Folder1
        Folder3
            Sub-Folder1
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
>> C:\
>>    ParentFolder (Contains ONLY folders that I do want to process)

And where are the Folder_key and Folder_txt subfolders? Those are missing in your correct structure.
0
 
nainilAuthor Commented:
@gerwinjansen

The Folder_txt & Folder_key are outside the current structure:

c:\Folder_key
c:\Folder_txt
0
 
Steve KnightIT ConsultancyCommented:
So have you tried gerwinjansen or my suggestions then?
0
 
Steve KnightIT ConsultancyCommented:
I guess that was a "yes".
0
 
nainilAuthor Commented:
Yes. I tried your solution first where I had to change the copy to move and then I tried gerwinjansen's suggestion which worked perfect as well. Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now