Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
set x=Folder
xcopy /y /v /e .\%x%\* \\10.0.0.200\d\

for %%y in ( 202 203 204 205) do (
net use \\10.0.0.%%y\e Password1 /user:User1
echo Copying files to \\10.0.0.%%y\e\
xcopy /y /v /e .\%p%\* \\10.0.0.%%y\e\
)
Posted
Updated 4-Jan-22 16:55pm
Comments
Sergey Alexandrovich Kryukov 6-Apr-15 0:04am    
I'm curious: why? :-)
What have you tried so far?
—SA

I have no idea how did you come to the situation where you need such "convert". It could be the result of illusionary attempts to get away without any programming. :-) Anyway…

For "net use" and other commands, you can start the process with command line parameters using System.Diagnostics.Process.Start:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.start%28v=vs.110%29.aspx.

For copying of files, its better to use the methods System.IO.File.Copy:
https://msdn.microsoft.com/en-us/library/system.io.file.copy%28v=vs.110%29.aspx.

To get a list of file to be copied, possible using a file mask, you can use the methods System.IO.Directory.GetFiles:
https://msdn.microsoft.com/en-us/library/system.io.directory.getfiles%28v=vs.110%29.aspx[^].

That's all.

—SA
 
Share this answer
 
Below StackOverflow question may lead you in the right direction as it elaborates multiple ways of doing similar things.

c# - Run Command Prompt Commands - Stack Overflow[^]
 
Share this answer
 
If you have a bat that serves you, and what it does is execute commands, and it has done that, I don't see much advantage in converting to C#.

You would have to learn C#, and write all your commands already porting them to methods and instructions inside C#, and believe me, that will take a long time.

It would have to carry its variables, its commands, the paths of folders/files, accounts and users, credentials, password, in addition to treatments for possible errors (predictable and unpredictable), and this really takes a long time..

There is no converter that does this, however it is possible to "run" a bat in C# and capture the output, or it is also possible to make "calls" for isolated (or not) executions of commands.

As much as I have some remarks in your script, and I won't comment on those, as I understand that it's just an outline to exemplify the question, the only visible reason for your intention would be to omit username/password.

Is this correct? If so, then your question would have either another, you would have to open a specific question in that sense...

Anyway, there are several options without having all this work/time, among them the ones I usually use, I just make the C# generate the bat...

The simplest way to generate a bat, and without wasting a lot of time, already reducing the number of necessary lines in C#, is to:

C#
using System;
using System.IO;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication
{
   class Program
   {
      static void Main(string[] args)
      {
        String encodedString = @"QGVjaG8gb2ZmIAoKc2V0ICJ4PUZvbGRlciIKeGNvcHkgL3kgL3YgL2UgLlw
                                 iJXglIlwqIFxcMTAuMC4wLjIwMFxkXAoKZm9yICUleSBpbiAoMjAyLDIwMy
                                 wyMDQsMjA1KWRvICgKICAgICBuZXQgdXNlIFxcMTAuMC4wLiUlfnlcZSAiJ
                                 X4xIiAvdXNlcjoiJX4yIgogICAgIGVjaG9cQ29weWluZyBmaWxlcyB0byBc
                                 XDEwLjAuMC4lJX55XGVcCiAgICAgeGNvcHkgL3kgL3YgL2UgLlwiJXglIlw
                                 qIFxcMTAuMC4wLiUlfnlcZVwKICAgICk=";
                                 
        File.WriteAllBytes(@"z:\batchfilename.bat", Convert.FromBase64String(encodedString));
        System.Diagnostics.Process.Start(@"z:\batchfilename.bat", "\"PassWord1\" \"User1\"");
      }
   }
}

/* bat file decoded:

@echo off 

set "x=Folder"
xcopy /y /v /e .\"%x%"\* \\10.0.0.200\d\

for %%y in (202,203,204,205)do (
     net use \\10.0.0.%%~y\e "%~1" /user:"%~2"
     echo\Copying files to \\10.0.0.%%~y\e\
     xcopy /y /v /e .\"%x%"\* \\10.0.0.%%~y\e\
    )


Execute bat: 
@"z:\batchfilename.bat", "\"PassWord1\" \"User1\""

Bat argument:
Argument %1 == PassWord1   Argument %2 == User1
*/


1. Create your bat and test it as much as possible

2. Convert the code to base64

3. Defines a variable in your code with the base64 strings

4. Decode at runtime to a pre-defined and proper location for execution

5. Call the bat execution on the path where it was decodes

6. If necessary, pass your arguments

For more options and to get output without opening a cmd window, see this answer on Executing Batch File in C# - Stack Overflow
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900