Copy 1 file from multiple subdirectories to another directory

Joined
Feb 12, 2012
Messages
5
Reaction score
0
I've searched Google and forums for days trying to figure out how to accomplish building a batch file that will parse a directory tree and copy 1 file from each subdirectory to a new folder.

I've got this as a lead:
https://www.pcreview.co.uk/forums/can-copy-file-multiple-folders-t1959548.html

and this...
http://stackoverflow.com/questions/1094921/dos-copy-the-first-file-in-a-directory

And I've tried this:
Code:
@ECHO OFF
CLS
PAUSE
FOR /R %%A in (C:\Users\Liam\Desktop\Master) DO (   
    COPY %%A  C:\Users\Liam\Desktop\Master\Master-Copies\
   GOTO :Exit
)
:Exit
Pause

The pauses are present because I'm trying to figure out exactly what is occurring.

Within my "Master" directory there are about 250 subdirectories. I'd like to copy a single file from each subdirectory and put them in a new subdirectory called "Master-Copies", which should ultimately have 250 files. The subdirectories and files don't have spaces in their names, however I'd like to build that functionality into the batch file in case I ever run into this situation again. I'd like to copy the first file in each subdirectory. I know from reading other forums that the "FIRST" file is relative, but I'd like it to be the first file sorted by name.

I know I'm asking a lot, and hopefully some genius here can help out a kid new to programming.

Thanks!
 
Joined
Feb 12, 2012
Messages
5
Reaction score
0
Link: http://stackoverflow.com/questions/...her-directory/9270681#comment11686048_9254868

I took the answer from dbenham and played with it for a while to come up with this:

Code:
@echo off
set name="Master"
setlocal
set root="C:\Users\Liam\Desktop\%name%\"
set dest="C:\Users\Liam\Desktop\%name%\%name%-Copies"
md %dest%
for /r %root% %%D in (.) do if "%%D" neq %dest% call :copyFirstFile "%%D"
pause
exit /b

:copyFirstFile
for %%F in ("%~1\*") do (copy "%%F" %dest%
exit /b)
This script will recursively parse the folder on my desktop called "Master", create a sub-directory called "Master-Copies", and copy the first file from all of the other subdirectories in "Master" to the newly created directory. So far this has been great, but occasionally file names are identical and they are overwritten.
So...how can I rename the copied file to have a prefix which is the original parent directory?


For example:


"file1.txt" in C:\Users\Liam\Desktop\Master\SubDirectory1\ gets copied to
C:\Users\Liam\Desktop\Master\Master-Copies\ as "SubDirectory1-file1.txt"
 
Joined
Feb 12, 2012
Messages
5
Reaction score
0
@SilverHazeSurfer

Thanks for your help. Currently the system is pulling over the "first" file in each subdirectory. I believe it is pulling the first based upon viewing the folder in alphabetical order sorted by name. So if the file names are 1.txt, 2.txt, 3.txt, etc., the system will copy 1.txt to the "Master-Copies" folder. So far I've gotten this to work, but the problem is if another subdirectory has 1.txt, then the previous written file is overwritten.

I've looked into
Code:
Copy /?
, and an option exists to suppress notification of overwriting files, which my batch code doesn't have, yet overwriting is still taking place.:confused:

So, ideally, during the copy, I'd like to have the file renamed with the prefix of the subdirectory it came from.

I'll bear with you as long as it takes.:D . I'm a rookie and you're a veteran.:bow:
 
Joined
Feb 12, 2012
Messages
5
Reaction score
0
I have figured out how to get the parent directory name via:

Code:
@echo off
::setlocal enableextensions,enabledelayedexpansion
SET CDIR=%~p0
SET CDIR=%CDIR:~1,-1%
SET CDIR=%CDIR:\=,%
SET CDIR=%CDIR: =_%
FOR %%a IN (%CDIR%) DO SET "CNAME=%%a"
SET CNAME=%CNAME:_= %
echo %CNAME%
However I don't know how to append it to the current file name. I have looked through the
Code:
REN
and
Code:
RENAME
parameters but can't figure it out. Any help would be greatly appreciated. Thanks!
 

Silverhazesurfer

Master of Logic
Joined
Oct 3, 2008
Messages
1,068
Reaction score
42
I'm out of options. As I am not a programmer, I cannot comment on the syntax. I can only recommend commands you can call.

Logically, setting a function that would reference the filename.ext and append a prefix to the name. Again, since I am not a code-head, I am not certain of the syntax or wildcard characters needed to get that done.

That's about all I got. I hope that at least some of this can spur a google search for you. I know that talking through something helps me, even if the "help" doesn't understand what it is i'm actually trying to do.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top