Exemplo n.º 1
0
Arquivo: pt.cs Projeto: 24/source_04
        // PrintManager_v2 pm
        public static void RenameFile(PrintManager 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;

            //Trace.WriteLine("path \"{0}\"", path);
            if (!zPath.IsPathRooted(path))
            {
                path = zPath.GetFullPath(path);
            }
            //Trace.WriteLine("path \"{0}\"", path);
            string file = zPath.GetFileName(path);

            if (!zFile.Exists(path))
            {
                Trace.WriteLine("file dont exists \"{0}\"", file);
                return;
            }
            PrintIssue issue;

            if (printFile != null)
            {
                issue = pm.Find(printFile + ".pdf");
            }
            else
            {
                issue = pm.Find(file);
            }
            string msgFile = "\"" + file + "\"";

            if (printFile != null)
            {
                msgFile += " (\"" + printFile + "\")";
            }
            if (issue == null || issue.Error != null)
            {
                if (writeUnknowPrint)
                {
                    Trace.Write(fmt1, msgFile);
                    if (issue == null)
                    {
                        Trace.Write(" unknow print");
                    }
                    else
                    {
                        Trace.Write(" {0}", issue.Print.Name);
                    }
                    if (issue != null && issue.Error != null)
                    {
                        Trace.Write(" " + issue.Error);
                    }
                    Trace.WriteLine();
                }
                return;
            }
            if (debug)
            {
                Trace.Write(fmt1, msgFile);
                Trace.WriteLine(fmt2, issue.Print.Name);
                issue.PrintValues.zTrace();
            }
            string file2 = issue.GetFilename();

            //Trace.WriteLine("zPath.GetDirectoryName(path) \"{0}\"", zPath.GetDirectoryName(path));
            //Trace.WriteLine("issue.Print.Directory       \"{0}\"", issue.Print.Directory);
            if (file == file2 && (!moveFile || zPath.GetDirectoryName(path).Equals(issue.Print.Directory, StringComparison.InvariantCultureIgnoreCase)))
            {
                if (writeFilenameOk)
                {
                    Trace.Write(fmt1, msgFile);
                    Trace.Write(fmt2, issue.Print.Name);
                    Trace.Write(" filename ok");
                    if (moveFile)
                    {
                        Trace.Write(" move to same directory");
                    }
                    Trace.WriteLine();
                }
                return;
            }

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

            if (moveFile && !simulate && logFileInDestinationDirectory)
            {
                traceFile = zPath.Combine(issue.Print.Directory, "log.txt");
                //_tr.AddTraceFile(traceFile);
                //_tr.AddTraceFile(traceFile, LogOptions.None);
                if (traceFile != null)
                {
                    Trace.CurrentTrace.AddOnWrite("zprint", WriteToFile.Create(traceFile, FileOption.None).Write);
                }
            }
            try
            {
                Trace.Write(fmt1, msgFile);
                Trace.Write(fmt2, issue.Print.Name);
                string path2;
                bool   fileExists  = false;
                bool   filesEquals = false;
                if (moveFile)
                {
                    path2 = zPath.Combine(issue.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 = issue.GetFilename(index++);
                    path2 = zpath.PathSetFileName(path2, 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("zprint");
                }
            }
        }
Exemplo n.º 2
0
Arquivo: pt.cs Projeto: labeuze/source
        // PrintManager_v2 pm
        public static void RenameFile(PrintManager 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;
            //Trace.WriteLine("path \"{0}\"", path);
            if (!zPath.IsPathRooted(path))
                path = zPath.GetFullPath(path);
            //Trace.WriteLine("path \"{0}\"", path);
            string file = zPath.GetFileName(path);
            if (!zFile.Exists(path))
            {
                Trace.WriteLine("file dont exists \"{0}\"", file);
                return;
            }
            PrintIssue issue;
            if (printFile != null)
                issue = pm.Find(printFile + ".pdf");
            else
                issue = pm.Find(file);
            string msgFile = "\"" + file + "\"";
            if (printFile != null)
                msgFile += " (\"" + printFile + "\")";
            if (issue == null || issue.Error != null)
            {
                if (writeUnknowPrint)
                {
                    Trace.Write(fmt1, msgFile);
                    if (issue == null)
                        Trace.Write(" unknow print");
                    else
                        Trace.Write(" {0}", issue.Print.Name);
                    if (issue != null && issue.Error != null)
                        Trace.Write(" " + issue.Error);
                    Trace.WriteLine();
                }
                return;
            }
            if (debug)
            {
                Trace.Write(fmt1, msgFile);
                Trace.WriteLine(fmt2, issue.Print.Name);
                issue.PrintValues.zTrace();
            }
            string file2 = issue.GetFilename();
            //Trace.WriteLine("zPath.GetDirectoryName(path) \"{0}\"", zPath.GetDirectoryName(path));
            //Trace.WriteLine("issue.Print.Directory       \"{0}\"", issue.Print.Directory);
            if (file == file2 && (!moveFile || zPath.GetDirectoryName(path).Equals(issue.Print.Directory, StringComparison.InvariantCultureIgnoreCase)))
            {
                if (writeFilenameOk)
                {
                    Trace.Write(fmt1, msgFile);
                    Trace.Write(fmt2, issue.Print.Name);
                    Trace.Write(" filename ok");
                    if (moveFile)
                        Trace.Write(" move to same directory");
                    Trace.WriteLine();
                }
                return;
            }

            if (moveFile && !simulate && !zDirectory.Exists(issue.Print.Directory))
                zDirectory.CreateDirectory(issue.Print.Directory);
            string traceFile = null;
            if (moveFile && !simulate && logFileInDestinationDirectory)
            {
                traceFile = zPath.Combine(issue.Print.Directory, "log.txt");
                //_tr.AddTraceFile(traceFile);
                //_tr.AddTraceFile(traceFile, LogOptions.None);
                if (traceFile != null)
                    Trace.CurrentTrace.AddOnWrite("zprint", WriteToFile.Create(traceFile, FileOption.None).Write);
            }
            try
            {
                Trace.Write(fmt1, msgFile);
                Trace.Write(fmt2, issue.Print.Name);
                string path2;
                bool fileExists = false;
                bool filesEquals = false;
                if (moveFile)
                    path2 = zPath.Combine(issue.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 = issue.GetFilename(index++);
                    path2 = zpath.PathSetFileName(path2, 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("zprint");
            }
        }