简体   繁体   中英

moving files to folders based on extension

Finally I got to move files with different extensions into folders according to their extensions, but I cannot get rid of the "." preceding the created folders... Here's my batch file:

for %%i in (*) do mkdir "%%~xi" & move "%%i" "%%~xi"

Please help me remove the dot. Thanks

One way:

setlocal enabledelayedexpansion
for %%i in (*) do (
    set ext=%%~xi
    set ext=!ext:~1!
    mkdir "!ext!" 
    move "%%i" "!ext!"
)

What you are doing is just creating a folder names after the file extension. To remove the dot all you need to do is remove the first character.

setlocal enabledelayedexpansion
for %%i in (*) do (   
   set "x=%%~xi"   
   set x=!x:~1,400!  
   mkdir "!x!"
   move "%%i" "!x!" 
)  
pause  `

If you by chance just want to rename the folders:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%D in ('dir /a:d /b') do (
   set x=%%D
   set x=!x:~1,100!
   set y=!x:~0,1!
   if "!y!"=="." (
        rename "%%D" "!x!"
  )
 )
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM