将文件移动到包含部分文件名的现有文件夹中
Move files into existing folders containing part of the filename
你会认为这是重复的 post,但我已经研究了 2 天了。我尝试过 filebot、filejuggler、robobasket、moveout 和高级重命名器。我在网上搜索过,没有解决我的问题。
我有一个名为 12345678.txt
的文件,想将其移动到目录 C:\folders345678-bla bla bla\
我尝试的一切都将文件移动到它创建的与文件同名的文件夹中。但是我的文件夹在文件名后有其他字符。
这里是确切的真实情况:
在此目录中:C:\F\Répertoires\FISCALITE\T220
我有以客户编号命名的文件,例如75063420.txt
。我需要将该文件移动到此文件夹中:C:\F\Répertoires\CLIENTS0634_Entreprises nameofcompany\
因此只需要文件名的前 6
个字符来匹配名称字符数多于文件名的现有文件夹。
我可能有 5000 个文件要移动,需要一种自动化的方式。
这是一种 powershell 方法,没有任何错误或冲突处理,例如具有相同六个起始字符的多个客户端目录或客户端文件夹中已经存在的文件。
$FilesToMove = 'C:\F\Répertoires\FISCALITE\T220'
$TargetPath = 'C:\F\Répertoires\CLIENTS'
$Files = Get-ChildItem -Path $FilesToMove -File
foreach ($File in $Files) {
$PathToMove = Get-ChildItem -Path $TargetPath -Directory -Filter "$(($File.Basename).Substring(0,6))*" | Select-Object -First 1
Write-Output "Moving File $File to $PathToMove"
Move-Item -Path $File.FullName -Destination "$($PathToMove.Fullname)$($File.Name)"
}
这是一个未经测试的 batch-file:
@Set "SourcePath=C:\F\Répertoires\FISCALITE\T220"
@Set "TargetPath=C:\F\Répertoires\CLIENTS"
@Set "FileMask=??????*.*"
@For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" "%SourcePath%":"%FileMask%" 2>NUL"'
)Do @Set "BaseName=%%~nA"&For /F "Delims=" %%B In (
'Dir /B/AD "%TargetPath%\%%BaseName:~,6%%_*" 2^>NUL'
)Do @Move /Y "%%A" "%TargetPath%\%%~nxB">NUL 2>&1
在线 3
,我使用通配符 *
来匹配所有扩展 ??????*.*
。如果您只是在寻找特定的扩展名,比如在您的示例中说 .txt
,您可以使用 ??????*.txt
,这将匹配所有 .txt
文件,其基本名称中包含六个或更多字符.如果您修改源 and/or 目标路径,请确保它没有尾随路径分隔符 \
.
如果 %TargetPath%
中可能有多个具有相同 6
字符前缀的目录,我建议您要么接受将其移动到第一个返回,或将 move
命令更改为 copy
,然后在之后执行 delete
。
这是 filebot
的解决方案:
filebot -rename "C:\F\Répertoires\FISCALITE\T220" --db file --output "C:\F\Répertoires\CLIENTS" --format "{output.list().find{ it.startsWith(fn) } / fn}"
您需要在测试和制作命令原型时添加 --action TEST
或 --action COPY
。
你会认为这是重复的 post,但我已经研究了 2 天了。我尝试过 filebot、filejuggler、robobasket、moveout 和高级重命名器。我在网上搜索过,没有解决我的问题。
我有一个名为 12345678.txt
的文件,想将其移动到目录 C:\folders345678-bla bla bla\
我尝试的一切都将文件移动到它创建的与文件同名的文件夹中。但是我的文件夹在文件名后有其他字符。
这里是确切的真实情况:
在此目录中:C:\F\Répertoires\FISCALITE\T220
我有以客户编号命名的文件,例如75063420.txt
。我需要将该文件移动到此文件夹中:C:\F\Répertoires\CLIENTS0634_Entreprises nameofcompany\
因此只需要文件名的前 6
个字符来匹配名称字符数多于文件名的现有文件夹。
我可能有 5000 个文件要移动,需要一种自动化的方式。
这是一种 powershell 方法,没有任何错误或冲突处理,例如具有相同六个起始字符的多个客户端目录或客户端文件夹中已经存在的文件。
$FilesToMove = 'C:\F\Répertoires\FISCALITE\T220'
$TargetPath = 'C:\F\Répertoires\CLIENTS'
$Files = Get-ChildItem -Path $FilesToMove -File
foreach ($File in $Files) {
$PathToMove = Get-ChildItem -Path $TargetPath -Directory -Filter "$(($File.Basename).Substring(0,6))*" | Select-Object -First 1
Write-Output "Moving File $File to $PathToMove"
Move-Item -Path $File.FullName -Destination "$($PathToMove.Fullname)$($File.Name)"
}
这是一个未经测试的 batch-file:
@Set "SourcePath=C:\F\Répertoires\FISCALITE\T220"
@Set "TargetPath=C:\F\Répertoires\CLIENTS"
@Set "FileMask=??????*.*"
@For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" "%SourcePath%":"%FileMask%" 2>NUL"'
)Do @Set "BaseName=%%~nA"&For /F "Delims=" %%B In (
'Dir /B/AD "%TargetPath%\%%BaseName:~,6%%_*" 2^>NUL'
)Do @Move /Y "%%A" "%TargetPath%\%%~nxB">NUL 2>&1
在线 3
,我使用通配符 *
来匹配所有扩展 ??????*.*
。如果您只是在寻找特定的扩展名,比如在您的示例中说 .txt
,您可以使用 ??????*.txt
,这将匹配所有 .txt
文件,其基本名称中包含六个或更多字符.如果您修改源 and/or 目标路径,请确保它没有尾随路径分隔符 \
.
如果 %TargetPath%
中可能有多个具有相同 6
字符前缀的目录,我建议您要么接受将其移动到第一个返回,或将 move
命令更改为 copy
,然后在之后执行 delete
。
这是 filebot
的解决方案:
filebot -rename "C:\F\Répertoires\FISCALITE\T220" --db file --output "C:\F\Répertoires\CLIENTS" --format "{output.list().find{ it.startsWith(fn) } / fn}"
您需要在测试和制作命令原型时添加 --action TEST
或 --action COPY
。