我正在尝试创建一个扫描驱动器的应用程序.但棘手的是,我的驱动器包含一组文件夹,这些文件夹在文件夹中包含文件夹并包含文档.我正在尝试扫描驱动器,获取所有文档和文件夹的"快照"并转储到.txt文件中.
我第一次运行这个应用程序时,输出将是一个包含所有文件夹和文件的文本文件.
我第二次运行这个应用程序,它将需要2个文本文件(从我第二次运行应用程序和第一次运行应用程序时的.txt文件生成的文件)并比较它们...报告什么已被移动/覆盖/删除.
有没有人有这个代码?我是这个C#的新手,任何帮助都会非常感激.
提前致谢.
我有去的,取代了"无效"字符(由我正则表达式定义)与blankspace的应用程序.我想要它,以便如果文件名中有2个或更多空格,则修剪一个.例如:
Deal A & B.txt
在我的应用程序运行后,将重命名为Deal A B.txt
(3个空格b/w A和B).我想要的是这样的:( Deal A B.txt
A和B之间的一个空格).
我试图确定如何做到这一点 - 我想我的应用程序将通过所有的文件名至少运行一次,以取代无效字符,然后通过文件名再次运行甩掉多余的空白.
任何人都可以帮我吗?
这是我目前用于替换无效字符的代码:
public partial class CleanNames : Form
{
public CleanNames()
{
InitializeComponent();
}
public void Sanitizer(List<string> paths)
{
string regPattern = (@"[~#&$!%+{}]+");
string replacement = " ";
Regex regExPattern = new Regex(regPattern);
StreamWriter errors = new StreamWriter(@"S:\Testing\Errors.txt", true);
var filesCount = new Dictionary<string, int>();
dataGridView1.Rows.Clear();
try
{
foreach (string files2 in paths)
{
string filenameOnly = System.IO.Path.GetFileName(files2);
string pathOnly = System.IO.Path.GetDirectoryName(files2); …
Run Code Online (Sandbox Code Playgroud) 我想知道如果我有一个文件名,我将如何摆脱文件名中的句点:
测试.... 1.txt看起来像Test 1.txt?我不希望触及像1.0.1 Test.txt这样的文件.只有具有连续句点的文件才应替换为空格.有任何想法吗?
这是我当前的代码,但正如您所看到的,它取代了扩展名称中的句点之外的每个句点:
public void DoublepCleanUp(List<string> doublepFiles)
{
Regex regExPattern2 = new Regex(@"\s{2,}");
Regex regExPattern4 = new Regex(@"\.+");
Regex regExPattern3 = new Regex(@"\.(?=.*\.)");
string replace = " ";
List<string> doublep = new List<string>();
var filesCount = new Dictionary<string, int>();
try
{
foreach (string invalidFiles in doublepFiles)
{
string fileOnly = System.IO.Path.GetFileName(invalidFiles);
string pathOnly = System.IO.Path.GetDirectoryName(fileOnly);
if (!System.IO.File.Exists(fileOnly))
{
string filewithDoublePName = System.IO.Path.GetFileName(invalidFiles);
string doublepPath = System.IO.Path.GetDirectoryName(invalidFiles);
string name = System.IO.Path.GetFileNameWithoutExtension(invalidFiles);
//string newName = name.Replace(".", " ");
string …
Run Code Online (Sandbox Code Playgroud) 我想知道这里是否有人可以帮助我,因为我还是C#的新手.我有一个文件夹w /在文件夹中的文件夹,所有文件夹都包含文件.有没有办法递归循环文件并将所有这些文件名收集到.txt文件中?
我不确定如何将这个实现到我的控制台应用程序中 - 所有人都有任何可能有用的代码吗?
我不确定我在这里做错了什么......但我注意到我的File.Move()没有重命名任何文件.
另外,有人知道在我的第二个循环中,我能够使用路径和清理文件名列表填充我的.txt文件吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//recurse through files. Let user press 'ok' to move onto next step
string[] files = Directory.GetFiles(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing", "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
Console.Write(file + "\r\n");
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
//End section
//Regex -- find invalid chars
string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
string replacement …
Run Code Online (Sandbox Code Playgroud) 我的应用程序的输出产生了重复的文件名...我不是100%确定为什么会这样.
我的应用程序通过查找文件名中的正则表达式模式来"清理"文件名.如果没有,则将其转储到"正常"列表中并忽略它.
这是我用于显示输出的代码:[这一直向我显示文件名的重复!!]
public partial class DriveRecursion_Results : Form
{
List<string> paths = new List<string>();
public DriveRecursion_Results()
{
InitializeComponent();
}
public void DriveRecursion(string retPath)
{
string pattern = (@"[~#&!%+{}]+");
Regex regEx = new Regex(pattern);
string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
List<string> normal = new List<string>();
List<string> fileNameOnlyList = new List<string>();
dataGridView1.Rows.Clear();
try
{
foreach (string fileNames in fileDrive)
{
string strippedFileName = System.IO.Path.GetFileName(fileNames);
fileNameOnlyList.Add(strippedFileName);
foreach (string nameOnly in fileNameOnlyList)
{
if (regEx.IsMatch(strippedFileName))
{
//string fileNameOnly = Path.GetFileName(fileNames);
string pathOnly = …
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,要求我"清理""脏"文件名.
我想知道是否有人知道如何处理名为的文件:
1.0.1.21 - 机密... doc或Accounting.Files.doc
基本上,不能保证每个文件名的句点都在同一个地方.我希望通过驱动器进行递归,在文件名本身中搜索句点(减去扩展名),删除句点然后将扩展名附加到其上.
有没有人知道这样做的更好的方法或者如何执行我希望做的事情?注意,regEx是该项目的要求.
编辑:而不是看到1.0.1.21 - 机密... doc,我想看看:10121 - Confidential.doc
对于另一个文件名,而不是Accounting.Files.doc,我想看看AccountingFiles. DOC
简单的问题,但我还没有找到答案。
这里有人知道如何抑制布尔字段显示 "False" 吗?
例如,在我的报告中,我有一些字段显示 True 或 False ......我宁愿只看到 True 并让水晶报告抑制“False”
有人知道怎么做吗?