move files to folders named by date from files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mato
Posts: 9
Joined: 13 May 2012 03:56

move files to folders named by date from files

#1 Post by mato » 13 May 2012 04:11

hi
I have files named textA_date_time.avi, textB_date_time.avi.... I need move all files to folders which names are create by dates from names from files.

for example: chose date from textA_20120513_120559.avi, create folder with name: 20120513 and move all files with this date (there are also files with different time, but same date)

do the same with textB_date_time.avi ....

any ideas?

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

Re: move files to folders named by date from files

#2 Post by aGerman » 13 May 2012 05:40

Can you make sure there is no further underscore in textA, textB, ...?

Regards
aGerman

mato
Posts: 9
Joined: 13 May 2012 03:56

Re: move files to folders named by date from files

#3 Post by mato » 13 May 2012 06:20

original names of files are date_time.avi. it is from motion detection from IP camera. I have 2 area of motion detection so I have 2 different names of group of videos. textA_date_time.avi can be date_time.avi, but it can be also textAdate_time.avi.

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

Re: move files to folders named by date from files

#4 Post by aGerman » 13 May 2012 06:32

Untested:

Code: Select all

@echo off
for /f "delims=" %%i in ('dir /a-d /b *.avi') do (
  set "name=%%~ni"
  setlocal EnableDelayedExpansion
    set "fdate=!name:~-15,8!"
    md "!fdate!" 2>nul
    move "!name!.avi" "!fdate!\"
  endlocal
)

Regards
aGerman

mato
Posts: 9
Joined: 13 May 2012 03:56

Re: move files to folders named by date from files

#5 Post by mato » 13 May 2012 08:36

It works. thank you very much

mato
Posts: 9
Joined: 13 May 2012 03:56

Re: move files to folders named by date from files

#6 Post by mato » 13 May 2012 08:42

is it possible put _ between 20120513? like this 2012_05_13 in folders names

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

Re: move files to folders named by date from files

#7 Post by aGerman » 13 May 2012 08:59

Of course. It's just string manipulation again.

Code: Select all

@echo off
for /f "delims=" %%i in ('dir /a-d /b *.avi') do (
  set "name=%%~ni"
  setlocal EnableDelayedExpansion
    set "fdate=!name:~-15,8!"
    set "fdate=!fdate:~0,4!_!fdate:~4,2!_!fdate:~-2!"
    md "!fdate!" 2>nul
    move "!name!.avi" "!fdate!\"
  endlocal
)

Regards
aGerman

mato
Posts: 9
Joined: 13 May 2012 03:56

Re: move files to folders named by date from files

#8 Post by mato » 13 May 2012 09:57

thank you. I have last request: how can I delete this folders after for example 60days?

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

Re: move files to folders named by date from files

#9 Post by aGerman » 13 May 2012 10:04

Very similar:
viewtopic.php?f=3&t=3300

Regards
aGerman

mato
Posts: 9
Joined: 13 May 2012 03:56

Re: move files to folders named by date from files

#10 Post by mato » 13 May 2012 10:13

I think it is quite dificult. maybe it can by easyli like this: forfiles /p C:\folder /d -60 /c "cmd /c del @file" but it does not work.
It only delete files inside folders and I must press Y to delete. Is it possible to do without Y ?

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

Re: move files to folders named by date from files

#11 Post by aGerman » 13 May 2012 10:44

The question is how you want to determine the date? According the folder name you should use the :date2jdate function. According the folder date you could use FORFILES.

Regards
aGerman

mato
Posts: 9
Joined: 13 May 2012 03:56

Re: move files to folders named by date from files

#12 Post by mato » 13 May 2012 10:48

According the folder date is ok for me. but I don not know how

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

Re: move files to folders named by date from files

#13 Post by aGerman » 13 May 2012 11:01

Try

Code: Select all

@echo off
for /f "tokens=1*" %%i in ('forfiles /d -60 /m ????_??_?? /c "cmd /c echo @isdir @path"') do (
  if %%i==TRUE ECHO rd /s /q %%j
)
PAUSE

Remove ECHO and PAUSE if you think it would work.

Regards
aGerman

mato
Posts: 9
Joined: 13 May 2012 03:56

Re: move files to folders named by date from files

#14 Post by mato » 13 May 2012 11:18

thank you

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

Re: move files to folders named by date from files

#15 Post by Squashman » 13 May 2012 11:48

mato wrote:I think it is quite dificult. maybe it can by easyli like this: forfiles /p C:\folder /d -60 /c "cmd /c del @file" but it does not work.
It only delete files inside folders and I must press Y to delete. Is it possible to do without Y ?

As aGerman has already shown you the DEL command deletes files. The RMDIR or RD deletes directories. You gotta use the correct tool for the corresponding job. ;)

You wouldn't use a Jack Hammer to pound in a nail.

Post Reply