Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供替换文件和文件夹和文件内容的工具 #150

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,4 @@ healthchecksdb
/GitRevisionTask/Properties/launchSettings.json
/RunWithConfigValueTask/Properties/launchSettings.json
/RegexReplaceTask/Properties/launchSettings.json
*/Properties/launchSettings.json
57 changes: 57 additions & 0 deletions ReplacePathNameAndContent/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// See https://aka.ms/new-console-template for more information

using dotnetCampus.Cli;

var option = CommandLine.Parse(args).As<Option>();

CopyFolder(Directory.CreateDirectory(option.Folder), Directory.CreateDirectory(option.OutputFolder),
name => name.Replace(option.Content, option.ReplaceText));

static void CopyFolder(DirectoryInfo originFolder, DirectoryInfo desFolder, Rename rename)
{
foreach (var directory in originFolder.EnumerateDirectories())
{
var originName = directory.Name;
var newName = rename(originName);

var newDirectory = desFolder.CreateSubdirectory(newName);
CopyFolder(directory, newDirectory, rename);
}

foreach (var file in originFolder.EnumerateFiles())
{
var originName = file.Name;
var newName = rename(originName);

var destFileName = Path.Join(desFolder.FullName, newName);

try
{
// 尝试读取文件重写一下
var content = File.ReadAllText(file.FullName);
var output = rename(content);
File.WriteAllText(destFileName, output);
}
catch
{
file.CopyTo(destFileName, overwrite: true);
}
}
}

delegate string Rename(string name);

class Option
{
[Option('f', nameof(Folder))]
public string Folder { get; set; } = null!;

[Option('o', "Output")]
public string OutputFolder { get; set; } = null!;

[Option('c', nameof(Content))]
public string Content { get; set; } = null!;

[Option('r', "Replace")]
public string ReplaceText { get; set; } = null!;
}
14 changes: 14 additions & 0 deletions ReplacePathNameAndContent/ReplacePathNameAndContent.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.CommandLine" Version="3.3.1" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions ReplacePathNameAndContent/ReplacePathNameAndContent.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReplacePathNameAndContent", "ReplacePathNameAndContent.csproj", "{7FED2487-B912-43B9-9970-CC8EBE5A4E0C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7FED2487-B912-43B9-9970-CC8EBE5A4E0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FED2487-B912-43B9-9970-CC8EBE5A4E0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FED2487-B912-43B9-9970-CC8EBE5A4E0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FED2487-B912-43B9-9970-CC8EBE5A4E0C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion SyncTool/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"Serve": {
"commandName": "Project",
"commandLineArgs": "serve -p 5625",
"workingDirectory": "C:\\lindexi\\SyncFile_Serve\\"
"workingDirectory": "C:\\lindexi\\SyncFile_Serve\\File\\"
},
"Sync": {
"commandName": "Project",
Expand Down
Loading