使用powershell循环浏览一个文件夹中的文件,并使用这些名称创建txt文件

时间:2019-03-03 12:20:04

标签: powershell loops

我正在寻找一个脚本来为文件夹中的每个文件创建一个文本文件。 .txt文件应使用该文件夹中每个文件的文件名,但扩展名为.txt而不是原始扩展名。

我只需要在几个文件夹中执行此操作,因此我想在该文件夹中打开PowerShell窗口,然后在该文件夹中创建文本文件(而不必指定路径)。

我发现该位置很近,但是它在生成的.txt文件中添加了文件夹名称和扩展名

Get-ChildItem -Path "C:\Temp" -Recurse | ForEach {
    [System.IO.File]::WriteAllText("C:\Temp" + $_.Name + ".txt", $_.FullName)
}

2 个答案:

答案 0 :(得分:0)

您将要过滤出目录和*.txt文件:

Get-ChildItem -Path "C:\Temp" -Recurse -File |Where-Object {$_.Extension -ne '.txt'} |ForEach-Object {
    [System.IO.File]::WriteAllText("C:\Temp" + $_.BaseName + ".txt", $_.FullName)
}

我也将$_.Name更改为$_.BaseName以获取没有扩展名的文件名

答案 1 :(得分:0)

如果您只想处理当前文件夹,

  1. 删除-Recurse
  2. 使用-Path '.\*'
  3. 请记住,该命令将覆盖所有先前存在的.txt文件
    以及其他.txt个文件(来自具有相同BaseName的其他文件)。
    为了解决这个问题,您可以Group-Object个具有相同BaseName的文件。

我会将其作为函数存储在配置文件中,因此您始终可以访问它。

## Q:\Test\2019\03\03\SO_54968726.ps1
Function New-TextRef {
  Get-ChildItem -Path '.\*' -File |
    Where-Object Extension -Notin '.txt','.mkv','.avi','.mp4','.jpg','.png','.wav'|
      Group-Object BaseName | ForEach-Object{
        [System.IO.File]::WriteAllText(("{0}\{1}.txt" -f (Pwd).Path,$_.Name),($_.Group -join "`n"))
      }
}

无论您如何命名.ps1脚本文件,您也可以.dot源文件(加载到当前会话中)。

启用后,您只需在当前文件夹中运行名为New-TextRef的函数并创建.txt文件即可。

示例输出:

> Get-ChildItem

    Directory: Q:\Test\2019\03\03

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019-03-03     22:30            377 netsh_wlan_show_profiles
-a----       2019-03-04     00:31            343 SO_54968726.ps1
-a----       2019-03-03     23:02            328 SO_54972848.ps1
-a----       2019-03-03     22:32            216 SU_1410873.cmd
-a----       2019-03-03     22:47            216 test.xml
-a----       2019-03-03     23:02            194 testNew.xml


> . .\SO_54968726.ps1

> New-TextRef

> Get-ChildItem

    Directory: Q:\Test\2019\03\03

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019-03-03     22:30            377 netsh_wlan_show_profiles
-a----       2019-03-04     00:36             43 netsh_wlan_show_profiles.txt
-a----       2019-03-04     00:31            343 SO_54968726.ps1
-a----       2019-03-04     00:36             34 SO_54968726.txt
-a----       2019-03-03     23:02            328 SO_54972848.ps1
-a----       2019-03-04     00:36             34 SO_54972848.txt
-a----       2019-03-03     22:32            216 SU_1410873.cmd
-a----       2019-03-04     00:36             33 SU_1410873.txt
-a----       2019-03-04     00:36             27 test.txt
-a----       2019-03-03     22:47            216 test.xml
-a----       2019-03-04     00:36             30 testNew.txt
-a----       2019-03-03     23:02            194 testNew.xml

> Select-String '^' *.txt

netsh_wlan_show_profiles.txt:1:Q:\Test\2019\03\03\netsh_wlan_show_profiles
SO_54968726.txt:1:Q:\Test\2019\03\03\SO_54968726.ps1
SO_54972848.txt:1:Q:\Test\2019\03\03\SO_54972848.ps1
SU_1410873.txt:1:Q:\Test\2019\03\03\SU_1410873.cmd
test.txt:1:Q:\Test\2019\03\03\test.xml
testNew.txt:1:Q:\Test\2019\03\03\testNew.xml
相关问题