简体   繁体   中英

Moving files to a different directory

I'm having a nightmare trying to copy files from multiple directories into a single directory.

Basically I received a data set with individual png files in individual folders. What I want is one folder with all the png files.

The reason I want to do this with code is that the data set has approx. 6000 folders, so it is not practical to do it manually. This can be done with WinApi as I have no need to make this cross platform. I just want to merge the bloody folders.

So far I have managed to get the paths to the png files all saved into a .txt which are saved on each line.

Example:

C:/Users/rudwi/Documents/Visual_Studio_2017/Projects/ExtractPhotos/ExtractPhotos/lfw-deepfunneled/Abbas_Kiarostami/*.png

Just wondering is there a way I can move from the path above to a different dir.

The solution given by ANON I believe was for linux but it is pretty much the same using xCopy from the command line. Instead, just add /s before the source path to include everything in the directory. Hope this helps someone.

a script for cmd may look something like that:

set DESTDIR= C:\DestinationPath\
for /f "delims=" %%i in (filelist.txt) do (xcopy "%%i" %DESTDIR% /i /y )

Note, don't forget backslash at end of destination. of course, script can be parameterized:

set DESTDIR= %2
for /f "delims=" %%i in (%1) do (xcopy "%%i" %DESTDIR% /i /y )

put that into a mcopy.cmd file, and it can be used as this:

mcopy filelist.txt C:\\DestinationPath\\

It's easier to just create a .bat file of the commands to do a copy since you already have the source paths.

In your favorite editor do the following:

  1. Prepend 'cp ' to all of your lines.

  2. Append ' destination path' to all of your lines.

  3. Rename the file to .bat and run it from a cmd window.

As an example change your line above from

C:/Users/rudwi/Documents/Visual_Studio_2017/Projects/ExtractPhotos/ExtractPhotos/lfw-deepfunneled/Abbas_Kiarostami/*.png

to

cp C:/Users/rudwi/Documents/Visual_Studio_2017/Projects/ExtractPhotos/ExtractPhotos/lfw-deepfunneled/Abbas_Kiarostami/*.png C:/DestinationPath

When I had to do something like that and if I needed it done once quickly without writing any scripts I'd do this:

collect all png files from subdirs: dir /s /b *.png > copy.bat

Then modify your copy.bat by adding @echo off at the top and prepend each filename with call:mycopy and then at the bottom of copy.bat add:

goto:eof
:mycopy
copy %1 C:\mynewdir
exit /b 0

Not the best approach, but it's quick to do without really doing any scripting. Then when running that copy.bat I'd check output for errors for duplicate filenames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM