Moving Files based on name syntax

June 15, 2009 at 14:36:24
Specs: Windows XP
Hi,

I'm trying to sort thousands of small aiff files. The files have a predictable naming syntax, "20090513_000800.aif" (Date_Time). Ideally it would sort the files into folders by date 20090513* and within those by hour 20090513_01* etc.

I found this batch script on another forum. I thought I had modified it to sort files by their hour in the day, but obviously went wrong somewhere as all the files end up in folder "lf" (lost&found):

@echo off
set source=C:\a2w1
set target1=C:\a2w1\lf
set target2=C:\a2w1\test
:: List of file types
set types=20090513_00;20090513_01;20090513_02;20090513_03;^
20090513_04;20090513_05;20090513_06
:: Move matching files
for %%a in (%types%) do (
move "%source%\*%%~a*.aif" "%target2%\%%~a")
:: Whatever's left move to lost and found
move "%source%\*.aif" "%target1%"

Could anyone lend some guidance here? It would be much appreciated!

Thanks,
Abe Borker
aborker@ucsc.edu


See More: Moving Files based on name syntax

Report •


#1
June 15, 2009 at 16:49:37
_000800.

Is that part always 6 didgits between _ and . ?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
June 15, 2009 at 17:44:24
always indeed YYYYMMDD_HHMMSS.aif

Report •

#3
June 15, 2009 at 18:07:06
:: moveaif Tue 16-06-2009 7:56:01.67

:: UNTESTED

@echo off > newfile & setLocal enableDELAYedexpansion

set /p src=src ?:
set /p dest=dest ?:
pushd !src!

for /f "tokens=* delims= " %%a in ('dir/b/a-d *.aif') do (
set str=%%a
move %%a !str:~0,8!\!str:~9,2!
)

goto :eof

::===========================================

Assuming you set your dest below which are the directories.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
June 15, 2009 at 20:32:22
Wow thanks!

I'll try this tomorrow when I'm with those files!

If there was something like "ARU#47_YYYYMMDD_HHMMSS.aif" would I change

move %%a !str:~0,8!\!str:~9,2!

to

move %%a !str:~7,15!\!str:~16,2!

Or am I way off?


Report •

#5
June 15, 2009 at 21:32:39
Looks about right to me.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
June 16, 2009 at 10:02:33
Hi M2,

I tried it this morning and ran into a hangup...

"The system could not find the path specified" repeated a slew of times...

I double and triple checked I'm entering my folders right, "C:\a2w12\" and "C:\a2w12\test\"

Any ideas?

Thanks again for your help!


Report •

#7
June 16, 2009 at 12:20:08
For those interested, with alot of help I've found a solution, thank you to Alexander Zivic of Austria for pointing me towards AnyHotKey and putting together this script.

SetWorkingDir %A_ScriptDir%
Loop,*.aif,0,0
{
    StringReplace,temp,A_LoopFileName,.%A_LoopFileExt%
    StringSplit, temp, temp, _

    id = %temp1%
    date = %temp2%
    time = %temp3%

    StringMid, hour, time, 1, 2
    StringMid, minute, time, 3,2
    StringMid, second, time, 5,2

    StringMid, year, date, 1,4
    StringMid, month, date, 5,2
    StringMid, day, date, 7,2

    FormatTime, week , %date%, YWeek
    StringMid, week, week, 5,2
   
    FileCreateDir,%id%\%date%\%hour%
    FileMove, %A_LoopFileName%, %id%\%date%\%hour%\
}

Hopefully by sharing it I can help others dealing with a similar challenge.


Report •

#8
June 16, 2009 at 12:24:00
err, AutoHotKey, http://www.autohotkey.com/

Report •

#9
June 16, 2009 at 14:29:44
Hi abe,

Post your current script and I'll go through it.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#10
June 16, 2009 at 15:01:46
Hi M2,

I'll post it for your interest, but as far as my end is concerned it's solved. I appreciate all your help.

:: moveaif Tue 16-06-2009 7:56:01.67

:: UNTESTED

@echo off > newfile & setLocal enableDELAYedexpansion

set /p src=src ?:
set /p dest=dest ?:
pushd !src!

for /f "tokens=* delims= " %%a in ('dir/b/a-d *.aif') do (
set str=%%a
move %%a !str:~7,9!\!str:~16,2!
)

goto :eof

::===========================================


Report •

#11
June 16, 2009 at 17:03:40
Hi abe,

If it ain't broke, I won't try to fix it. Glad you came out with something useable.


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question