moving files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
joejames_786
Posts: 17
Joined: 12 Feb 2017 05:11

moving files

#1 Post by joejames_786 » 05 Apr 2018 06:49

hi

I have a folder containing a mix of files around 100000 in number
they are mainly .pdf .mp4 .doc .docx .mov .xls .xlsx
can someone help me sort these files to separate folders (ie pdf mp4 doc docx.... and so on)

thanks
and regards
joe

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: moving files

#2 Post by batnoob » 05 Apr 2018 07:53

try this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
cd .

set "list=pdf mp4 doc docx mov xls xlsx"
for %%1 in (%list%) do (mkdir %%1)
for /f "tokens=*" %%a in ('dir /b /o:n /a-d *.') do (
	set a=%%a
	for %%b in (%list%) do (
		if "!a:~-4!" == "%%b" (
			move !a! %%b
		) else if "!a:~-3!" == "%%b" (
			move !a! %%b
		) 	
	)
)

joejames_786
Posts: 17
Joined: 12 Feb 2017 05:11

Re: moving files

#3 Post by joejames_786 » 05 Apr 2018 08:15

hi

the script when executed creates the directory but fails to move the files..


thanks

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: moving files

#4 Post by batnoob » 05 Apr 2018 08:36

Code: Select all

@echo off
setlocal enabledelayedexpansion
cd .

set "list=pdf mp4 doc docx mov xls xlsx"
for %%1 in (%list%) do (mkdir %%1)
for /f "tokens=*" %%a in ('dir /b /o:n /a-d *.') do (
	set a=%%a
	for %%b in (%list%) do (
		if "!a:~-4!" == "%%b" (
			robocopy !a! %%b /MOV
		) else if "!a:~-3!" == "%%b" (
			robocopy !a! %%b /MOV
		) 	
	)
)
how's that?

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: moving files

#5 Post by Squashman » 05 Apr 2018 08:40

If the file names have spaces in the names then you need quotes around all the variables. Best practice is too always use quotes.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: moving files

#6 Post by Squashman » 05 Apr 2018 08:42

Code: Select all

dir /b /o:n /a-d *.
This code only grabs files that do not have a file extension.

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: moving files

#7 Post by batnoob » 05 Apr 2018 08:44

sorry, i forgot to change that part when i copied the code from my other post

Code: Select all

@echo off
setlocal enabledelayedexpansion
cd .

set "list=pdf mp4 doc docx mov xls xlsx"
for %%1 in (%list%) do (mkdir %%1)
for /f "tokens=*" %%a in ('dir /b /o:n') do (
	set a=%%a
	for %%b in (%list%) do (
		if "!a:~-4!" == "%%b" (
			move !a! %%b
			
		) else if "!a:~-3!" == "%%b" (
			move !a! %%b
			
		) else (
			REM
		)		
	)
	
	
)


joejames_786
Posts: 17
Joined: 12 Feb 2017 05:11

Re: moving files

#8 Post by joejames_786 » 05 Apr 2018 09:04

yes that did the job

thankyou very much :D

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: moving files

#9 Post by Squashman » 05 Apr 2018 09:08

batnoob wrote:
05 Apr 2018 08:44
sorry, i forgot to change that part when i copied the code from my other post
Your code will still fail on file names with spaces in them.

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: moving files

#10 Post by batnoob » 05 Apr 2018 09:17

so put quotes around it, right?

Code: Select all

move "!a!" %%b

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: moving files

#11 Post by Squashman » 05 Apr 2018 09:20

batnoob wrote:
05 Apr 2018 09:17
so put quotes around it, right?

Code: Select all

move "!a!" %%b
As I said earlier it is best practice to always use quotes. So I would put quotes around both. Granted you know that the directory name does not have spaces but if someone takes your code and starts modifying it again to use with directory names that have spaces then your code will fail again.

joejames_786
Posts: 17
Joined: 12 Feb 2017 05:11

Re: moving files

#12 Post by joejames_786 » 05 Apr 2018 09:24

yes squashman was right it moves files without spaces but leaves behind all those files with spaces in their names

even after adding quotes..

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: moving files

#13 Post by batnoob » 05 Apr 2018 09:40

add quotes to both variables in both if statements?

Code: Select all

if "!a:~-4!" == "%%b" (
	move "!a!" "%%b"
	
) else if "!a:~-3!" == "%%b" (
	move "!a!" "%%b"
	
) else (
	REM
)	

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: moving files

#14 Post by aGerman » 05 Apr 2018 09:48

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for %%i in (*.*) do if "%%~i" neq "%~nx0" for /f "tokens=* delims=." %%j in ("%%~xi") do (
  if not exist "%%j\" md "%%j"
  move "%%~i" "%%j\"
)
Steffen

joejames_786
Posts: 17
Joined: 12 Feb 2017 05:11

Re: moving files

#15 Post by joejames_786 » 05 Apr 2018 09:53

hi


I was just wondering what happened to steffen!!

I was getting anxious as time passed by 8)

wow that was the ultimate solution

steffen you really ROCK MAN :D 8)

THANKYOU VERY MUUUUUCCCCCCHHHHHHH

Post Reply