Copy all files recursively from one folder to another
VB.NET
JAVA
Code
Imports System.IO
Public Class JoeyHandler
Public Shared Sub Handle(ByVal originalFolder As String, ByVal newFolder As String)
Dim files As FileInfo() = JoeyFileHelper.FindAllFilesRecursively(originalFolder, "*.java")
For Each fi As FileInfo In files
Dim originalPath As String = fi.FullName
Dim newPath As String = GetNewPath(originalPath, originalFolder, newFolder)
JoeyFileHelper.CopyOver(originalPath, newPath)
Next
End Sub
Private Shared Function GetNewPath(ByVal originalPath As String, ByVal originalFolder As String, ByVal newFolder As String) As String
Dim startIndex As Integer = originalFolder.Length
Dim length As Integer = originalPath.Length - startIndex
Dim remaining As String = originalPath.Substring(startIndex, length)
Return newFolder + remaining
End Function
End Class
Imports System.IO
Public Class JoeyHandler
Public Shared Sub Handle(ByVal originalFolder As String, ByVal newFolder As String)
Dim files As FileInfo() = JoeyFileHelper.FindAllFilesRecursively(originalFolder, "*.java")
For Each fi As FileInfo In files
Dim originalPath As String = fi.FullName
Dim newPath As String = GetNewPath(originalPath, originalFolder, newFolder)
JoeyFileHelper.CopyOver(originalPath, newPath)
Next
End Sub
Private Shared Function GetNewPath(ByVal originalPath As String, ByVal originalFolder As String, ByVal newFolder As String) As String
Dim startIndex As Integer = originalFolder.Length
Dim length As Integer = originalPath.Length - startIndex
Dim remaining As String = originalPath.Substring(startIndex, length)
Return newFolder + remaining
End Function
End Class
Code
Imports System.IO
Public Class JoeyFileHelper
Public Shared Function FindAllFilesRecursively(ByVal folder As String, ByVal searchPattern As String) As FileInfo()
If Directory.Exists(folder) Then
Dim directory As New DirectoryInfo(folder)
Return directory.GetFiles(searchPattern, SearchOption.AllDirectories)
Else
Return Nothing
End If
End Function
Public Shared Sub CopyOver(ByVal originalPath As String, ByVal newPath As String)
Dim fi As FileInfo = New FileInfo(newPath)
If Not Directory.Exists(fi.DirectoryName) Then
Directory.CreateDirectory(fi.DirectoryName)
End If
If File.Exists(newPath) Then
File.Delete(newPath)
End If
File.Copy(originalPath, newPath)
End Sub
End Class
Imports System.IO
Public Class JoeyFileHelper
Public Shared Function FindAllFilesRecursively(ByVal folder As String, ByVal searchPattern As String) As FileInfo()
If Directory.Exists(folder) Then
Dim directory As New DirectoryInfo(folder)
Return directory.GetFiles(searchPattern, SearchOption.AllDirectories)
Else
Return Nothing
End If
End Function
Public Shared Sub CopyOver(ByVal originalPath As String, ByVal newPath As String)
Dim fi As FileInfo = New FileInfo(newPath)
If Not Directory.Exists(fi.DirectoryName) Then
Directory.CreateDirectory(fi.DirectoryName)
End If
If File.Exists(newPath) Then
File.Delete(newPath)
End If
File.Copy(originalPath, newPath)
End Sub
End Class
JAVA
Code
import java.io.*;
public class Handler {
public static void handler(String originalFolder, String newFolder) {
File[] files = FileHelper.findAllFilesRecursively(originalFolder, ".*\\.java");
for (File file : files) {
String originalPath = file.getAbsolutePath();
String newPath = getNewPath(originalPath, originalFolder, newFolder);
FileHelper.copyOver(originalPath, newPath);
}
}
private static String getNewPath(String originalPath, String originalFolder, String newFolder) {
int startIndex = originalFolder.length();
int endIndex = originalPath.length();
String remaining = originalPath.substring(startIndex, endIndex);
return newFolder + remaining;
}
}
import java.io.*;
public class Handler {
public static void handler(String originalFolder, String newFolder) {
File[] files = FileHelper.findAllFilesRecursively(originalFolder, ".*\\.java");
for (File file : files) {
String originalPath = file.getAbsolutePath();
String newPath = getNewPath(originalPath, originalFolder, newFolder);
FileHelper.copyOver(originalPath, newPath);
}
}
private static String getNewPath(String originalPath, String originalFolder, String newFolder) {
int startIndex = originalFolder.length();
int endIndex = originalPath.length();
String remaining = originalPath.substring(startIndex, endIndex);
return newFolder + remaining;
}
}
Code
import java.io.*;
import java.util.*;
public class FileHelper {
public static File[] findAllFilesRecursively(String folder, String searchPattern) {
File directory = new File(folder);
if (directory.exists()) {
ArrayList<File> files = new ArrayList<File>();
File[] entries = directory.listFiles();
for (File entry : entries) {
if (entry.isFile()) {
if (RegularExpressionHelper.match(entry.getAbsolutePath(), searchPattern)) {
files.add(entry);
}
} else {
File[] subFiles = findAllFilesRecursively(entry.getAbsolutePath(), searchPattern);
for (File file : subFiles) {
files.add(file);
}
}
}
return files.toArray(new File[files.size()]);
} else {
return null;
}
}
public static void copyOver(String originalPath, String newPath) {
File file = new File(newPath);
File folder = new File(file.getParent());
if (!folder.exists()) {
folder.mkdirs();
}
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
InputStream is = new FileInputStream(new File(originalPath));
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
} catch (IOException ioe) {
}
}
}
import java.io.*;
import java.util.*;
public class FileHelper {
public static File[] findAllFilesRecursively(String folder, String searchPattern) {
File directory = new File(folder);
if (directory.exists()) {
ArrayList<File> files = new ArrayList<File>();
File[] entries = directory.listFiles();
for (File entry : entries) {
if (entry.isFile()) {
if (RegularExpressionHelper.match(entry.getAbsolutePath(), searchPattern)) {
files.add(entry);
}
} else {
File[] subFiles = findAllFilesRecursively(entry.getAbsolutePath(), searchPattern);
for (File file : subFiles) {
files.add(file);
}
}
}
return files.toArray(new File[files.size()]);
} else {
return null;
}
}
public static void copyOver(String originalPath, String newPath) {
File file = new File(newPath);
File folder = new File(file.getParent());
if (!folder.exists()) {
folder.mkdirs();
}
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
InputStream is = new FileInputStream(new File(originalPath));
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
} catch (IOException ioe) {
}
}
}
Code
import java.util.regex.*;
public class RegularExpressionHelper {
public static boolean match(String toCheck, String expression) {
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(toCheck);
return matcher.find();
}
}
import java.util.regex.*;
public class RegularExpressionHelper {
public static boolean match(String toCheck, String expression) {
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(toCheck);
return matcher.find();
}
}