i need to move jpg image files from one place to another place.
cut and past.
lets say the folser "c:/abc/1" kave 10 images .
then what i have to do is when i run my aplication on inside that "c:/abc/1" all the images on that folder must be cut and pase toa another folder @ "D:/dani/web/"
so i have no idea on this but i have done some work on it.
so if their any one know about this plz help me.

private void button1_Click(object sender, EventArgs e)
        {
            int filecount = 0;
            foreach (string  sfiles in Directory.GetFiles("C:\\MyFolder"))
            {
                string extension = sFile.Substring(sFile.LastIndexOf("."));
                if (extension == ".jpg" || extension == ".JPG")
                {
            }

        }

Recommended Answers

All 13 Replies

System.IO.File.Move(path, path2);

might want to do some error checking too. but that's it.

is this all what i have to do

File.Move(SourcePath, DestinationPath);

that's it.

You can also use:

string[] files = Directory.GetFiles(@"C:\Images\", "*.jpg", SearchOption.AllDirectories);

to avoid having to manually check whether the files you've added are images ones, and also with the SearchOption enum you can tell it whether you want only the images in the top directory or the top directory and all subdirectories.

private void button1_Click(object sender, EventArgs e)
        {
           
            
               
                if (extension == ".jpg" || extension == ".JPG")
                {
                    File.Move(@"D:\New Folder\New Folder\wall papers\Bar Refaeli Wall", @"D:\New Folder\New Folder\wall papers");
                }

            }
        }

guys i chang ma code to this.but when i run this i get an error cold "extension does not exit in this current context".

string sourceFolder = @"put your first folder here";
string destFolder = @"put your 2nd folder here path here";

 private void button1_Click(object sender, EventArgs e)
        {
            int filecount = 0;
            foreach (string  sfiles in Directory.GetFiles(sourceFolder))
            {
                string extension = sFile.Substring(sFile.LastIndexOf("."));
                if (extension == ".jpg" || extension == ".JPG")
                {
                     File.Move(sfiles, destFolder +   sFile.Substring(sFile.LastIndexOf(@"\")));
                }
            }

        }

Something like that should get it.

string sourceFolder = @"put your first folder here";
string destFolder = @"put your 2nd folder here path here";

 private void button1_Click(object sender, EventArgs e)
        {
            int filecount = 0;
            foreach (string  sfiles in Directory.GetFiles(sourceFolder))
            {
                string extension = sFile.Substring(sFile.LastIndexOf("."));
                if (extension == ".jpg" || extension == ".JPG")
                {
                     File.Move(sfiles, destFolder +   sFile.Substring(sFile.LastIndexOf(@"\")));
                }
            }

        }

Something like that should get it.

this gives me an error on the string extension = sFile.Substring(sFile.LastIndexOf(".")); line

sorry theres a typo there. it should be

string extension = sFiles.Substring(sFile.LastIndexOf("."));

forgot the "s" in "sFiles"... but that should have been obvious.

thanxx but,,,can u tell me what is the wrong with this approach.

string destFolder = @"D:\New Folder\New Folder\wall papers";

        private void button1_Click(object sender, EventArgs e)
        {



            try
            {
                string[] filePaths = Directory.GetFiles(@"D:\New Folder\New Folder\wall papers\Bar Refaeli Wall", "*.jpg");
                File.Move(filePaths.ToString(), destFolder.ToString());

            }
            catch (Exception ex )
            {
                MessageBox.Show(ex.ToString());
                throw;
            }

I Don't have the time explain all the things wrong with that.

but for starts you are passing an array of files to a method that only is suppose to take the path of 1 files, and you aren't giving the new file name to the files you are moving.

you MUST use a loop for this, you can't just toString an array in a move method. The code I posted is an acceptable method.

the file object's move method accepts the filename of the file you want to move, and the file name of the files you want to move it to. that means the full path of the existing file, and the full path of the file as it will be. So you need the file name of the file to be appended to the destination path. and you must do this method for each file. you can't just expect it to work all at the same time.

Try using the method I wrote for you. look at it and try and understand how it works. If you have any problems post them and I will try and explain it better. But that what you come up with is not an acceptable solution.

the line string[] filePaths = Directory.GetFiles(@"D:\New Folder\New Folder\wall papers\Bar Refaeli Wall", "*.jpg"); is ok. you could use that in the method before instead of checking the extension manually. but you still need to loop through all the files in that array.

commented: beat me to it AND with a fuller explaination...curse my slow fingers :p +1

File.Move only accepts a single string as the source filename. You need to call File.Move once for each string in the array filePaths rather than trying to pass it all of the strings at once.

it's wrong because you when calling File.Move() procedure, it's input parameters should be the paths to the for the first location of the file and for the location where you want to move it.
This is line bellow is not correct:

File.Move(filePaths.ToString(), destFolder.ToString());

because your parameters are just the array objects printed there and NOT the elements of the object.
Just use Diamonddrake's solution. It's simple and straight forward.

you can try these steps to move jpg image file
(Optional) If you’re not moving photos to an existing folder, create a new folder. See Creating and managing folders.
In the Grid view of the Library module, select the photo or photos you want to move.
Drag the photo or photos to the destination folder in the Folders panel: Drag from the center of the thumbnail, not from the edge.

or convert image to barcode, check this code for barcode .jpg file conversion in c#.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.