SoFunction
Updated on 2024-11-19

Python realize batch get the file name under the current folder

Recently, when I was communicating with my netizens, they recommended a video, I opened it, it was a mobile video to get all the file names in the current directory manually. The method used is to copy the paths of all the files in Win11, then paste them into Excel, remove the redundancies by Find Replace and Split, and finally get the file names.

This method was ingenious and got the filename without resorting to programming or less common software. I then explored some easy ways to share them with you.

Let's start with the requirements:Get all png files in the current folder, excluding extensions.

First, copy the path method - simple and convenient

Use the shortcut ctrl+a to go all the way to the current file, then select [Copy Path] at the top of Explorer to get the paths of all the files.

Get file path

Next, these paths to the Excel file, through the [Find and Replace] to get all the file names, which is not png can be removed from the file, and then column A in accordance with the [...]. Separation, so that you get the file names of all the pictures.

Find Replace

This method is simple, practical, no technical content, white people can also easily get started. If the current directory is mixed with multiple types of files, then you have to delete the non-compliant paths one by one. The solution is to arrange the files in accordance with the type, and then selected to obtain the path.

Second, Dos command method

Dos commands are a built-in feature of windows that is constantly being updated but never abandoned. I think I learned computer science from dos at the beginning, and its commands are sometimes very powerful and much simpler to use than those in windows.

In order to get the filenames in the current directory, we can enter the dos interface by typing [cmd] above the current directory.

Enter dos in the current directory

Then, enter the command:

dir *.png /b > 

Write filename to csv file

In this way, we have written the file names of all the files with the extension png. We can open this csv file in Excel and just disaggregate it.

The dos command writes the name of a file in the current directory

This method significantly reduces the number of steps, not only the filename filtering, but also do not have to copy and paste, almost a step to the filename written to the Excel file. Of course, this method is simple, but can not extract the file name in the subdirectory.

III. Batch command method

Batch commands are more powerful than normal dos commands, as they can poke through subdirectories and even create files in bulk, for example, the above dos command to write a filename to a csv file can also be rewritten as the following batch command:

for %F in (*.png) do @echo %F>>

The function of the above command is to list the filenames (extensions) of all .png files in the current directory and append the results to the file.

If you don't want to carry an extension, you can also do it with one click with the following code:

for %F in (*.png) do @echo %~nF>>

In the above two commands, the for loop iterates through all the .png files in the current directory.

for %F in (*.png): This is the syntax of the for command, which iterates through all files in the current directory with filename matching *.png. %F is a loop variable that represents the filename currently being processed.

do @echo %~nF>>: The do keyword indicates the command to be executed at each iteration. @echo %~nF is used to print the filename portion of the current filename (without extension).

%~nF is the syntax to get the filename part of %F.

>> Indicates that the output is appended to the end of the file instead of overwriting the original content.

IV. Python command method

If the computer has python installed, you can also use Python programming methods, import os or pathlib module, by traversing the list of each element, append them to the Excel table, and then save it can be easily realized by specifying the file name.

#Import Module
import os
from openpyxl import Workbook
 
# Create a workbook object
wb = Workbook()
 
# Activate the first worksheet
ws = 
 
# Your list data
data = [file for file in () if (".png")]
# If you don't want the extension, you can write it like this:
# data = [(file)[0],for file in () if (".png")]
# Write data row by row to worksheet
        
for row in data:
    ([row])
 
# Save the workbook
('')

V. VBA Methods

If you often need to get the names of all the files in the current directory, you can use the following VBA code:

Sub GetFileNames()
    Dim FileSystem As Object
    Dim Folder As Object
    Dim File As Object
    Dim i As Integer
    
    ' emptySheet1data contained in
    Sheets("Sheet1").
    
    ' establishFileSystemboyfriend
    Set FileSystem = CreateObject("")
    
    ' Get current directory path
    Set Folder = ()
    
    ' existSheet1Show filenames in
    i = 1
    For Each File In 
        Sheets("Sheet1").Cells(i, 1).Value = 
        i = i + 1
    Next File
End Sub

We can copy and paste the above code into Excel's VBA editor (press Alt + F11 to open it), then find ThisWorkbook in Microsoft Excel Objects in Project Explorer, double-click to open it, and paste the code into the code window that opens. Then close the VBA editor and save the file.

Whenever you run the GetFileNames macro, it will list all the file names in the current directory in Sheet1. This is shown in the following figure:

This method is a one-step process that easily gets all the file names. However, be careful, VBA will run when the data in Sheet1 will be cleared, so ensure that the current worksheet is sheet1, and then no other important data.

VI. Post-learning reflections

The same function we can use a variety of methods to achieve, from manual copy and paste to later dos commands or programming, I can easily achieve to reach the final goal. Everyone can take it according to their own preferences. The first method is simple and easy to remember, and is recommended for beginners. The second and third methods involve dos commands, which need to be memorized. The third and fourth are saved in py and xls files respectively, basically out of the box.

This article on Python to achieve bulk access to the current folder file name of the article is introduced to this, more related Python to get the file name of the content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!