包含文件的文件夹结构示例:
我希望将此结构转换为电子表格,如下所示:
column 1 column 2
1 folder 1
1.1 subfolder 1
1.1.1 file 1
1.1.2 file 2
1.2 subfolder 1
1.2.1 file 3
2 folder 2
2.1 file 4
我怎样才能做到最好?我尝试过VBA宏。我确实设法列出所有文件和文件夹。但编号没有成功。请注意,子文件夹的深度不限于此示例。从理论上讲,编号可以超过1.1.1.1.1.1.1.1.1等。
Sub FolderNames()
Application.ScreenUpdating = False
Dim xPath As String
Dim xWs As Worksheet
Dim fso As Object, j As Long, folder1 As Object
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the folder"
.Show
End With
On Error Resume Next
xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
'Application.Workbooks.Add
Set xWs = Application.ActiveSheet
xWs.Cells(1, 1).Value = xPath
xWs.Cells(2, 1).Resize(1, 2).Value = Array("Level", "Name")
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.getFolder(xPath)
getSubFolder folder1
Application.ScreenUpdating = True
End Sub
Sub getSubFolder(ByRef prntfld As Object)
Dim xFolderName As String
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Dim filecounter As Integer
Dim foldercounter As Integer
Dim SubFolder As Object
Dim subfld As Object
Dim xRow As Long
foldercounter = 1
For Each SubFolder In prntfld.SubFolders
subcount = subcount + 1
filecounter = 1
xFolderName = SubFolder.Path
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.getFolder(xFolderName)
xRow = Range("A1").End(xlDown).Row + 1
Cells(xRow, 1).Resize(1, 3).Value = Array(foldercounter, filecounter, SubFolder.Name)
For Each xFile In xFolder.Files
xRow = Range("A1").End(xlDown).Row + 1
Cells(xRow, 1).Resize(1, 3).Value = Array(foldercounter, filecounter, xFile.Name)
filecounter = filecounter + 1
Next xFile
foldercounter = foldercounter + 1
Next SubFolder
For Each subfld In prntfld.SubFolders
getSubFolder subfld
Next subfld
End Sub
答案 0 :(得分:0)
我认为以下内容应该有效,这需要您的一般概念,但重组它们。我向getSubFolder添加了另一个参数prntLevel,它是一个以子文件夹或文件的索引为前缀的字符串。它变成了一个越来越长的数字序列,因为当程序更深入到文件夹结构时,程序会自行回过头来。
另外,为了便于理解,每个子文件夹都获得一个0的索引,以区别于其中包含的文件。否则,例如1.1可以是第一个文件或第一个子文件夹,它开始变得不明显我的意见是你的树变深了。
public class Cart extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
TextView txtTotalPrice;
FButton btnPlace;
List<Order> cart=new ArrayList<>();
CartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
//Firebase
database = FirebaseDatabase.getInstance();
requests = database.getReference("Requests");
//Init
recyclerView = (RecyclerView) findViewById(R.id.listCart);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
txtTotalPrice = (TextView) findViewById(R.id.total);
btnPlace =(FButton)findViewById(R.id.btnPlaceOrder);
btnPlace.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
showAlertDialog();
}
});
loadlistfood();
}
private void showAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
alertDialog.setTitle("One more step!");
alertDialog.setMessage("Enter your Address: ");
final EditText edtAddress = new EditText(Cart.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
edtAddress.setLayoutParams(lp);
alertDialog.setView(edtAddress);//add edt text to alert box
alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
edtAddress.getText().toString(),
txtTotalPrice.getText().toString(),
cart
);
//submit to firebase
//we will use system.currntMill to key
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Delete cart
new Database(getBaseContext()).cleanCart();
Toast.makeText(Cart.this, "Thank you,Order placed", Toast.LENGTH_SHORT).show();
finish();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private void loadlistfood() {
cart=new Database(this).getCarts();
adapter=new CartAdapter(cart,this);
recyclerView.setAdapter(adapter);
//calculate price
int total=0;
for(Order order:cart)
total+=(Integer.parseInt(order.getPrice()))*
(Integer.parseInt(order.getQuantity()));
Locale locale=new Locale("en","US");
NumberFormat fmt=NumberFormat.getCurrencyInstance(locale);
txtTotalPrice.setText(fmt.format(total));
}
}