Computer Hope

Microsoft => Microsoft DOS => Topic started by: gumbaz on January 25, 2009, 02:38:12 PM

Title: File Type Organizer..??
Post by: gumbaz on January 25, 2009, 02:38:12 PM
I need to make a BAT Script that will search a Folder & all its Sub-Contents for every file type it has in it,
then create a folder for each type of file Found, then move each file type to its corresponding File-Type-Folder
So if it finds a .JPG file it creates a Folder named "JPG" then moves that file into that "JPG" folder.. Ect
So for each file Found a corresponding folder is created & named base on the file type found "JPG" "AVI" "MPG" "PDF" Ect..
Then each file that is found is moved into the its corresponding File Type Folder..

This was an old BAT i was using before but i need to update it a lil more to do what im asking here..
Can anyone please help me out to tweak my existing code to do what I want..
Thanx..

Code: [Select]
CD /D C:\FILES
set MEDIA=C:\[-MEDIA-]
if not exist %MEDIA% MD %MEDIA%
setlocal enabledelayedexpansion
for %%T in (jpg jpeg gif bmp png tiff swf avi mpg mpeg wmv flv mp4 mov vob) do (
  set file-type=%%T
echo file type !file-type!
set /a num=1
(
for /f "delims==" %%F in ('dir /s /b *.!file-type!') do (
     set pad=
     if !num! LEQ 999999 set pad=0!pad!
     if !num! LEQ 99999 set pad=0!pad!
     if !num! LEQ 9999 set pad=0!pad!
     if !num! LEQ 999 set pad=0!pad!
     if !num! LEQ 99 set pad=0!pad!
     if !num! LEQ 9 set pad=0!pad!
     ATTRIB -H -S -A -R "%%F"
     IF NOT EXIST "%MEDIA%\%%T" MD "%MEDIA%\%%T"
     COPY "%%F" "%MEDIA%\%%T\!pad!!num!.!file-type!"
     DEL /F /Q "%%F"
     ECHO MOVED "%%F" to "%MEDIA%\%%T\!pad!!num!.!file-type!"
     set /a num=!num!+1
)
)
)
Title: Re: File Type Organizer..??
Post by: fireballs on January 25, 2009, 03:24:01 PM
how does this look?

Code: [Select]
for /f "delims=" %%A in ('dir /b /a-d') do (
if exist "%CD%\%%~xA" (xcopy "%%A" "%CD%\%%~xA") else (MD "%CD%\%%~xA" & xcopy "%%A" "%CD%\%%~xA")
del "%%A"
)

:EDIT sorry i missed a few things should work now!

FB
Title: Re: File Type Organizer..??
Post by: Dias de verano on January 25, 2009, 03:40:08 PM
Code: [Select]
IF NOT EXIST "%MEDIA%\%%T" MD "%MEDIA%\%%T"
     COPY "%%F" "%MEDIA%\%%T\!pad!!num!.!file-type!"

The inner loop does not see the variable %%T, I think.
Title: Re: File Type Organizer..??
Post by: gumbaz on January 25, 2009, 03:43:46 PM
thanx for the help FB..
could you comment your code for me please so i know what its doing  
also can you use my Variables instead of yours .. Im not sure what yours mean..?? (%%A) (%CD%) (%%~xA)
Thnx again for the help..

@Dias de verano, I dont get what you mean..??
Title: Re: File Type Organizer..??
Post by: fireballs on January 25, 2009, 03:50:13 PM
%CD%=the current directory.
%%A=the file the for loop is currently working on.
%%~xA=the extension of the file that the for loop is currently working on.

Code: [Select]
for /f "delims=" %%A in ('dir /b /a-d') do (Takes all the files in the current directory.
Code: [Select]
if exist "%CD%\%%~xA" (xcopy "%%A" "%CD%\%%~xA") If the directory "current directory/extension" exists copy "current file" to "current directory\extension"
Code: [Select]
else (MD "%CD%\%%~xA" & xcopy "%%A" "%CD%\%%~xA")If the directory "current directory\extension" doesn't exist "make directory, then copy file to directory"
Code: [Select]
del "%%A" delete current file and move onto the next one.

FB
Title: Re: File Type Organizer..??
Post by: gumbaz on January 25, 2009, 04:13:34 PM
ok thanks.
So how would I incorporate a same file name collision code into this..?? encase a Src file needs to be moved and a file already exists in the destination folder. how can I Append like (1) (2) (3) Ect into the new file name to be copied over if a file already exists in the dest folder with the same name..??

So if "C:\[MEDIA]\JPG\PIC-ABC.JPG" already exists and "F:\PIC-ABC.JPG" needs to be moved over to "C:\[MEDIA]\JPG" how would I append (1) to its file name like: "C:\[MEDIA]\JPG\PIC-ABC(1).JPG" and so on for each new same named file the needs to be moved over..??
Title: Re: File Type Organizer..??
Post by: gumbaz on January 25, 2009, 05:39:18 PM
Anybody got any ideas at all..??
Title: Re: File Type Organizer..??
Post by: Dias de verano on January 26, 2009, 12:30:08 AM
Bad Gumbaz, bumping! And after only 2 hours!
Title: Re: File Type Organizer..??
Post by: fireballs on January 26, 2009, 06:38:10 AM
After my beautifully crafted three line answer the first time, i can't think of a nice, efficient way of doing what you want except like this:

Code: [Select]
for /f "delims=" %%A in ('dir /b /a-d') do (
if not exist "%CD%\%%~xA" MD "%CD%\%%~xA" & xcopy "%%A"
if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA(1)") else (
if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA(2)") else (
REM etc.)
)
del "%%A")

FB
Title: Re: File Type Organizer..??
Post by: gumbaz on January 26, 2009, 11:07:03 AM
Tanx verry muchos FB..
What do you mean by "else (REM etc.)" ..??

Will your code only rename the new file copied over up to 2 collisions or will it keep sequentially adding (+1) to each of found duplicate names..??
Ex..
"C:\[MEDIA]\JPG\PIC-ABC.JPG"  already exist....
"F:\FILES\PIC-ABC.JPG"  needs to be copied over, so it gets copied over as "C:\[MEDIA]\JPG\PIC-ABC(1).JPG"
then another file is found with the same name "F:\FILES\1\PIC-ABC.JPG"
and its copied over as "C:\[MEDIA]\JPG\PIC-ABC(2).JPG"
and every new same named file that needs to be copied over gets renamed with a sequential number (1)(2)(3)(4) Ect...
Ex..
"C:\[MEDIA]\JPG\PIC-ABC(1).JPG"
"C:\[MEDIA]\JPG\PIC-ABC(2).JPG"
"C:\[MEDIA]\JPG\PIC-ABC(3).JPG"
"C:\[MEDIA]\JPG\PIC-ABC(4).JPG"
"C:\[MEDIA]\JPG\PIC-ABC(5).JPG"
"Ect..."

Will your code do that, or does it need to be modified a lil more..??
Title: Re: File Type Organizer..??
Post by: fireballs on January 26, 2009, 11:32:45 AM
wow i was not awake this morning, that code would not have worked. my appologies.

Code: [Select]
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /a-d') do (
set count=1
if not exist "%CD%\%%~xA" MD "%CD%\%%~xA"
if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA") else (
:1
if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
set /a count=!count!+1
goto 1)
)
del "%%A")

Hmm that should work now i'm a little bit more awake!

FB
Title: Re: File Type Organizer..??
Post by: Dias de verano on January 26, 2009, 11:37:45 AM
labels inside FOR loops are deprecated, as I suspect you will soon discover...
Title: Re: File Type Organizer..??
Post by: gumbaz on January 26, 2009, 12:16:38 PM
Eh not too sure about what that means.. im a lil stupid dis mornin...
Thnx thow for yer stupendis helpingness FB you da man dawg.!! whoop whoop..!! :)
Title: Re: File Type Organizer..??
Post by: DaveLembke on January 26, 2009, 05:26:19 PM
Was looking this over and I found this to be very useful for a task I have with a external hard drive that is loaded with scattered data.

I added the /s  switch to focus also on all files in all sub directories starting at the root trigger point of the batch to xcopy these files as well into the grouped by file extension file folders.

I also got rid of the DEL command because I dont want to destroy the file structure, although total chaos, and end up with just .fileextension  for each file extension discovered and file placed within, but instead added this logging to file ability in place of it @echo.%%A>>Copied.txt  to have a list of every file copied.

Code: [Select]
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /s /a-d') do (
set count=1
if not exist "%CD%\%%~xA" MD "%CD%\%%~xA"
if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA") else (
:1
if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
set /a count=!count!+1
goto 1)
)
@echo."%%A">>Copied.txt)

But what is strange and I cant figure out is why when finding an alternate of the same name it is not adding (1) or (2) etc at the end of the name to keep same file names seperate. It prompts do you want to overwrite instead of adding the (1) etc to the end of the file name such as 2007taxes(1).pdf and 2007Taxes(2).pdf  ... Any idea what may be going wrong?   :-\

I thought that maybe instead of IF NOT EXIST for

if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
set /a count=!count!+1
goto 1)

should be IF EXIST then increment +1, but when trying to fix this I get stuck in endless loop because the condition doesnt change to exit the loop.
Title: Re: File Type Organizer..??
Post by: gumbaz on February 01, 2009, 05:22:28 AM
This what i got so far into this batch work..
The only problem is that after it copies the first duplicate named file over to the corresponding extension folder Ex. "Picture(1).JPG" when it goes to copy the next duplicate named file over as "Picture(2).JPG" it doesn't work correctly because it says it cant find %F or whatever..
can you guys please help me figure out whats going wrong in the code..??

Code: [Select]
CD /D C:\FILES
set MEDIA=C:\[-MEDIA-]
setlocal enabledelayedexpansion
if not exist %MEDIA% MD %MEDIA%
for /f "delims=" %%F in ('DIR /AH /AS /AR /AA /B /S') do (
set /a NUM=1
if not exist "%MEDIA%\%%~xF" MD "%MEDIA%\%%~xF"
if not exist "%MEDIA%\%%~xF\%%~nxF" (ATTRIB -H -S -A -R "%%F" & copy "%%F" "%MEDIA%\%%~xF" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nxF") else (
:TOP
echo %NUM%
ECHO %%F
if not exist "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" (ATTRIB -H -S -A -R "%%F" & copy "%%F" "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF") else (
set /a NUM+=1 & goto TOP)
)
DEL /F /Q "%%F"
)
Title: Re: File Type Organizer..??
Post by: gumbaz on February 01, 2009, 03:26:52 PM
Anybody see whats going wrong in my code at all..??
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 01, 2009, 03:31:56 PM
Anybody see whats going wrong in my code at all..??

I think it's nearly unreadable. No wonder you can't see what's wrong. Neither can anyone else!

Here's an example.

Code: [Select]
if not exist "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" (ATTRIB -H -S -A -R "%%F" & copy "%%F" "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF") else (
Why did you write it like that?
Title: Re: File Type Organizer..??
Post by: gumbaz on February 01, 2009, 05:56:31 PM
Code: [Select]
if not exist "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" (ATTRIB -H -S -A -R "%%F" & copy "%%F" "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF") else (
Whats so unreadable about it exactly..??
I understand what its suppose to do, but maybe i coded it wrong..
What do you see thats wrong with it exactly..??
Title: Re: File Type Organizer..??
Post by: BC_Programmer on February 01, 2009, 06:05:25 PM
I think the main preventer of readability is the unnecessary use of ampersands to concatenate commands on single lines for no reason at all.
Title: Re: File Type Organizer..??
Post by: gumbaz on February 01, 2009, 06:19:23 PM
so how would i recode it properly then..??
Title: Re: File Type Organizer..??
Post by: BC_Programmer on February 01, 2009, 08:41:46 PM
so how would i recode it properly then..??

put each line on a separate line.
Title: Re: File Type Organizer..??
Post by: gumbaz on February 02, 2009, 06:16:08 PM
what, you mean like this..??

Code: [Select]
CD /D C:\FILES
set MEDIA=C:\[-MEDIA-]
setlocal enabledelayedexpansion
if not exist %MEDIA% MD %MEDIA%
for /f "delims=" %%F in ('DIR /AH /AS /AR /AA /B /S') do (
set /a NUM=1
if not exist "%MEDIA%\%%~xF" MD "%MEDIA%\%%~xF"
if not exist "%MEDIA%\%%~xF\%%~nxF" (ATTRIB -H -S -A -R "%%F"
copy "%%F" "%MEDIA%\%%~xF"
ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nxF") else (
:TOP
ECHO %NUM%
ECHO %%F
if not exist "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF" (ATTRIB -H -S -A -R "%%F"
copy "%%F" "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF"
ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nF(!NUM!)%%~xF") else (
set /a NUM+=1 & goto TOP)
)
DEL /F /Q "%%F"
)
Title: Re: File Type Organizer..??
Post by: gumbaz on February 03, 2009, 12:23:30 AM
its still not working for me after it copies over "FILE(1).EXT"

File not found - %F
The system cannot find the file specified.


Whats going wrong..??
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 03, 2009, 12:27:37 AM
Look carefully at your braces.
Title: Re: File Type Organizer..??
Post by: gumbaz on February 03, 2009, 12:59:33 AM
im blind, please just tell me where its wrong.. i spent too much time conjuring up my 2 brain cells on this already...
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 03, 2009, 01:09:51 AM
You are using braces (like this
to create blocks for if) but (
you are also creating filenames with (!braces!) in them within those blocks)

Which breaks the code

Thats what I am guessing

because your code is so horrible to look at.

What does your code do when it finds a directory name?
Title: Re: File Type Organizer..??
Post by: gumbaz on February 03, 2009, 01:37:15 AM
it doesn't pick up directory names because of this ('DIR /AH /AS /AR /AA /B /S')
maybe thats the prob..??
i want it to pick up every file with any attribute possible..
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 03, 2009, 01:41:39 AM
DIR /A-D /AH /AS /AR /AA /B /S
Title: Re: File Type Organizer..??
Post by: gumbaz on February 03, 2009, 01:51:05 AM
still aint working for some reason..??
it works fine up untill the part when it has to copy over "FILE(2).EXT"
it copies over "FILE(1).EXT" just fine but "FILE(2).EXT" just havin it man..??

trie it out..
put any file type into c:\files then run the bat
copy back the same file to c:\files and run the bat again it gets copied over as "FILE(1).EXT"
copy back the file once more & run the bat again, and thats when the "File Not Found Error" occurs...
Title: Re: File Type Organizer..??
Post by: BC_Programmer on February 03, 2009, 02:23:36 AM
you are also creating filenames with (!braces!) in them within those blocks)

the braces should be escaped I think.
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 03, 2009, 02:43:53 AM
I ran this deplorably ugly code on some test data and it seems that the variable !NUM! - which seems to be something  gumbaz has devised for renaming a file in the event of a name collision - is being set to nothing (a blank) leading to a torrent of "ECHO is ON" messages and the variable being rendered literally rather than being expanded.

I mentioned about a week ago (on 26 Jan) that labels inside loops or other parenthetical structures are FROWNED UPON. I was ignored. The reason is that labels must be at the start of a line. The commands within the parentheses are really just one long line of multiple commands and the label ends up buried in the middle of that line. That code won't -ever- work.

Rather than repair this mess, I would call it a learning experience and start again, and this time I would:

Get the method and logic right and then write the code!

Title: Re: File Type Organizer..??
Post by: gumbaz on February 03, 2009, 03:51:18 AM
so how would you go about coding in a same name collision renaming function then all mighty Guru Master..??
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 03, 2009, 03:54:15 AM
I would rename the incoming colliding file somehow. Maybe use the system clock which changes 100 times every second.

e.g.

Code: [Select]
set timecode=%time%
set timecode=%timecode::=%
set timecode=%timecode:.=%
echo %timecode%
Title: Re: File Type Organizer..??
Post by: gumbaz on February 28, 2009, 08:01:24 AM
Code: [Select]
CD /D C:\FILES
set MEDIA=C:\[-MEDIA-]
setlocal enabledelayedexpansion
if not exist "%MEDIA%" MD "%MEDIA%"
for /f "delims=" %%F in ('DIR /ARASH /A-D /B /S') do (
if not exist "%MEDIA%\%%~xF" MD "%MEDIA%\%%~xF"
if not exist "%MEDIA%\%%~xF\%%~nxF" ATTRIB -H -S -A -R "%%F" & COPY "%%F" "%MEDIA%\%%~xF\%%~nxF" & DEL /F /Q "%%F" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nxF"
if exist "%MEDIA%\%%~xF\%%~nxF" ATTRIB -H -S -A -R "%%F" & COPY "%%F" "%MEDIA%\%%~xF\%%~nF(%TIME::=%)%%~xF" & DEL /F /Q "%%F" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nF(%TIME::=%)%%~xF"
)

Everything works ok in this new script except the (%TIME::=%) part at the end..
the time never changes when renaming the 2nd same named file collisions..
it just keeps the same time and if there are more than 2 same files the 3rd file gets deleted..??
whats causing the time to stay the same..??
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 28, 2009, 08:27:18 AM
Everything works ok in this new script except the (%TIME::=%) part at the end..
the time never changes when renaming the 2nd same named file collisions..
it just keeps the same time and if there are more than 2 same files the 3rd file gets deleted..??
whats causing the time to stay the same..??

Being in a loop causes %time% to stay the same. It would not happen with !time!. (Isn't that why you have enabled delayed expansion? - nothing else in that loop needs it that I can see.)

Also,

Code: [Select]
"%MEDIA%\%%~xF\%%~nF(%TIME::=%)%%~xF" & DEL /F /Q "%%F" & ECHO MOVED "%%F" to "%MEDIA%\%%~xF\%%~nF(%TIME::=%)
the second TIME may not be the same as the first, since it measures to 1/100 second.
Title: Re: File Type Organizer..??
Post by: gumbaz on February 28, 2009, 09:22:35 AM
so how exactly would i use the !time! feature in my code then..??
Title: Re: File Type Organizer..??
Post by: Dias de verano on February 28, 2009, 11:32:53 AM
Quote from: gumbaz
so how exactly would i use the !time! feature in my code then..??

The same way you used the %time% feature.