从批处理中删除文件ext

时间:2014-08-18 09:46:30

标签: windows batch-file

Hay我在这里搜索了很多,并尝试整天从路径名中删除文件ext

我尝试在B变量设置后执行此操作,但它不会工作,当我问到它在某个地方我可以告诉foreach它可以循环的文件夹?

现在它循环播放该文件夹中的所有内容,但我希望它在文件夹A-Z中循环而且工作目录中没有文件

/ Mvh Lukasz

@echo off & setlocal enabledelayedexpansion

:: Foreach files in folder
for /R . %%f in (*) do (

  :: Get file extension
  set A=%%~xf
  echo !A!

  :: Set path
  set B=%%f

  :: Remove file ext
  set D=!B:!A=%

  echo !D!

  :: Show result
  ::echo !D:%CD%\=!

  mkdir "%cd%\root\!B:%CD%\=!"
)

pause

2 个答案:

答案 0 :(得分:0)

@echo off
setlocal enabledelayedexpansion
if "%CD:~-1%" == "\" (
    set PREFIX=%CD:~0,-1%
) else (
    set PREFIX=%CD%
)
for /f "delims=" %%F in ('dir /s/b') do (
    set N=%%~dpF%%~nF
    set N=!N:%PREFIX%=!
    if not "!N:~1,4!" == "root" (
        set N=%PREFIX%\root!N!
        if not exist "!N!" (
            mkdir "!N!"
        )
    )
)

没有子目录:

@echo off
setlocal enabledelayedexpansion
if "%CD:~-1%" == "\" (
    set PREFIX=%CD:~0,-1%
) else (
    set PREFIX=%CD%
)
for /f "delims=" %%F in ('dir /s/b') do (
    set N=%%F
    set N=!N:%PREFIX%=!
    if not "!N:~1,4!" == "root" (
        set N=%PREFIX%\root\%%~nF
        if not exist "!N!" (
            mkdir "!N!"
        )
    )
)

答案 1 :(得分:0)

@echo off
    setlocal enableextensions disabledelayedexpansion
    for /R "%cd%" %%a in (*) do for %%b in ("%%~dpa.") do (
        if not exist "%cd%\root\%%~nb\%%~na\" echo mkdir "%cd%\root\%%~nb\%%~na"
    )

如果输出到控制台是正确的,请删除echo以创建文件夹