Exemplo n.º 1
0
        //private static ITrace _tr = Trace.CurrentTrace;

        public static void RenameFile(PrintManager_v1 pm, string path, bool simulate = false, bool moveFile = false, string printFile = null)
        {
            string fmt1 = "file {0,-70}";
            string fmt2 = " {0,-30}";
            string fmt3 = " {0,-60}";
            bool writeFilenameOk = true;
            bool writeUnknowPrint = true;
            bool logFileInDestinationDirectory = false;
            bool debug = false;
            string file = zPath.GetFileName(path);
            if (!zFile.Exists(path))
            {
                Trace.WriteLine("file dont exists \"{0}\"", file);
                return;
            }
            Print1 print;
            //Trace.WriteLine("search print start");
            string error;
            if (printFile != null)
                print = pm.Find(printFile + ".pdf", out error);
            else
                print = pm.Find(file, out error);
            //Trace.WriteLine("search print end");
            string msgFile = "\"" + file + "\"";
            if (printFile != null)
                msgFile += " (\"" + printFile + "\")";
            if (print == null)
            {
                if (writeUnknowPrint)
                {
                    Trace.Write(fmt1, msgFile);
                    Trace.Write(" unknow print");
                    if (error != null)
                        Trace.Write(" " + error);
                    Trace.WriteLine();
                }
                return;
            }
            if (debug)
            {
                Trace.Write(fmt1, msgFile);
                Trace.WriteLine(fmt2, print.Name);
                //foreach (KeyValuePair<string, object> value in print.PrintValues)
                foreach (KeyValuePair<string, ZValue> value in print.PrintValues)
                    Trace.WriteLine("  {0} = {1}", value.Key, value.Value);
            }
            string file2 = print.GetFilename();
            if (file == file2 && (!moveFile || zPath.GetDirectoryName(path) == print.Directory))
            {
                if (writeFilenameOk)
                {
                    Trace.Write(fmt1, msgFile);
                    Trace.Write(fmt2, print.Name);
                    Trace.Write(" filename ok");
                    if (moveFile)
                        Trace.Write(" move to same directory");
                    Trace.WriteLine();
                }
                return;
            }

            if (moveFile && !simulate && !zDirectory.Exists(print.Directory))
                zDirectory.CreateDirectory(print.Directory);
            string traceFile = null;
            if (moveFile && !simulate && logFileInDestinationDirectory)
            {
                traceFile = zPath.Combine(print.Directory, "log.txt");
                //_tr.AddTraceFile(traceFile, LogOptions.None);
                if (traceFile != null)
                    Trace.CurrentTrace.AddOnWrite("pu1", WriteToFile.Create(traceFile, FileOption.None).Write);
            }
            try
            {
                Trace.Write(fmt1, msgFile);
                Trace.Write(fmt2, print.Name);
                string path2;
                bool fileExists = false;
                bool filesEquals = false;
                if (moveFile)
                {
                    //if (!simulate && !Directory.Exists(print.Directory))
                    //    Directory.CreateDirectory(print.Directory);
                    path2 = zPath.Combine(print.Directory, file2);
                }
                else
                    path2 = zpath.PathSetFileName(path, file2);
                int index = 2;
                while (zFile.Exists(path2))
                {
                    fileExists = true;
                    if (path == path2)
                        break;
                    //filesEquals = zfile.FilesEquals(path, path2);
                    filesEquals = zfile.AreFileEqual(path, path2);
                    if (filesEquals)
                        break;
                    file2 = print.GetFilename(index++);
                    //path2 = path.PathSetFileWithExt(path, file2);
                    path2 = zpath.PathSetFileName(path2, file2);
                }
                //if (simulate)
                //    Trace.Write(" simulate");
                //Trace.Write(" rename");
                //if (moveFile)
                //    Trace.Write(" and move");
                //Trace.WriteLine(" \"{0}\"", file2);
                Trace.Write(fmt3, "\"" + file2 + "\"");
                if (simulate)
                    Trace.Write(" simulate");
                if (fileExists)
                {
                    Trace.Write(" file exists");
                    if (filesEquals)
                        Trace.Write(" and is equals");
                    else
                        Trace.Write(" and is different");
                }
                if (filesEquals)
                    Trace.Write(" delete file");
                else
                {
                    Trace.Write(" rename");
                    if (moveFile)
                        Trace.Write(" and move");
                    Trace.Write(" file");
                }
                Trace.WriteLine();
                if (!simulate)
                {
                    if (filesEquals)
                        zFile.Delete(path);
                    else if (!zFile.Exists(path2))
                        zFile.Move(path, path2);
                }
            }
            finally
            {
                if (traceFile != null)
                    //_tr.RemoveTraceFile(traceFile);
                    Trace.CurrentTrace.RemoveOnWrite("pu1");
            }
        }
Exemplo n.º 2
0
        //private static ITrace _tr = Trace.CurrentTrace;

        public static void RenameFile(PrintManager_v1 pm, string path, bool simulate = false, bool moveFile = false, string printFile = null)
        {
            string fmt1             = "file {0,-70}";
            string fmt2             = " {0,-30}";
            string fmt3             = " {0,-60}";
            bool   writeFilenameOk  = true;
            bool   writeUnknowPrint = true;
            bool   logFileInDestinationDirectory = false;
            bool   debug = false;
            string file  = zPath.GetFileName(path);

            if (!zFile.Exists(path))
            {
                Trace.WriteLine("file dont exists \"{0}\"", file);
                return;
            }
            Print1 print;
            //Trace.WriteLine("search print start");
            string error;

            if (printFile != null)
            {
                print = pm.Find(printFile + ".pdf", out error);
            }
            else
            {
                print = pm.Find(file, out error);
            }
            //Trace.WriteLine("search print end");
            string msgFile = "\"" + file + "\"";

            if (printFile != null)
            {
                msgFile += " (\"" + printFile + "\")";
            }
            if (print == null)
            {
                if (writeUnknowPrint)
                {
                    Trace.Write(fmt1, msgFile);
                    Trace.Write(" unknow print");
                    if (error != null)
                    {
                        Trace.Write(" " + error);
                    }
                    Trace.WriteLine();
                }
                return;
            }
            if (debug)
            {
                Trace.Write(fmt1, msgFile);
                Trace.WriteLine(fmt2, print.Name);
                //foreach (KeyValuePair<string, object> value in print.PrintValues)
                foreach (KeyValuePair <string, ZValue> value in print.PrintValues)
                {
                    Trace.WriteLine("  {0} = {1}", value.Key, value.Value);
                }
            }
            string file2 = print.GetFilename();

            if (file == file2 && (!moveFile || zPath.GetDirectoryName(path) == print.Directory))
            {
                if (writeFilenameOk)
                {
                    Trace.Write(fmt1, msgFile);
                    Trace.Write(fmt2, print.Name);
                    Trace.Write(" filename ok");
                    if (moveFile)
                    {
                        Trace.Write(" move to same directory");
                    }
                    Trace.WriteLine();
                }
                return;
            }

            if (moveFile && !simulate && !zDirectory.Exists(print.Directory))
            {
                zDirectory.CreateDirectory(print.Directory);
            }
            string traceFile = null;

            if (moveFile && !simulate && logFileInDestinationDirectory)
            {
                traceFile = zPath.Combine(print.Directory, "log.txt");
                //_tr.AddTraceFile(traceFile, LogOptions.None);
                if (traceFile != null)
                {
                    Trace.CurrentTrace.AddOnWrite("pu1", WriteToFile.Create(traceFile, FileOption.None).Write);
                }
            }
            try
            {
                Trace.Write(fmt1, msgFile);
                Trace.Write(fmt2, print.Name);
                string path2;
                bool   fileExists  = false;
                bool   filesEquals = false;
                if (moveFile)
                {
                    //if (!simulate && !Directory.Exists(print.Directory))
                    //    Directory.CreateDirectory(print.Directory);
                    path2 = zPath.Combine(print.Directory, file2);
                }
                else
                {
                    path2 = zpath.PathSetFileName(path, file2);
                }
                int index = 2;
                while (zFile.Exists(path2))
                {
                    fileExists = true;
                    if (path == path2)
                    {
                        break;
                    }
                    //filesEquals = zfile.FilesEquals(path, path2);
                    filesEquals = zfile.AreFileEqual(path, path2);
                    if (filesEquals)
                    {
                        break;
                    }
                    file2 = print.GetFilename(index++);
                    //path2 = path.PathSetFileWithExt(path, file2);
                    path2 = zpath.PathSetFileName(path2, file2);
                }
                //if (simulate)
                //    Trace.Write(" simulate");
                //Trace.Write(" rename");
                //if (moveFile)
                //    Trace.Write(" and move");
                //Trace.WriteLine(" \"{0}\"", file2);
                Trace.Write(fmt3, "\"" + file2 + "\"");
                if (simulate)
                {
                    Trace.Write(" simulate");
                }
                if (fileExists)
                {
                    Trace.Write(" file exists");
                    if (filesEquals)
                    {
                        Trace.Write(" and is equals");
                    }
                    else
                    {
                        Trace.Write(" and is different");
                    }
                }
                if (filesEquals)
                {
                    Trace.Write(" delete file");
                }
                else
                {
                    Trace.Write(" rename");
                    if (moveFile)
                    {
                        Trace.Write(" and move");
                    }
                    Trace.Write(" file");
                }
                Trace.WriteLine();
                if (!simulate)
                {
                    if (filesEquals)
                    {
                        zFile.Delete(path);
                    }
                    else if (!zFile.Exists(path2))
                    {
                        zFile.Move(path, path2);
                    }
                }
            }
            finally
            {
                if (traceFile != null)
                {
                    //_tr.RemoveTraceFile(traceFile);
                    Trace.CurrentTrace.RemoveOnWrite("pu1");
                }
            }
        }