将文件路径嵌入到名称中

时间:2017-09-27 15:58:45

标签: python powershell file-rename batch-rename

我有类似的问题:我想批量重命名文件,如:

c:>/folder/path_a/to/my_first_file.txt
c:>/folder/path_b/to/my_2_nd_file.txt
c:>/folder/path_c/to/my_bla_bla.txt
c:>/folder/path_d/to/pippo.txt
c:>/folder/path_d/to/pluto.txt
c:>/folder/path_d/to/my_bla_bla.txt

我会将它们重命名为:

c:>/folder/path_a-to_my_first_file.txt
c:>/folder/path_b-to-my_2_nd_file.txt
c:>/folder/path_c-to-my_bla_bla.txt
c:>/folder/path_d-to-pippo.txt
c:>/folder/path_d-to-pluto.txt
c:>/folder/path_d-to-my_bla_bla.txt

'嵌入'文件名路径的一部分

有人可以帮我(我正在使用Windows)吗?

找到this,但我不知道怎么告诉PS'只抓住子目录名'

编辑: 在python上我会做这样的事情(对不起,如果代码不起作用,但在这里我没有python):

for dirname, _, fnames in os.walk('.'):
    first=dirname.replace('\','-')
    for fname in fnames:
        name="{}-{}".format(first,fname)
        os.rename("{}\{}".format(dirname,fname),"{}"name)

for for dirname,_,fname in os.walk('.'):
    os.remove(dirname)

=================更新===================

由于处理powershell是一个耗时的问题,我把我的数据放在一个USB驱动器上,并在另一台机器上使用下面的python脚本来完成这项任务:(该脚本还会更改文件名以删除之前的'。 c'在全体会议中加入的延伸)

from os import rename, walk, chdir
EXT = '.c'
PATH = 'folder'    

chdir(PATH)
for cart, _, files in walk("."):
        for i in files:
                if os.path.isfile("{}/{}".format(cart,i)):
                        if EXT in i:
                                nn = i.replace(EXT,'') #removes inner extension
                                rename('{}/{}'.format(cart,i),'{}-{}'.format(cart,nn)) #changes the path

2 个答案:

答案 0 :(得分:0)

这应该这样做。

   $root = "c:>/folder/"
   $paths = "c:>/folder/path_a/to/my_first_file.txt",`
    "c:>/folder/path_b/to/my_2_nd_file.txt",`
    "c:>/folder/path_c/to/my_bla_bla.txt",`
    "c:>/folder/path_d/to/pippo.txt",`
    "c:>/folder/path_d/to/pluto.txt",`
    "c:>/folder/path_d/to/my_bla_bla.txt"

    function update-paths
    {

        params(
            string[] $paths
        )


        $paths | foreach {

            $path = $_.substring($root.Length,$_.Length-$root.Length)


            Write-Output ($root + ($path.replace("/","-")))

        }

    }

    update-paths -paths $paths

c:>/folder/path_a-to-my_first_file.txt
c:>/folder/path_b-to-my_2_nd_file.txt
c:>/folder/path_c-to-my_bla_bla.txt
c:>/folder/path_d-to-pippo.txt
c:>/folder/path_d-to-pluto.txt
c:>/folder/path_d-to-my_bla_bla.txt

答案 1 :(得分:0)

展平/折叠目录树不仅仅是重命名,而是移动文件,然后删除(空)目录。

## Q:\Test\2017\09\27\SO_46452350.ps1
$Base = 'C:\folder\'
Set-Location $Base
Get-ChildItem -Dir | ForEach-Object {
    Get-ChildItem -Path $_ -File -Recurse |
        Move-Item -Destination {Join-Path $Base ($_.FullName.Replace($Base,'').Replace('\','_'))} 
    $_ | Remove-Item -Recurse
}

示例树:

> tree /F
C:.
└───folder
    ├───path_a
    │   └───to
    │           my_first_file.txt
    │
    ├───path_b
    │   └───to
    │           my_2_nd_file.txt
    │
    ├───path_c
    │   └───to
    │           my_bla_bla.txt
    │
    └───path_d
        └───to
                my_bla_bla.txt
                pippo.txt
                pluto.txt

运行脚本后:

> tree /F
C:.
│
└───folder
        path_a_to_my_first_file.txt
        path_b_to_my_2_nd_file.txt
        path_c_to_my_bla_bla.txt
        path_d_to_my_bla_bla.txt
        path_d_to_pippo.txt
        path_d_to_pluto.txt