Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 999
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
paultomasiCommented:
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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