我有一些檔案夾,比如
Computer Idea
confidenze fatali
Casa Naturale sette
和檔案之類的
2021-09-01 computer idea.rar
into confidenze fatali.pdf
casa naturale sette 454.jpg
我試著以這種方式移動
Computer Idea
|
|---> 2021-09-01 computer idea.rar
confidenze fatali
|
|---> into confidenze fatali.pdf
Casa Naturale sette
|
|---> casa naturale sette 454.jpg
例如,檔案和檔案夾computer idea都找到組合詞(在這種情況下,我們有相同的 2 個相鄰詞)。組合詞的分隔符只是一個空格。
我嘗試使用此批處理腳本但不起作用,我還要求提供 powershell 解決方案,因此我添加了有問題的標簽。2021-09-01 computer idea.rarComputer Idea
@Echo off
Pushd %1
For /d %%A in (*) do For /f "delims=" %%B in (
'Dir /B "*%%~nxA*" 2^>Nul '
) do If "%%~nxA" NEQ "%%~nxB" Move "%%~fB" "%%~fA\" 2>&1>>Nul
Popd
uj5u.com熱心網友回復:
使用 PowerShell,您可以:
$rootFolder = 'D:\Test'
# get a list of folders inside the root folder
$subFolders = Get-ChildItem -Path $rootFolder -Directory
# next loop through the folders and find files that match their names
foreach ($folder in $subFolders) {
# use the foldername as filter, surrounded with wildcard characters (*)
Get-ChildItem -Path $rootFolder -Filter "*$($folder.Name)*" -File | Move-Item -Destination $folder.FullName -WhatIf
}
該-WhatIf開關是一個安全開關,它只會在螢屏上顯示要移動的檔案,而不會實際移動任何東西。如果您發現該資訊是正確的,請-WhatIf從代碼中洗掉該開關并再次運行。
uj5u.com熱心網友回復:
我 99.9% 確信有更好的方法來做到這一點,但這是我的看法:
Get-ChildItem -Path "C:\Path" | Sort-Object -Property "Mode" |
ForEach-Object -Begin {
$files = @{}
} -Process {
if (-not $_.PSIsContainer) {
$files.Add($_.Name,$_.FullName) | Out-Null
}
else {
$folder = $_
$files.GetEnumerator() | ForEach-Object -Process {
if ($_.Key -match $folder.Name) {
Move-Item -Path $_.Value -Destination $folder.FullName -WhatIf
}
}
}
}
由于它們都位于同一個目錄中,因此您可以通過先將檔案發送到哈希表來過濾出哪個是檔案,哪個是檔案夾;這就是為什么我們過去Sort-Object按檔案型別在頂部列出檔案進行組織的原因。在我們的if陳述句中首先處理檔案后,我們知道我們的 else 塊(真的不需要)將只包含檔案夾。最后,我們可以列舉我們的哈希表,以便將檔案名與檔案夾名匹配。剩下要做的就是將檔案移動到相應的檔案夾中。
-WhatIf當您確定顯示的結果是您執行實際移動的結果時,請洗掉通用引數。
uj5u.com熱心網友回復:
For /d %%A in (*) do Move "*%%A*" "%%A\" 2>&1>>Nul
應該可以 - 首先在測驗目錄上嘗試。
鑒于某些目標目錄名稱可能是其他目錄名稱的進一步資訊......
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following setting for the source directory is a name
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"
:: remove variables starting #
FOR /F "delims==" %%b In ('set # 2^>Nul') DO SET "%%b="
FOR /f "tokens=1*delims=:" %%b IN ('dir /b /ad ^|findstr /n .') DO SET "#%%b=%%c"&SET /a count=%%b
:: find a directory that is not a substring of another & process it
:reselect
FOR /L %%b IN (1,1,%count%) DO IF DEFINED #%%b (
SET "targetstring=!#%%b!"
FOR /L %%c IN (%%b,1,%count%) DO IF %%b neq %%c IF DEFINED #%%c (FOR %%e IN ("!#%%b!") DO IF /i "!#%%c:%%~e=!" neq "!#%%c!" SET "targetstring=")
IF DEFINED targetstring (
SET "#%%b="
FOR /f "delims=" %%c IN ('dir /b /a-d "*!targetstring!*" 2^>nul') do Move "%%c" "!targetstring!\"
GOTO reselect
)
)
POPD
首先,找到目錄名并將它們存盤在變數中#1..#whatever,方法是將它們提交到findstr /n每個前綴serial:
接下來,重復掃描#n變數陣列,targetstring用作正在檢查的字串的存盤。將它與陣列中的其他字串進行比較,如果它是另一個字串的子字串,則清除它targetstring,如果targetstring存在,則清除#variable具有此值的那個,處理該值并重新開始。
重復直到所有#variables 都被處理——這意味著它們都被從環境中洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/462617.html
上一篇:批處理檔案洗掉超過特定數量的檔案
