How do I copy files in a deep structure to a flat structure?

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

JBNielsen
Posts: 267
Joined: Thu Jul 07, 2011 10:31 pm
Location: Denmark

How do I copy files in a deep structure to a flat structure?

Post by JBNielsen »

I have this file structure:

Code: Select all

dir-player1
   dir-tournament1
      game1.pgn
      game2.pgn
      game3.pgn
   dir-tournament2
      game1.pgn
      game2.pgn
dir-player2
   game1.pgn
dir-player3
   dir-tournament1
      game1.pgn
      game2.pgn
      game3.pgn
   dir-tournament2
      game1.pgn
      game2.pgn
.....
....
...
How do I (in windows) copy all these games into a single directory?
(I have earlier had this need for text-files and pictures)

And when they are copied to a single directory:
How do I concatenate these files into a single file?
(many years ago I could do that in DOS)
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: How do I copy files in a deep structure to a flat struct

Post by Sven »

To copy all *.pgn in the current tree to some target directory and overwrite identically named files:

Code: Select all

@echo off
set targetDir=whatever
for /R %f in (*.pgn) do copy /Y %f %targetDir%
To concatenate all *.pgn in the current tree to one target output file:

Code: Select all

@echo off
set targetFile=whatever
for /R %f in (*.pgn) do type %f >>%targetFile%
Sven
JBNielsen
Posts: 267
Joined: Thu Jul 07, 2011 10:31 pm
Location: Denmark

Re: How do I copy files in a deep structure to a flat struct

Post by JBNielsen »

Sven Schüle wrote:To copy all *.pgn in the current tree to some target directory and overwrite identically named files:

Code: Select all

@echo off
set targetDir=whatever
for /R %f in (*.pgn) do copy /Y %f %targetDir%
To concatenate all *.pgn in the current tree to one target output file:

Code: Select all

@echo off
set targetFile=whatever
for /R %f in (*.pgn) do type %f >>%targetFile%
Sven
Oh, thanks! :D

Very fast answer! 8-)

Just to be sure - do I make a bat-file with these commands, or is it better done in another way?
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: How do I copy files in a deep structure to a flat struct

Post by lucasart »

The proper way is indeed to do a for loop. And the program type writes the input file on stdout, which is then redirected into the end of the target file, so as to accumulate everything in it. If anyone is interested, the equivalent in Linux would be

Code: Select all

find ./dir/ -type f -iname "*.pgn" -exec cat {} >> ./target.pgn \;
But you can also solve the first problem the dumb way, not involving any command line at all. Just type Windows+F to open up the Find program. And find the files matching *.pgn in the specified folder (incl. sub-folders). Once files have been found just select them (Ctrl+A) and use copy/paste or drag/drop to put them uin the folder of your choice.
Now for the second problem (file concatenation), I can't think of a "dumb way" for that. Really it has to be command line.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: How do I copy files in a deep structure to a flat struct

Post by Sven »

JBNielsen wrote:Just to be sure - do I make a bat-file with these commands, or is it better done in another way?
I would put it into a .bat file. (No, I would use another script language like Perl or Shell but that's another issue ;-) )

The first version might take targetDir as parameter.

The second version might take no parameters at all if you remove the ">>%targetFile%" part, then you could simply call the .bat script and optionally append any output redirection as you like directly on the command line, in this case ">whatever" would be sufficient. In my example the ">>" was needed since that occurs within the loop and a single ">" would cause multiple overwriting of the output file with each new file, instead of concatenating.

Sven