Refactor bat file ren command with wildcards to C# - c#

I'm looking to refactor a bat file into a C# console application. The main task of this bat file is to move and rename files using wildcards. I cannot find if there is anything in C# System.IO namespace (or other namespace) that can accomplish what I am trying to do.
Example input file:
D:\Testing\SrcDir\Input-20210318123001.txt
Desired output file:
D:\Testing\DestDir\Output-20210318123001.txt
Here is an example of the existing commands run that I am looking to convert to C#:
ren D:\Testing\SrcDir\Input-*.* Output-*.*
copy D:\Testing\SrcDir\Output-*.* D:\Testing\DestDir\
del D:\Testing\SrcDir\Output-*.*
My first thought was to simply use the System.IO.File.Move method. But this does not support wildcard usage. Neither does the System.IO.File.Copy method.
I'm trying to avoid using regex if possible (I have little to no experience with regex). I'd like to avoid rolling my own here if there is a simple solve for this that I have missed in my searching. Is there a good way to refactor this functionality into C#?

Related

How to specify compiler options in c# file?

I have a code.cs file that I compile with the following command line:
"%ProgramFiles(x86)%\MICROS~3\2017\ENTERP~1\MSBuild\15.0\Bin\Roslyn\csc.exe" ^
/target:library /out:fx1.dll fx1.cs ^
/reference:"C:\blah\blah\Microsoft.JScript.dll" ^
/reference:"C:\astor\loads\better\ease\zog.dll"
Is there a practical way to specify those options inside the fx1.cs file ?
This is mainly C#, but what about other .Net languages like Vb.Net and the others? Is there at least one language that can specify compiler parameters in a self-contained file?
Update: I also see there are answer files for csc, but they lack (or I can't see) enough flow control to embed one in a cs file.
The solution I came up with as baseline answer for this question is to turn the C# file into a "polyglot" *.cs.bat file that runs the full compile command on top of the C# source code.
/*? 2>NUL & #echo off
echo.
echo COMPILING...
"%ProgramFiles(x86)%\MICROS~3\2017\ENTERP~1\MSBuild\15.0\Bin\Roslyn\csc.exe" ^
/target:library /out:fx1.dll fx1.cs.bat ^
/reference:"C:\blah\blah\Microsoft.JScript.dll" ^
/reference:"C:\astor\loads\better\ease\zog.dll"
PAUSE
GOTO:EOF REM */
// C# program...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
// blah...
To compile I can just run fx1.cs or fx1.cs.bat. That certinly is practical.
This may be useful but the downsides are at least these:
text highlighting is lost in the various editors that have it for *.cs files... If only there was a (easy) way to run *.cs files as batch (type file | cmd seems to ignore PAUSE, EXIT /B, EXIT, GOTO:EOF)
The compiler path is hardcoded (but I think it can be determined with more batch fiddling)
It tries to run /*.exe, so the first line always gives me an error because I found no way to avoid it being run, so I can only put a ? in there (so it should not be a valid path ever) and hide it with error redirection.
While it is unpractical to edit in an IDE, I think this may be an easy format for distribution of simple utilities.
I'm not very good at this... so if someone has better ideas (for the polyglot route) please improve this answer by commenting or editing it.

Find path to active user directory in C

I'm writing a C# program where I have to write some files to a temp folder. In this program, I call an executable from a C project which also needs to write to said temp folder. In C#, I can simply find this with
Directory.GetTempPath();
But in C, I can't seem to find any way of locating it. Do I need to rewrite the C program to take the path as an argument? I would really prefer not to since I'm not very proficient in the language and only barely got it working for my current purpose.
Are there any other, static locations in Windows where it would be appropriate to write temporary files?
My suggestion is to declare the file name relative to a system variable, like %temp% and create there your file as standard file (maybe with a pattern so you can delete it at end of your need), there's also F* tempfile() library function, but I used it only on linux and don't know if it works on windows (it returns you a pointer to a system-defined temp file)
There is a similar function GetTempPath in Win32 API - GetTempPathA MSDN link
You can see how most of C# code is implemented using ReferenceSource. For the GetTempPath() function it calls Win32 API directly.

Significance of a PATH explained

This is probably a rudimentary question but I am still kinda new to programming and I've wondered for awhile. I've done multiple projects in Python, C#, and Java, and when I try to use new libraries (especially for Python) people always say to make sure its in the right PATH and such. I just followed an online tutorial on how to install Java on a new computer and it rekindled my question of what a path really is. Is the Path just were the programming language looks for a library in the file system? I get kinda confused on what it's significance is. Again, I'm sorry for the wide question, its just something that I've never quite gotten on my own programming.
EDIT: I just wanted to thank everyone so much for answering my question. I know it was a pretty dumb one now that I've finally figured out what it is, but it really helped me. I'm slowly working through as many C#, Java and Python tutorials as I can find online, and it's nice to know I have somewhere to ask questions :)
The PATH is an environment variable which the shell (or other command interpreter) uses to search for commands. Usually (always?) commands are found with a greedy algorithm, so entries that come first in the PATH are returned first. For example, a command in /usr/local/bin will override a command in /usr/bin given a PATH such as
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
while the purpose is consistent, the syntax is slightly different on WINDOWS - you would use
C:\> ECHO %PATH%
to "echo" your PATH.
First my shell is going to search /usr/local/sbin then /usr/local/bin then /usr/sbin and then /usr/bin before searching /sbin and /bin if the command isn't found then it will report that it couldn't find such a command...
# Like so
$ thisprogramdoesntexist
thisprogramdoesntexist: command not found
Now, on Linux at least, there's also a LD_LIBRARY_PATH which the system will use to search for dynamic libraries (greedily), on Windows I think it just uses the PATH. Finally, Java uses a CLASSPATH which is similar (but used to search for classes and JARs).
On Linux one might add an entry to the PATH like so,
$ export PATH="$PATH:/addNewFolder"
While on Windows you might use
set PATH=%PATH%;c:\addNewFolder
Sometimes, you might manipulate your PATH(s) to enable specific functionality, see update-java-alternatives on Ubuntu for an example.
A PATH is a file directory on your computer. If you need to install a programming language, you might need to put it in your system PATH variable. This means that the system looks to these files for different information, IE where the libraries for the code you are using are.
Hope that helped!
Exactly as other said, PATH is a list of folders that is included in the search -other than the current folder- and you can always access straight away. It's one of the Environment Variables.
For example, we have the python folder in C:\Python27. I'm sure you know that to run a python file, we commonly use python script.py.
What happens is that the command line searches for python.exe in your current folder, and if not found, search it in the folders in the path variable.
To read the path, you can, straightforwardly use:
$ PATH
If you're on windows, like i am, an easy way to deal with this is to just use System Properties. Just type it in the start menu, open it, and go to the 'advanced' tab. Click on the Environment Variables, there! You'll see a PATH variable, and you can modify it as you want.
I myself use more than one version of Python, and to deal with this, i appended all the folders to PATH, and changed my python.exe to pythonversion_number.exe. Problem solved! Now, i can run this in the command line:
$ python26 script.py
$ python33 script2.py
Some further reading on this, if you're interested, here's a good question asked
Hope this helps!
The best resource (so far) about PATH information, you can see in this question:
https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them
Stack Overflow is not the best place to search about this, always check the amazing
https://superuser.com/ for this kind of question.
PATH is a symbolic name usually associated to string values separated by a semicolons (where each string part is a directory name). This symbolic name (and its values) is handled by the operating system and could be modified by the end user through the some command line instruction like SET PATH=........ or through some kind of user interface configuration tool.
It is common practice for tools like compilers or other programming tools to look at this symbolic name and use the list of string values for searching files that are not directly available in the current folder used by the tools.
So, if an installation procedure set the PATH symbol in this way
SET PATH=%path%;C:\PROGRAM FILES\MYTOOLFOLDER;
it means, set the PATH symbol to the previous value (%PATH%) and add another string value to it (C:\PROGRAM FILES\MYTOOLFOLDER).
Then the tool, when it needs to search for a particular file or library, could read the PATH symbol values, split them at the semicolons and iteratively look at the directories listed one by one looking for the file required.
In C# programming, for example, the tool code could contain something like this
string pathSymbol = Environment.GetEnvironmentVariable("PATH");
string[] pathFolders = pathSymbol.Split(';');
foreach(string folder in pathFolders)
{
if(File.Exists(Path.Combine(folder, "mylibrary.dll"))
{
..... do whatever you need to do with the file
}
}
This example assumes a Windows environment.

How to merge 2 zip files together into 1 zip

I am trying to make a custom launcher for Minecraft in C# but I have come across a bump.
I want to add something into it, Minecraft Forge, but the only way I could think of is to change the extension of minecraft.jar to minecraft.zip, extract the contents of the Minecraft Forge.zip and the minecraft.zip into the same folder and then zip that entire folder up into minecraft.jar.
However minecraft.jar has a file named aux.class so whenever my extract script (Made in java) tries to extract it, it simply says:
Unable to find file G:\Programming\C#\Console\Forge Installer\Forge Installer\bin\Debug\Merge\aux.class.
The only other way I can think of is to merge minecraft_forge.zip into minecraft.zip, I have spent around 2 hours looking on Google (watch as someone sees it within a couple of minutes) but it always shows me results for "How to zip multiple files", "How to make a zip file in C#" etc.
So I have come here looking for my answer, sorry if this is a lot to read but I always see comments on here saying "You didn't give enough information for us to help you with".
EDIT: The question in case it wasn't clear is: How am I able to put the contents of minecraft_forge.zip into minecraft.zip?
In your case, if you cannot unzip the files due to OS limitations, you need to "skip" unzipping temporary files to zip them. Instead, only handle input & output streams, as suggested in the answers found here: How can I add entries to an existing zip file in Java?
As you pointed out, "aux" is a protected keyword within windows and it does not matter what the file suffix may be; windows won't let you use it. Here are a couple of threads that discusses this in general.
Ref 1: Windows reserved words.
Ref 2: Windows reserved words.
If you are typing in commands to perform the copy or unzip, there is a chance you can get this to work by using a path prefix of the following \\.\ or \\?\. When I tested this, it worked with either a single or double back-slash following the period or question mark. Such that the following work:
\\.\c:\paths\etc
\\.\\c:\paths\etc
\\?\c:\path\etc
\\?\\c:\path\etc
I used the following command to test this. When trying to rename through windows explorer it gave a "The specified device name is invalid." error message. From the command line it worked just fine. I should point out, that once you create these files, you will have to manually delete them using the same technique. Windows Explorer reports that these text files which have a size of 0 bytes "is too large for the destination file system", ie... the recycle bin.
rename "\.\c:\temp\New Text Document.txt" aux.txt
del "\.\c:\temp\aux.txt"
As far as copying directly from zip or jar files, I tried this myself and it appeared to work. I used 7-zip and opened the jars directly using the "open archive..." windows explorer context menu. I then dragged-and-dropped the contents from forge.jar to the minecraft jar file. Since it is the minecraft jar file with the offending file name the chance of needing to create a temporary file on the filesystem is reduced. I did see someone mention that 7-zip may extract to a temporary file when copying between jars and zips.
7-zip reference on copying between archives
I should point out that my copy of minecraft jar (minecraft_server.1.8.7.jar) did not contain a file named aux.class. I also did not try to use the jar after the copy/merge. Nor did I spend too much time trying to figure out how well it merged the two contents since it appears like there may be a conflict with com\google\common\base\ since there are similar class name but with different $ variable suffixes on them.
I hope these two possible suggestions could give you some room to work with to find a solution for your needs... if you're still looking.

Write Resource file for .net project using the command line

I need to create a resource file for a .net project (by hand) and compile it using the ResGen.exe tool provided by the .NET framework. I can't find any documentation for this. I need to write the resource file by hand because I'm in a situation where I don't want to download/buy extra tools (like VS) to generate this resource file, and also I feel more productive through the command-line (helps me understand how things really work).
So I need to write a resource file by hand to store an ICON in the executable and use it from within my program. I would also like to use this icon to represent my executable in Windows Explorer.
Any references would be great!
Visual C# Express Edition will do what you want for free. If nothing else you can download that, create the resource file and then use that as a subject for your admirable curiosity about 'how it really works'. This may also save you some time in manual experimentation to get it right the first time around.
These 2 links in conjunction provide information on using that tool to create and embed an icon file, it seems specific to C#. Of course i'm guessing at your full intention, let me know if this points you in the proper direction.
http://www.xtremedotnettalk.com/showthread.php?t=75449
specifically there is a post which states;
I think you should first create a *.resources-File from the Icon with the tool named "Resgen.exe"...
resgen App.ico App.ico.resources
the next step would be compiling...
csc /t:winexe /out:Keygen.exe /res:App.ico.resources /r:Crypto.dll /win32icon:App.ico Keygen.cs AssemblyInfo.cs
I'm sure you were here already.
http://msdn.microsoft.com/en-us/library/ccec7sz1(VS.80).aspx
You should check this link:
http://msdn.microsoft.com/en-us/library/ekyft91f.aspx
It explains what formatter is used and gives some code samples to generate one from code. You could then write a small wrapper app that you can call from the command line. No downloads needed!

Categories