将文件路径嵌入名称
Embedding file paths to names
我有类似的问题:我想批量重命名文件,如:
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
'embedding' 部分文件名路径
有人可以帮助我吗(我正在使用 Windows)?
找到this但不知道怎么说PS'grab only subdirectorys name'
编辑:
在 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
这应该可以做到。
$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
Flattening/collapsing 目录树不只是重命名而是移动文件然后删除(空)目录。
## Q:\Test17\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
我有类似的问题:我想批量重命名文件,如:
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
'embedding' 部分文件名路径
有人可以帮助我吗(我正在使用 Windows)?
找到this但不知道怎么说PS'grab only subdirectorys name'
编辑: 在 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
这应该可以做到。
$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
Flattening/collapsing 目录树不只是重命名而是移动文件然后删除(空)目录。
## Q:\Test17\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