Message Boards Message Boards

0
|
4607 Views
|
10 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How copy files to another directory

Posted 11 years ago
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;
POSTED BY: Marcelo De Cicco
10 Replies
Innitially I thought the same but, it showed error : not recognized as string..etc..
So I started to play out  the alternatives.

Salute!
POSTED BY: Marcelo De Cicco
FileNames[] always returns strings.  It may be a little confusing that the quotes are not shown in the output.
POSTED BY: Szabolcs Horvát
Yes!

thanks!
POSTED BY: Marcelo De Cicco
Marcelo,

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

Luc
POSTED BY: Luc Barthelet
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. >>
POSTED BY: Marcelo De Cicco
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,
POSTED BY: Marcelo De Cicco
Luc,

Even tough dir already existed, I did what  you suggested see  the error below:
CreateDirectory::filex: C:\Users\Marcelo\Pictures\PTMIMAG\ already exists. >>
POSTED BY: Marcelo De Cicco
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
POSTED BY: Luc Barthelet
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?
POSTED BY: Marcelo De Cicco
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
POSTED BY: Luc Barthelet
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract