1.

Solve : Batch problem. (xcopy)?

Answer»

I wrote a short batch file in windows xp, who needs to copy from one directory to the other and from the other one to this one (updating all the filles from one to another):

xcopy %1 %2 /d /e /i /f /y /g /EXCLUDE:Exclude.txt
xcopy %2 %1 /d /e /i /f /y /g /EXCLUDE:Exclude.txt
pause

(I sent parameters [%1,%2] using c#)
All the copying PROCESS went well exept for the excluding, if the exclude file contains paths with spaces it won't exclude them, and those files will be copied.
Another bug that happens from time to time (not always) - the first path in Exclude.txt is copied normaly.
I read all the articals about xcopy and non of them helped me (neither using slashes or short paths ect.).
I have tried using " " for files containing spaces, I.e. like "c:\program files\whatever".

Thanks, Dan. XCOPY can be one of the most helpful utilities but the Microsoft documentation can be a bit obtuse.

The exclude file is used for PATTERN matching so short file names, slashes(?) and quotes will make a match more unlikely.

I cannot duplicate your file path problems with embedded spaces, nor with the first path in the exclude file being included in the output.

Please post your exclude file otherwise we're just stumbling around in the dark.

 


I tried it again with another exclude file and i found that the sapces are not the problem, the problem is that many of my file's name's are in hebrew (=a language), and it messy with batch files (it becomes gyberish).
Is there any way to create a batch file that supports other (non latin) alpha-bet?

Thanks, Dan.You can install Hebrew language support by going to Control Panel==>Language and Regional Options. Click on the Languages tab and then check the Install file for complex scripts and right to left languages box. You will need your Windows XP installation disk.

Once installed, you can use the character map (Accessories==>System Tools) to select the Hebrew font and then select and copy characters to the clipboard where they can be pasted into the editor you're using for the exclude file.

Just to be safe, I highly suggest you make a restore point before installing the Hebrew language support.

Good luck.  I did all you have written, it's still the same problem...
maby it can't be solved.

Although, thanks! Quote from: lo12p on July 11, 2010, 01:02:47 AM

maby it can't be solved.

Of course it can be solved. You need to use an editor that can save the exclude file with Unicode encoding. (Notepad works fine).  With all the select and copy from the character map and the pasting into Notepad, it is a painstakingly slow process but given enough time and effort this can be done.

Hint: You can select all the characters in sequence for a single file from the character map and then copy the entire string to the clipboard for pasting into notepad. This way you build the exclude file line by line instead of character by character.

Note: Test just one excluded file and use the XCOPY /L switch. This will list out the files that would be copied but no actual file copying takes place. Remove the /L switch when you're ready for the real thing.

Good luck.  I tried it (the unicode save) and it worked.
But haw can i save a text file (with this unicode thing) using c#?
Is there a function that saves a file and get's arguments? ect.
(because im writing to the exclude files using c#)I'm not a C# programmer, but it looks suspiciously like VB.Net

This may help. Check out Write a Text File (Example 2).

 

Thanks!! Quote from: Sidewinder on July 11, 2010, 11:01:27 AM
I'm not a C# programmer, but it looks suspiciously like VB.Net

C# is nothing like VB.NET Syntax wise, only framework-wise.

Anyway, if I understand the requirement, is the intention to open an ASCII file and save it as a Unicode? Because that's pretty easy. The sample referenced looks old- it doesn't use the now recommended "using" construct, and instead wraps everything in a try...catch...finally.


Anyway, I took this as a small project and came up with a small program that simply opens a file using ASCII encoding, and WRITES it to another file that it opens/creates for Unicode.

Code: [Select]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace writetest
{
    class Program
    {
//note, I had to wrap the following constant in order to prevent the post from looking huge horizontally
        private const string helptext = "ASC2UNC help summary\n\nSYNTAX:\nASC2UNC <sourcefile>
<destfile> [/a]\n\nASC2UNC opens the source file (using ASCII encoding)
and directly copies the stream to the destination file (using unicode). of /A os specified, the destination file is opened for appending.";
        static void showhelp()
        {


            Console.WriteLine(helptext);



        }

        static void Main(string[] args)
        {
            //takes two arguments: args[0] is the source filename, args[1] is the destination filename.

            if (args.Length < 2)
            {
                Console.WriteLine("Error: Insufficient number of arguments.");
                showhelp();

                return;
            }
            String sourcefile = args[0];
            String destfile = args[1];

            if (!Path.IsPathRooted(sourcefile))
                sourcefile = Path.Combine(Environment.CurrentDirectory, sourcefile);
            if (!Path.IsPathRooted(destfile))
                destfile = Path.Combine(Environment.CurrentDirectory, destfile);
            bool DoAppend = args.ToList().Exists((w) =>(w.Equals("/A", StringComparison.OrdinalIgnoreCase) ||
                                        w.Equals("-A", StringComparison.OrdinalIgnoreCase)));
            try
            {
                using (StreamReader Ascinput = new StreamReader(sourcefile, Encoding.ASCII))
                {
                    using (StreamWriter Uncoutput = new StreamWriter(destfile,
                                                                     DoAppend,
                                                                     Encoding.Unicode))
                    {
                       

                        Uncoutput.Write(Ascinput.ReadToEnd());


                    }

                }


            }
            catch (IOException e)
            {
                Console.WriteLine("Exception:" + e.Message);


            }
        }
    }
}


to test, I created a sample text file in Notepad and saved it using ASCII encoding. I then used the program and created a file, "unicode.txt" which, while I was able to use "type" and other PROGRAMS to view the file just fine, was twice the size of the ASCII file I created it from (suggesting unicode was in fact properly used).

Of course I really have no idea wether this is even close to what is desired, but I would imagine it's closer then the sample. Also if the ability to go the other way is required, it shouldn't be too DIFFICULT. One could simply store the Encoding enumeration values in variables and change them if you find specific switches.

... oh yeah and sorry about the lambda expression... I remember how confusing those were for me but now they are second nature.


Discussion

No Comment Found