开发者

Separate File by Dates

开发者 https://www.devze.com 2023-04-06 06:10 出处:网络
I cannot figure out how to separate files into folders by date. I have 1,000,000 files and all files in 1 folder is making Explorer sick :P

I cannot figure out how to separate files into folders by date.

I have 1,000,000 files and all files in 1 folder is making Explorer sick :P so I want to separate them into different folders by开发者_如何转开发 date:

01-09-11

02-09-11

03-09-11

etc.


Extracting the file date is relatively easy, see HELP CALL and try this simple BAT file

@echo off
setlocal enabledelayedexpansion
FOR %%A IN (*.*) DO (
  set tf=%%~tA
  echo  %%~fA ... !tf! 
)

To move beyond this approach into a solution to your problem seems pretty straightforward...

@echo off
setlocal enabledelayedexpansion
FOR %%A IN (*.*) DO (
  set tf=%%~tA
  set fd=!tf:~0,10!
  md !fd! 
  move /Y %%~fA !fd! 
)

but, wait, that code is not guaranteed to run. There are some dependencies on the date format that may prevent this simple code to run. Handling of dates in BAT file is not easy, because date format depends on locale and even on custom preferences. In this particular piece, it will play havoc in case the date separator is / , for example; or if the date format uses two digits for the year instead of four digits, making the date to fill only 8 positions instead of 10... The variations on format and thus the possible bugs of this code are endless.

One possible solution is to momentarily change the date format to a known format. Insert this code before the loop

....
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH:mm:ss" /f >nul
...

and then, back to original, after the loop.

...
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
...


The following is a batch program that allows you to specify a date to compare against in format mm dd yyyy. You can then specify "on and before" or "on and after" the date and the destination folder. It will even create the folder if it doesn't exist. then the script will move the files.

@ECHO OFF
ECHO Please ensure you are running this batch file from the directory where the files reside. If not, please press CTRL+C to cancel this script and move it to the correct location, then run it again.
PAUSE
SET BorA=none
ECHO Please enter the full path of where you wish your files to be moved to. Example would be C:\Documents and Settings\mechaflash\Desktop\move_folder. Please do not include a trailing \.
SET /p _path=" "

:_date
SET _correct=none
SET /p mm="Two digit month. E.G. 10 = October  "
SET /p dd="Two digit day. E.G. 10 = Day 10  "
SET /p yyyy="4 digit year. E.G. 2010 = Year 2010  "
ECHO Is the date %mm%/%dd%/%yyyy% correct? [1] Yes or [2] No?
SET /p _correct=" "
IF %_correct% EQU 1 GOTO:BorA
IF %_correct% EQU 2 (GOTO:_date) ELSE (ECHO Sorry, you entered an invalid option. & GOTO:_date)
IF "%_correct%"=="none" ECHO Sorry, you entered an invalid option. & GOTO:_date

:BorA
ECHO Would you like to select all files on and before [1], or on and after [2] your entered date of %mm%/%dd%/%yyyy% ?
SET /p BorA=" "
IF %BorA% EQU 1 SET _oper=LEQ
IF %BorA% EQU 2 (SET _oper=GEQ) ELSE (Echo Sorry, you entered an invalid option.  & GOTO:BorA)
IF "%BorA%"=="none" ECHO Sorry, you entered an invalid option. & GOTO:BorA
SET _date=%yyyy%%mm%%dd%
IF NOT EXIST %_path%\NUL MKDIR %_path%

SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (*.*) DO (
 SET var=%%~tA
 SET var2=!var:~0,-9!
 FOR /F "USEBACKQ tokens=1-3 delims=/" %%F IN (`ECHO !var2!`) DO (
  IF %%H%%F%%G %BorA% %_date% MOVE /Y "%%A" "%_path%\~nxA"
 )
)

setting var to %%~tA returns the date with time of the files. setting var2 to !var:~0,-9! removes the time and only leaves the date. The following FOR loop removes the / from the date and rearranges it into yyyymmdd format, allowing it to properly compare dates with operands.

So if you want to leave out all the other stuff, you can just take the following:

SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (*.*) DO (
 SET var=%%~tA
 SET var2=!var:~0,-9!
 FOR /F "USEBACKQ tokens=1-3 delims=/" %%F IN (`ECHO !var2!`) DO (
  IF %%H%%F%%G %BorA% %_date% MOVE /Y "%%A" "%_path%\~nxA"
 )
)

And do some edits to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号