Renaming files in powershell using the folder name

后端 未结 3 955
生来不讨喜
生来不讨喜 2021-02-06 16:05

Using Powershell, I want to rename files in folders by using the name of the folder that the files are in. So in my C:\\temp directory, there are 3 folders called \'aaa\', \'bb

相关标签:
3条回答
  • 2021-02-06 16:30

    Try this

    $dirname = resolve-path . | split-path -leaf
    Get-ChildItem -Name | Foreach { Rename-Item $_  ( $dirname + $_ ) }
    

    Be careful not to destroy/delete any of your files. No guarantee.

    0 讨论(0)
  • 2021-02-06 16:41

    [IO.Directory]::GetCurrentDirectory().split('\\')[-1] will give you the directory you are in.

    0 讨论(0)
  • 2021-02-06 16:52

    This will rename the files and put an underscore ('_') between the folder name and the file name:

    Get-ChildItem C:\temp -Filter *.txt -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}
    
    0 讨论(0)
提交回复
热议问题