2017-07-17 15 views
1

這個腳本(除了尋找重複文件)重命名的文件不包括(在單個文件夾)的特殊字符。當文件名有這樣的字符時,我仍然遇到問題。我試圖在delayedexpansion之內添加內容,並且在外部沒有成功。批:轉義特殊字符,如「和」可變

@echo off 
echo "%~1" | find /i "system volume information" >nul && exit /b 
echo "%~1" | find /i "s-1-5-21-3" >nul && exit /b 
echo "%~1" | find /i "recycle" >nul && exit /b 
cd /d %1 2>nul 
if /i not "%~1"=="%cd%" echo cannot access %~n1 && exit /b 

for %%v in (*) do set "original=%%~nxv" & call :validate 
for /f "delims=" %%v in ('dir /b /a-d') do set "compare=%cd%\%%v" & call :detection 
exit /b 

:validate 
:: add handling of 「 and 」 
set "newtitle=%original:!=%" 
set "newtitle=%newtitle:」=%" 
set "newtitle=%newtitle:&=and%" 
setlocal enabledelayedexpansion 
set "newtitle=!newtitle:%%= percent!" 
setlocal disabledelayedexpansion 
if not "%original%"=="%newtitle%" ren "%original%" "%newtitle%" && echo validated %newtitle% 
goto next 

:detection 
:: this extrapolates %string% from beginning of filename 
call d:\other\scripts\lootname.cmd "%compare%" data 
set count=0 
for %%v in ("%string%*") do set /a "count+=1" 
for %%v in ("%string%*") do if not "%count%"=="1" echo "%%v" 
:next 
+0

文件名不能有像**「**和**」 ** – Compo

+0

以及YouTube下載併成功的文件名保存到我的NTFS驅動器字符。現在我嘗試了我也可以。 – bricktop

+0

@Compo你爲什麼這麼認爲?我只是把你的確切句子放到一個文件名中,但批處理並不是處理這些字符的理想環境,我建議使用PowerShell – LotPings

回答

1

這個PowerShell的一個班輪應該遞歸查找帶有印刷引號的文件(從當前文件夾中)並刪除它們。

$Search = '「|」';gci -r -file |where Name -Match $Search|Ren -NewName {$_.Name -replace $Search} -Confirm 

只需粘貼到PowerShell控制檯並讓它運行。 的-Confirm參數會讓你問每一個重命名

輸出示例:

Confirm 
Are you sure you want to perform this action? 
Performing the operation "Rename File" on target 
"Item: A:\A file name cannot have characters like 「 and 」 
Destination: A:\A file name cannot have characters like and ". 
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): 

一般情況下我嵌入的PowerShell代碼:

powershell -NoP -Command "$Search = '「|」';gci -r -file |where Name -Match $Search|Ren -NewName {$_.Name -replace $Search}" 

但是要記住那個powershell通常在內部使用UTF16像窗口本身,所以這裏的代碼頁限制適用。
我無法粘貼到我的基於chcp 850的控制檯 - 印刷引號被交換到正常的。

+0

關於轉移到PowerShell;我可以通過在常規批處理腳本中運行一行PowerShell來增量執行它,還是必須重寫整個事件?如果腳本需要另一個腳本,我可以從regul調用powershell腳本批處理腳本,反之亦然? – bricktop

+0

我將在一批中嵌入上述行。一般來說,混合命令需要引用/轉義,而不是讓一個shell解釋另一個命令的命令。 – LotPings

+0

你能詳細說明UTF/CHCP和頁面限制嗎? – bricktop