cmd를 사용하여 디렉터리를 반복하고 파일 이름에서 특수 문자를 제거합니다.

cmd를 사용하여 디렉터리를 반복하고 파일 이름에서 특수 문자를 제거합니다.

원하는 디렉토리의 모든 파일을 반복하고 파일 이름의 특수 문자를 공백으로 바꿀 수 있는 명령줄 스크립트를 찾고 있습니다. 특수 문자에 대한 대체 논리가 작동하지 않습니다. (abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789)에 없는 항목을 교체하려고 합니다.

for /r C:\yourdirectorypath %F in (*) do replace(filename, regexp, ' ') 

답변1

다음 .bat스크립트가 도움이 될 수 있습니다. 더 나은 이해를 위해 ::또는 줄로 주석을 달았습니다 rem.

regexp나는 지원을 모른다 cmd; 따라서 특정 문자열은한 글자 한 글자서브루틴 의 기본 ( 구문 분석에 제공된 문자열에 백분율 기호 :convrt가 있는 경우 잘못된 결과에 유의하세요 ).%

@ECHO OFF >NUL
@SETLOCAL enableextensions
:: all `echo` lines for debugging purposes only (remove no sooner than debugged)
:::: set "_ALLOWED=abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789"
:::: set "_REPLWITH=-"
:: next setting suffice as '%variable:StrToFind=NewStr%' is case insensitive
set "_ALLOWED=ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789-"
:: note a blank space in the above line (with no full stop)
:: note last character of %_ALLOWED% value used to be the replace one as follows
set "_REPLWITH=%_ALLOWED:~-1%"

:: a filename sample (no extension) to be converted 
:: containing some batch-exceptional characters
SET "_sample=AÄaá^ cč^DĎ(1!)&°~!.foo~bar"
call :convrt _ELPAMS _sample

:: next lines show an intentional usage (with no subfolder recursion)  
pushd "D:\bat\Unusual Names"
:: create a particular file of an unusual name: echo _sample "%_sample%">"%_sample%.txt"
for /F "delims=" %%G in ('dir /B "A*.txt"') do (
  echo(
  set "_sample=%%~nG"
  call :convrt _ELPAMS _sample
  call :output "for debugging purposes only"
)
popd

ENDLOCAL
goto :eof

Next :convrt subroutine accepts two parameters (passed by reference):
  %1 (output) variable name
  %2 (input)  variable name
The subroutine will return 
 - any `~` tilde character unchanged "it's not a bug, it's a feature";
 - wrong result for any `%` percent sign present in a file name. 

:convrt
SETLOCAL enableextensions disabledelayedexpansion
set "__auxAll=%~2"
call set "__auxAll=%%%~2%%"
echo sub %~2 "%__auxAll%"
set "__result="
:loop
  if "%__auxAll%"=="" goto :endconvrt
  set "__auxOne=%__auxAll:~0,1%"
  set "__auxAll=%__auxAll:~1%"
  call set "__changed=%%_ALLOWED:%__auxOne%=%%"
  if not "%__changed%"=="%_ALLOWED%" (
    set "__result=%__result%%__auxOne%"
  ) else (
    set "__result=%__result%%_REPLWITH%"
  )
goto :loop

:endconvrt
echo sub %~1 "%__result%"

ENDLOCAL&call set "%~1=%__result%"
goto :eof

:output
  rem echo for _sample "%_sample%"
  echo for _ELPAMS "%_ELPAMS%"
goto :eof

산출견본. 사례 를 참고하세요 %(사려 깊은 패치워크):

==>D:\bat\SU\921338.bat
sub _sample "AÄaá^ cč^DĎ(1!)&°~!.foo~bar"
sub _ELPAMS "A-a-- c--D--1----~--foo~bar"

sub _sample "AÄaá^ cč^DĎ(1!)&°~!.foo~bar"
sub _ELPAMS "A-a-- c--D--1----~--foo~bar"
for _ELPAMS "A-a-- c--D--1----~--foo~bar"

sub _sample "AÄzž^ yýS%OS%Š%%OS%%(%1!)&°~%%G!^%~2.foo~bar"
sub _ELPAMS "A-z-- y-S%OS%-%%OS%%-%1----~%%G--%~2-foo~bar"
for _ELPAMS "A-z-- y-SWindows_NT-%OS%-_ELPAMS----~%G--_sample-foo~bar"

자원(읽어야 함, 미완성):

관련 정보