除了浏览目录之外,我没有任何Unix编码经验,并且想知道是否有人可以帮我写一个脚本来复制我在.NET中所做的事情。有人告诉我,由于.NET代码是远程部署的,因此运行得更快,有时使用映射驱动器来访问大量文件夹的速度很慢。我基本上是通过根据文件名将文件从选定文件夹移动到一组文件夹并根据文件名中包含的文件日期对每个文件夹进行排序来对文件进行排序。
Private Sub moveAllfiles(ByVal directoryStuff As String)
Dim templist As New ArrayList
Dim finalDestination As String = String.Empty
Dim pathName As String = String.Empty
Dim fileToDelete As String = String.Empty
Dim folderDate As String = String.Empty
Dim counter As Integer = 0
Dim folders = Directory.EnumerateDirectories(directoryStuff)
For Each item In folders
Dim files As String() = Directory.GetFiles(item)
//' if directory is empty delete folder
If Directory.GetFiles(item).Count = 0 Then
Directory.Delete(item)
Continue For
End If
For i As Integer = 0 To files.Count - 1
Try
counter += 1
Dim oInfo As New FileInfo(files(i))
// ' if file is empty or small delete it
If oInfo.Length <= 1 Then
File.Delete(files(i))
Continue For
End If
If Not files(i).EndsWith(".gz") Then
CompressFiles(files(i))
Continue For
End If
Dim objInfo As New FileInfo(files(i))
If Not objInfo.Name.Contains("Data_G_E") Then
If objInfo.Name.Contains("Data_G_P") Then
Dim pfiledate As String = objInfo.Name.Remove(20)
pfiledate = pfiledate.Remove(7, 5)
Dim ftempDirectory As String
= "M:\Archive\DataP\" + pfiledate & "\"
If Not Directory.Exists(ftempDirectory) Then
Directory.CreateDirectory(ftempDirectory)
Dim destdirectory As String = ftempDirectory
Dim ff As String = files(i)
fileToDelete = ff
File.Move(ff, destdirectory + objInfo.Name)
File.Delete(ff)
Else
Dim destdirectory As String = ftempDirectory
Dim ff As String = files(i)
fileToDelete = ff
File.Move(ff, destdirectory + objInfo.Name)
File.Delete(ff)
End If
End If
Continue For
End If
Dim filedate As String = objInfo.Name.Remove(20)
filedate = filedate.Remove(7, 5)
Dim tempDirectory As String = String.Empty
tempDirectory = "M:\Archive\DataE\" + filedate & "\"
If Not Directory.Exists(tempDirectory) Then
Directory.CreateDirectory(tempDirectory)
Dim destdirectory As String = tempDirectory
Dim ff As String = files(i)
fileToDelete = ff
File.Move(ff, destdirectory + objInfo.Name)
File.Delete(ff)
Else
Dim destdirectory As String = tempDirectory
Dim ff As String = files(i)
fileToDelete = ff
File.Move(ff, destdirectory + objInfo.Name)
File.Delete(ff)
End If
Catch ex As Exception
If ex.Message.Contains("already exists") Then
File.Delete(fileToDelete)
Console.WriteLine("DELETING OLD FILE " & fileToDelete)
End If
Continue For
End Try
Next
Next
End Sub
不确定逻辑是否有意义,但基本上它会搜索所有子文件夹中的文件。剥离文件名以获取指示文件应该去的位置的日期和名称。使用日期在目标目录中创建文件夹并相应地移动文件。如果有人可以帮助您开始或提出更好的方法来做到这一点,我将非常感激。
答案 0 :(得分:2)
在您的.NET代码中看起来没有太近(实际上,几乎没有),看起来您想要查找给定目录中或下面的所有文件,从文件名中的固定位置提取字符串以使用作为目标目录,然后将每个文件移动到该目录。如果这确实是你想要做的,那就相当简单了。要将目录/p/a/t/h
中或下面的每个文件移动到/path2/xxx
,其中xxx位于文件名中的位置7到10(我随机选择索引7和10),只需执行:
find /p/a/t/h -type f -exec sh -c 'd="/path2/${0:7:3}";
mkdir -p "$d"; mv -i "$0" "$d"' {} \;
如果要覆盖任何文件,-i
的{{1}}标志将导致交互式提示,并且在此处作为安全捕获,以帮助防止不小心的文件被吹走。 (但是,您,读者,永远不会执行您不完全理解的任何代码,因此这不是必需的!)您可能希望将其替换为mv
或者只是删除它。另请注意,只有在文件名是病态的情况下才需要双引号。 (例如,如果它们包含空格。)