Page 1 of 2

moving files

Posted: 05 Apr 2018 06:49
by joejames_786
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

Re: moving files

Posted: 05 Apr 2018 07:53
by batnoob
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
		) 	
	)
)

Re: moving files

Posted: 05 Apr 2018 08:15
by joejames_786
hi

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


thanks

Re: moving files

Posted: 05 Apr 2018 08:36
by batnoob

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?

Re: moving files

Posted: 05 Apr 2018 08:40
by Squashman
If the file names have spaces in the names then you need quotes around all the variables. Best practice is too always use quotes.

Re: moving files

Posted: 05 Apr 2018 08:42
by Squashman

Code: Select all

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

Re: moving files

Posted: 05 Apr 2018 08:44
by batnoob
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
		)		
	)
	
	
)


Re: moving files

Posted: 05 Apr 2018 09:04
by joejames_786
yes that did the job

thankyou very much :D

Re: moving files

Posted: 05 Apr 2018 09:08
by Squashman
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.

Re: moving files

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

Code: Select all

move "!a!" %%b

Re: moving files

Posted: 05 Apr 2018 09:20
by Squashman
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.

Re: moving files

Posted: 05 Apr 2018 09:24
by joejames_786
yes squashman was right it moves files without spaces but leaves behind all those files with spaces in their names

even after adding quotes..

Re: moving files

Posted: 05 Apr 2018 09:40
by batnoob
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
)	

Re: moving files

Posted: 05 Apr 2018 09:48
by aGerman

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

Re: moving files

Posted: 05 Apr 2018 09:53
by joejames_786
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