How copy files to another directory

I have been trying to move/copy files (images, in fact) from one directory to another. I am avoiding the use of windows picture manager, truly limited for my purposes. Well, first I have to choose the files types, then I try some copy code and another things but nothing works. Seems that when I use FileNames a list is generated but, it is not strings elements ( why?), so I tried to transform each element (files. something) in a string.

I attached the code.

In[31]:= ClearSystemCache[];
ClearAll["Global`*"];
<< Utilities`CleanSlate`
ClearInOut[];
ResetDirectory[];

In[155]:= SetDirectory["C:\\Users\\Marcelo\\Dropbox\\astronomia"]

Out[155]= C:\Users\Marcelo\Dropbox\astronomia

(*setting the files I want*)

In[256]:= 
imagconjunto = 
  FileNames[{"*.bmp", "*.col", ".*col.b", "*.gv", "*.dot", "*.gif", "*.g6", 
      "*.gml", "*.graphml", "*.grb", ".*grib", ".*gxl", "*.jpeg", "*.jpg", 
      "*.jp2", "*.j2k", ".*jvx", "*.png", "*.pict", "*.ppm", "*.tiff", 
      "*.tif", "*.vtk"}, {"C:\\Users\\Marcelo\\Dropbox\\astronomia"}, 
     Infinity] // InputForm // ToString;
imagconjunto1 =
  ToString[#] & /@ FileNameTake[#] & /@ 
   FileNames[{"*.bmp", "*.col", ".*col.b", "*.gv", "*.dot", "*.gif", "*.g6", 
     "*.gml", "*.graphml", "*.grb", ".*grib", ".*gxl", "*.jpeg", "*.jpg", 
     "*.jp2", "*.j2k", ".*jvx", "*.png", "*.pict", "*.ppm", "*.tiff", "*.tif",
      "*.vtk"}, {"C:\\Users\\Marcelo\\Dropbox\\astronomia"}, Infinity];
imagconjunto2 = 
  ToString[#] & /@ 
       FileNameJoin[{"C:\\Users\\Marcelo\\Pictures\\PTMIMAG\\", #}] & /@ 
     imagconjunto1 // InputForm // ToString;

(*Setting the new directory where I want put the files.something*)

In[253]:= SetDirectory["C:\\Users\\Marcelo\\Pictures\\PTMIMAG\\"]

Out[253]= C:\Users\Marcelo\Pictures\PTMIMAG

In[255]:= CopyFile[#, FileNameJoin[{"C:\\Users\\Marcelo\\Pictures\\PTMIMAG\\", #}]] & /@
   imagconjunto;

Marcelo,

CopyDirectory will copy all the files.

If you want to filter the files, the basic syntax is simply:

CopyFile[#,StringReplace[#,sourceFolder->destinationFolder]]& /@  Filenames[..]

Luc

Hi Luc,

thanks for your help.

I tried the algor., but did not work:

CopyFile::nffil: File not found during CopyFile[C:\Users\Marcelo\Dropbox\astronomia\astrofot\COMPUTERIZE YOUR DOBSONIAN TELESCOPE_arquivos\altdrive.jpg,C:\Users\Marcelo\Pictures\PTMIMAG\astrofot\COMPUTERIZE YOUR DOBSONIAN TELESCOPE_arquivos\altdrive.jpg]. >>

Something related to path I believe. Or the copy command has problems with subdirs?

Marcelo,

I would expect this to work:

CreateDirectory["C:\\Users\\Marcelo\\Pictures\\PTMIMAG"];
CopyFile[#,StringReplace[#,"C:\\Users\\Marcelo\\Dropbox\\astronomia"-> "C:\\Users\\Marcelo\\Pictures\\PTMIMAG"]]&/@FileNames[{"*.bmp","*.col",".*col.b","*.gv","*.dot","*.gif","*.g6", "*.gml","*.graphml","*.grb",".*grib",".*gxl","*.jpeg","*.jpg", "*.jp2","*.j2k",".*jvx","*.png","*.pict","*.ppm","*.tiff","*.tif", "*.vtk"},{"C:\\Users\\Marcelo\\Dropbox\\astronomia"},Infinity]

Luc

Luc,

Even tough dir already existed, I did what you suggested see the error below:

CreateDirectory::filex: C:\Users\Marcelo\Pictures\PTMIMAG\ already exists. >>

Luc,

I think your algor is doing the following:

it takes the whole set of files.* and transform it in a new set with dirs: …\\PTMIMAG, and then it tries to convert the old set:…\\astronomia\\… to this former, but it does no longer exist. (i tried also set delay )

Now I have done some changes:

and at least it does not show error for path:

imagconjunto = 
  FileNames[{"*.bmp", "*.col", ".*col.b", "*.gv", "*.dot", "*.gif", 
      "*.g6", "*.gml", "*.graphml", "*.grb", ".*grib", ".*gxl", 
      "*.jpeg", "*.jpg", "*.jp2", "*.j2k", ".*jvx", "*.png", "*.pict",
       "*.ppm", "*.tiff", "*.tif", 
      "*.vtk"}, {"C:\\Users\\Marcelo\\Dropbox\\astronomia"}, 
     Infinity] // InputForm // ToString;

CopyFile[imagconjunto, 
StringReplace[#,
"C:\\Users\\Marcelo\\Dropbox\\astronomia\\" -> 
     "C:\\Users\\Marcelo\\Pictures\\PTMIMAG\\"]] & /@ 
 FileNames[{"*.bmp", "*.col", ".*col.b", "*.gv", "*.dot", "*.gif", 
   "*.g6", "*.gml", "*.graphml", "*.grb", ".*grib", ".*gxl", 
   "*.jpeg", "*.jpg", "*.jp2", "*.j2k", ".*jvx", "*.png", "*.pict", 
   "*.ppm", "*.tiff", "*.tif", 
   "*.vtk"}, {"C:\\Users\\Marcelo\\Dropbox\\astronomia"}, Infinity]

$Failed, $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, \
$Failed, $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, \
$Failed, $Failed, $Failed, $Failed, $Failed,

Finally!

Very ugly.. but it worked..

CopyFile[#,"C:\\Users\\Marcelo\\Pictures\\PTMIMAG\\"<>FileNameTake[#]]&/@ FileNames[{"*.bmp","*.col", ".*col.b","*.gv", "*.dot","*.gif","*.g6","*.gml","*.graphml","*.grb", ".*grib",".*gxl","*.jpeg", "*.jpg","*.jp2","*.j2k",".*jvx","*.png","*.pict","*.ppm","*.tiff", "*.tif","*.vtk"},{"C:\\Users\\Marcelo\\Dropbox\\astronomia"},Infinity]//InputForm//ToString ;

Error:

CopyFile::filex: Cannot overwrite existing file C:\Users\Marcelo\Pictures\PTMIMAG\altdrive.jpg. >>
CopyFile::filex: Cannot overwrite existing file C:\Users\Marcelo\Pictures\PTMIMAG\azdrive.jpg. >>
CopyFile::filex: Cannot overwrite existing file C:\Users\Marcelo\Pictures\PTMIMAG\circuit.gif. >>
General::stop: Further output of CopyFile::filex will be suppressed during this calculation. >>

Marcelo,

You don’t need the //InputForm //ToString

Luc

Yes!

thanks!

FileNames[] always returns strings. It may be a little confusing that the quotes are not shown in the output.

Innitially I thought the same but, it showed error : not recognized as string..etc..
So I started to play out the alternatives.

Salute!