Windows systems use powershell to read file names and other information

Table of contents

Get file name saved in text file

Use one line of commands in the terminal to achieve

Use script to implement

Get file name and file size, save in text file

Use one line of commands in the terminal to achieve

Use script to implement


Get file name saved in text file

Use one line of commands in the terminal to achieve

If you only use one line of commands in the terminal, you can use semicolons; to string multiple commands together. Here's an example of a one-line command using PowerShell:

Get-ChildItem -Path "C:\你的文件夹路径" | ForEach-Object { $_.Name } > "C:\output.txt"

This command will obtain and save all file names in the specified path to the C:\output.txt file.

Replace "C:\your folder path" with the path of the folder you want to operate.

Replace "C:\output.txt" with the other file name you want to save to.

Please note that this way, if the filename already exists in C:\output.txt, it will be overwritten. If you wish to append rather than overwrite, you can use the >> operator, for example:

Get-ChildItem -Path "C:\你的文件夹路径" | ForEach-Object { $_.Name } >> "C:\output.txt"

Use script to implement

To  Get-ChildItem save the read file name to a text file, you can use the following PowerShell script:

# 设置文件夹路径
$folderPath = "C:\你的文件夹路径"

# 获取文件夹中的所有文件
$files = Get-ChildItem -Path $folderPath

# 创建一个空白的文本文件来保存文件名
$outputFilePath = "C:\output.txt"
New-Item -Path $outputFilePath -ItemType file -Force

# 遍历文件并将文件名追加到文本文件
foreach ($file in $files) {
    $fileName = $file.Name

    # 将文件名追加到文本文件
    Add-Content -Path $outputFilePath -Value $fileName
}

# 完成后输出信息
Write-Host "文件名已保存到 $outputFilePath"

In this script, we iterate through  $files each file object in , extract the file name and append it to the specified text file  $outputFilePath . This way, the final text file will contain only the file names, each on one line.

Please make sure that before running the script, the  $folderPath and  $outputFilePath variables are set to the actual folder path and the text file path where the file name is saved.

Get file name and file size, save in text file

Get the file name and file size and save them in a text file.

Use one line of commands in the terminal to achieve

Here's how to do it with a one-line command in the terminal:

Get-ChildItem -Path "C:\你的文件夹路径" | ForEach-Object { $_.Name + ", " + $_.Length + " bytes" } | Out-File -FilePath "C:\output.txt" -Append

This command will get all the file names and file sizes in the specified folder and append them to the `C:\output.txt` file in comma separated form.

Use script to implement

Here is an example PowerShell script:

# 设置文件夹路径
$folderPath = "C:\你的文件夹路径"

# 获取文件夹中的所有文件
$files = Get-ChildItem -Path $folderPath

# 创建一个空白的文本文件来保存文件名和文件大小
$outputFilePath = "C:\output.txt"
New-Item -Path $outputFilePath -ItemType file -Force

# 遍历文件并将文件名和文件大小追加到文本文件
foreach ($file in $files) {
    $fileName = $file.Name
    $fileSize = "$($file.Length) bytes"
    $fileInfo = "$fileName, $fileSize"

    # 将文件名和文件大小追加到文本文件
    Add-Content -Path $outputFilePath -Value $fileInfo
}

# 完成后输出信息
Write-Host "文件名和大小已保存到 $outputFilePath"

This script will perform the same task as the above one-line command, but in script form to provide more readability and maintainability. Please make sure that before running the script, the `$folderPath` and `$outputFilePath` variables are set to the actual folder path and the text file path holding the file name and file size.

Guess you like

Origin blog.csdn.net/weixin_56337147/article/details/133881034