2017-08-05 132 views
0
@echo off 

FOR /f "delims=" %%G IN ('dir /a-d /b /s /o-n ^|sort /r') DO (
    setlocal enabledelayedexpansion 
    pushd "%%~dpG" 
    SET Var=%%~nfG 
    SET Var=!Var: =_! 
    SET Var=!Var:[=_! 
    SET Var=!Var:]=_! 
    SET Var=!Var:(=_! 
    SET Var=!Var:)=_! 
    SET Var=!Var:,=_! 
    SET Var=!Var:'=_! 
    rename "%%~nfG" "!Var!" 
    popd 
    endlocal 
) 

不工作得到錯誤提示中像批處理文件來替換子文件夾中的特殊字符在Windows

_!在這個時候是意外的。

任何人都可以回答或更正此plz。 enter code here

+0

我雙引號所有'設置Var'這樣'設置「無功無功=:!= _」' – LotPings

+0

同意了,因爲你沒有使用雙引號,你有沒有阻止「特殊'關閉括號'字符'關閉你的'DO'塊 – Compo

回答

0

而且我會比較,如果改爲現名試圖重命名前:

:: Q:\Test\2017\08\05\SO_45521689.cmd 
@echo off 

FOR /f "delims=" %%G IN (
    'dir /a-d /b /s /o-n ^|findstr "[,'()\[\]]"^|sort /r' 
) DO (
    setlocal enabledelayedexpansion 
    pushd "%%~dpG" 
    SET "Var=%%~nfG" 
    SET "Var=!Var: =_!" 
    SET "Var=!Var:[=_!" 
    SET "Var=!Var:]=_!" 
    SET "Var=!Var:(=_!" 
    SET "Var=!Var:)=_!" 
    SET "Var=!Var:,=_!" 
    SET "Var=!Var:'=_!" 
    if "%%~nfG" neq "!Var!" rename "%%~nfG" "!Var!" 
    popd 
    endlocal 
) 
0
SET Var=!Var:)=_! 
rem   ↑  this closing parenthesis closes the `... DO()` code block. 

使用

SET "Var=!Var:)=_!" 

SET Var=!Var:^)=_! 

此外,you cannot specify a drive or path for rename target。使用SET "Var=%%~nxG"而不是SET Var=%%~nfG

要知道,你的腳本ingurgitates如果文件名包含所有的感嘆號。

以下腳本應該執行保持延遲展開禁用的作業。請注意,爲了調試目的,操作rename命令僅僅是ECHO

@ECHO OFF 
SETLOCAL EnableExtensions DisableDelayedExpansion 
rem choose initial directory 
pushd "D:\bat\Unusual Names" 
FOR /f "delims=" %%G IN ('dir /a-d /b /s /o-n') DO (
    SET "_FFN=%%~fG"         File Full Name 
    SET "Var=%%~nxG"         File Name + Extension 
    call :renaming 
) 
popd 
ENDLOCAL 
goto :eof 

:renaming 
    SET "Var=%Var: =_%" 
    SET "Var=%Var:[=_%" 
    SET "Var=%Var:]=_%" 
    SET "Var=%Var:(=_%" 
    SET "Var=%Var:)=_%" 
    SET "Var=%Var:,=_%" 
    SET "Var=%Var:'=_%" 
    ECHO rename "%_FFN%" "%Var%" 
goto :eof 
相關問題