Move All Files in a Folder to Another

Move all files in a directory to a new directory

File.Move requires a destination file not just a destination folder.
You need to decide how to name the moved file in the destination folder. It could be the same name of the source file or another name of your choice. Path.GetFilename could be of help if you want to maintain the old name.

Notice also that EnumerateFiles is better than GetFiles because allows you to start the moving action without loading all the files names in memory first.

try
{
string dest = @"C:\Users\Billeh\Desktop\Test";
foreach (var file in Directory.EnumerateFiles(@"C:\Users\Billeh\Desktop\"))
{
string destFile = Path.Combine(dest, Path.GetFileName(file))
if(!File.Exists(destFile))
File.Move(file, destFile);

}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}

Consider also that File.Move cannot overwrite an existing file in the target folder. If the file exists an IOException is raised. So, if you want to overwrite you need to Delete the destination file before but, in any case, you need to add a check with File.Exists

Moving all files from one directory to another using Python

Try this:

import shutil
import os

source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'

file_names = os.listdir(source_dir)

for file_name in file_names:
shutil.move(os.path.join(source_dir, file_name), target_dir)

Copy or move all files in a directory regardles of folder depth or number

You can do it like this:

find /absolute/path/to/Pictures -type f -name '*.png' -exec mv -i {} /absolute/path/to/results  \;

Another option is to use xargs

find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results

How can I move all the files from one folder to another using the command line?

You can use move for this. The documentation from help move states:

Moves files and renames files and directories.

To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file. Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.

/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.

See the following transcript for an example where it initially shows the qq1 and qq2 directories as having three and no files respectively. Then, we do the move and we find that the three files have been moved from qq1 to qq2 as expected.

C:\Documents and Settings\Pax\My Documents>dir qq1
Volume in drive C is Primary
Volume Serial Number is 04F7-0E7B

Directory of C:\Documents and Settings\Pax\My Documents\qq1

20/01/2011 11:36 AM <DIR> .
20/01/2011 11:36 AM <DIR> ..
20/01/2011 11:36 AM 13 xx1
20/01/2011 11:36 AM 13 xx2
20/01/2011 11:36 AM 13 xx3
3 File(s) 39 bytes
2 Dir(s) 20,092,547,072 bytes free

C:\Documents and Settings\Pax\My Documents>dir qq2
Volume in drive C is Primary
Volume Serial Number is 04F7-0E7B

Directory of C:\Documents and Settings\Pax\My Documents\qq2

20/01/2011 11:36 AM <DIR> .
20/01/2011 11:36 AM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 20,092,547,072 bytes free

 

C:\Documents and Settings\Pax\My Documents>move qq1\* qq2
C:\Documents and Settings\Pax\My Documents\qq1\xx1
C:\Documents and Settings\Pax\My Documents\qq1\xx2
C:\Documents and Settings\Pax\My Documents\qq1\xx3

 

C:\Documents and Settings\Pax\My Documents>dir qq1
Volume in drive C is Primary
Volume Serial Number is 04F7-0E7B

Directory of C:\Documents and Settings\Pax\My Documents\qq1

20/01/2011 11:37 AM <DIR> .
20/01/2011 11:37 AM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 20,092,547,072 bytes free

C:\Documents and Settings\Pax\My Documents>dir qq2
Volume in drive C is Primary
Volume Serial Number is 04F7-0E7B

Directory of C:\Documents and Settings\Pax\My Documents\qq2

20/01/2011 11:37 AM <DIR> .
20/01/2011 11:37 AM <DIR> ..
20/01/2011 11:36 AM 13 xx1
20/01/2011 11:36 AM 13 xx2
20/01/2011 11:36 AM 13 xx3
3 File(s) 39 bytes
2 Dir(s) 20,092,547,072 bytes free

How to move files from one folder to another in R?

Assuming you have a list of the names you want to copy and the destination folder already exists:

# vector with the 100 files names to be copied
names <- c("text1.txt", "text2.txt")

# custom function
my_function <- function(x){
file.rename( from = file.path("yourpath/folder1", x) ,
to = file.path("yourpath/folder3", x) )
}

# apply the function to all files
lapply(names, my_function)

Note that rename actually deletes the files in the from folder. If you do not want that you can use file.copy

move all files and folders in a folder to another?

This is what i use

   // Function to remove folders and files 
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..") rrmdir("$dir/$file");
rmdir($dir);
}
else if (file_exists($dir)) unlink($dir);
}

// Function to Copy folders and files
function rcopy($src, $dst) {
if (file_exists ( $dst ))
rrmdir ( $dst );
if (is_dir ( $src )) {
mkdir ( $dst );
$files = scandir ( $src );
foreach ( $files as $file )
if ($file != "." && $file != "..")
rcopy ( "$src/$file", "$dst/$file" );
} else if (file_exists ( $src ))
copy ( $src, $dst );
}

Usage

    rcopy($source , $destination );

Another example without deleting destination file or folder

    function recurse_copy($src,$dst) { 
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}

Please See: http://php.net/manual/en/function.copy.php for more juicy examples

Thanks
:)

How to move all files and folder from one folder to another of S3 bucket in php with some short way?

Is there any other short way to do the same

There is no short way to do this.

Here is the reason:

The concept of folders is unique to the console. Amazon S3 uses buckets and objects, but the service does not natively support folders, nor does it provide any API to work with folders.

To help you organize your data, however, the Amazon S3 console supports the concept of folders. [...]

Important

In Amazon S3, you create buckets and store objects. The service does not support any hierarchy that you see in a typical file system.

The console uses the object key names to derive the folder hierarchy. It uses the "/" character in the key name to infer hierarchy

http://docs.aws.amazon.com/AmazonS3/latest/UG/about-using-console.html

"Moving" files to another "folder," in S3, cannot be done, in reality. It can only be emulated, by making copies of each individual object, giving each object a new key name to the new "hierarchy," then deleting the original. S3 does not even support renaming an individual object. Renaming is also accomplished by making a copy with the new name, then removing the original. The console gives the appearance of supporting these operations, but it actually only emulates them, as described above. This is a deliberate part of the design of S3.

Moving Files From One Folder To Another VB.Net

I know why IdleMind recommended the approach they did, but it would probably make for a bit more readable code to just list out the file names:


Imports System.IO

...

Dim result = FolderBrowserImport.ShowDialog()

If result <> DialogResult.OK Then Exit Sub

For Each s as String in {"settings.txt", "settings2.txt", "settings3.txt", "settings4.txt", "settings5.txt" }

File.Copy( _
Path.Combine(FolderBrowserImport.SelectedPath, s), _
Path.Combine("c:\test", s), _
True _
)

Next s

You can swap this fixed array out for a list that VB prepares for you:

For Each s as String in Directory.GetFiles(FolderBrowserImport.SelectedPath, "settings*.txt", SearchOption.TopDirectoryOnly)

File.Copy(s, Path.Combine("c:\test", Path.GetFilename(s)), True)

Next s

Tips:

  • It's usually cleaner to do a If bad Then Exit Sub than a If good Then (big load of indented code) End If - test all your known failure conditions at the start and exit the sub if anything fails, rather than arranging a huge amount of indented code
  • Use Path.Combine to combine path and filenames etc; it knows how to deal with stray \ characters
  • Use Imports to import namespaces rather than spelling everything out all the time (System.Windows.Forms.DialogResult - a winforms app will probably have all the necessaries imported already in the partial class so you can just say DialogResult. If you get a red wiggly line, point to the adjacent lightbulb and choose to import System.WIndows/Forms etc)


Related Topics



Leave a reply



Submit