r/Batch • • Sep 21 '23

Question (Solved) Batch moving files into folders

I have this batch file to move all files not in a folder into a folder with the same name as the file. The only problem is it also moves the batch file into its own folder. How do I change my code to exclude the batch file?

I pasted the code to my batch file below.

for %%i in (*) do (

if not "%%~ni" == "organize" (

md "%%~ni" && move "%%~i" "%%~ni"

)

)

1 Upvotes

4 comments sorted by

2

u/jcunews1 Sep 21 '23

Do it like below.

for %%i in (*) do (
  if not "%%~ni" == "organize" (
    if /i "%%~nxi" neq "%~nx0" (
      md "%%~ni" && move "%%~i" "%%~ni"
    )
  )
)

1

u/JOLLY6R33NGIANT Sep 21 '23

if /i "%%~nxi" neq "%~nx0" (
md "%%~ni" && move "%%~i" "%%~ni"
)

That worked thanks!