2

I need to zip files from folder X to folder Y. When the files from folder X are zipped into folder Y. The files in folder X needs to be removed. The zip name must be the name of the file with the .DBS in that folder.

So I need to read whats the file name of the .DBS file is. Then I need to zip all the files in folder X to folder Y with the name: "Filename" (this is the same as the .DBS file) If the files are zipped and well in folder Y they need to be removed from folder X.

The code that I got at the moment will move the files of folder X too Y. So this is a start. My question is how can I get the name of the file too be the zip folder name.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;

namespace Ramasoftzipper
{
class Program
{
    static void Main(string[] args)
    {


        string fileName = @"160001.DBS";
        string sourcePath = @"C:\RMExport";
        string targetPath = @"C:\Exportrm";
        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);
        string startPath = @"C:\RMExport";
        string zipPath = (fileName);
        string extractPath = @"C:\Exportrm";


        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }


        System.IO.File.Copy(sourceFile, destFile, true);


        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);


            foreach (string s in files)
            {

                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
                ZipFile.CreateFromDirectory(startPath, zipPath);
                ZipFile.ExtractToDirectory(zipPath, extractPath);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");



        }
    }
}
}

Thanks in advance.

3
  • string[] files = System.IO.Directory.GetFiles(path, "*.DBS"); this will retrieve all the filenames that have a .DBS extension. Is that what you need ? Commented Nov 16, 2016 at 7:34
  • @bkaf yes thats what I need but my zip code still isnt working. Do you know what I should do to fix that? Commented Nov 16, 2016 at 7:36
  • @bkaf where should I place: string[] files = System.IO.Directory.GetFiles(startPath, "*.DBS"); In my code? Commented Nov 16, 2016 at 7:39

1 Answer 1

2

(only explaining this much because I know you and you need to learn this stuff, yo :P)

you could try something like this, read comments in code for more info, this code is only showing you how to zip all files in a folder, try the next step of adding certain extentions yourself

//files to zip, you can also use the same method as above to let the user determine what path to zip
string path = @"C:\Users\WsLocal.NL-ROE2-W297\Pictures";            
string zipPath = @"C:\Users\WsLocal.NL-ROE2-W297\Desktop\zip\result.zip";            

//zip files
ZipFile.CreateFromDirectory(path, zipPath);
string[] files = Directory.GetFiles(path);

//some debugging
foreach (string filePath in files)
{
    Console.WriteLine(filePath);
}
//wait untill user presses enter
Console.ReadLine();

[EDIT]

setting the name of the zip file to a file name:

replace

string zipPath = @"C:\Users\WsLocal.NL-ROE2-W297\Desktop\zip\result.zip";

with

//get all files from directory decladed by path
string[] files = Directory.GetFiles(path);
//select the 1st one and delete the folder information so just the file name is left with it's extention
string zipName = files[0].Replace(path, "");

//delete the extention
int index = zipName.IndexOf(".");
if (index > 0)
     zipName = zipName.Substring(0, index);

//assemble the zip location with the generated file name         
string zipPath = @"C:\Users\WsLocal.NL-ROE2-W297\Desktop\zip\"+ zipName + ".zip";   

and delete

string[] files = Directory.GetFiles(path);

under

//zip files
ZipFile.CreateFromDirectory(path, zipPath);
Sign up to request clarification or add additional context in comments.

10 Comments

Hey Luuk, How can I give the zip file the name from my .dbs file
is there anyway how I can delete the files from the ramasoft folder if they are zipped?
string[] files = Directory.GetFiles(path); gives an error. files is already defined in this scope
if it's already defined, delete it, rename it if you're using another path but still be able to use the other one, or remove string[] infront of files on the line where the error occurs
And could u explain your code a bit so I can learn from this? especially : string[] files = Directory.GetFiles(path); string zipName = files[0].Replace(path, ""); int index = zipName.IndexOf("."); if (index > 0) zipName = zipName.Substring(0, index); string zipPath = @"C:\Users\WsLocal.NL-ROE2-W297\Desktop\zip\"+ zipName + ".zip";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.