我无法弄清楚如何按日期将文件分成文件夹。
我有1,000,000个文件,1个文件夹中的所有文件都让Explorer病了:P 所以我想按日期将它们分成不同的文件夹:
11年1月9日
11年2月9日
03-09-11
等
答案 0 :(得分:1)
提取文件日期相对简单,请参阅HELP CALL
并尝试使用这个简单的BAT文件
@echo off
setlocal enabledelayedexpansion
FOR %%A IN (*.*) DO (
set tf=%%~tA
echo %%~fA ... !tf!
)
超越这种方法解决问题似乎非常简单......
@echo off
setlocal enabledelayedexpansion
FOR %%A IN (*.*) DO (
set tf=%%~tA
set fd=!tf:~0,10!
md !fd!
move /Y %%~fA !fd!
)
但是,请等一下,保证不会运行该代码。日期格式存在一些依赖性,可能会阻止此简单代码运行。处理BAT文件中的日期并不容易,因为日期格式取决于区域设置,甚至取决于自定义首选项。在这个特定的部分中,例如,如果日期分隔符为/
,它将会造成严重破坏;或者如果日期格式使用年份的两位数而不是四位数,则使日期仅填充8个位置而不是10个...格式的变化以及此代码的可能错误是无穷无尽的。
一种可能的解决方案是暂时将日期格式更改为已知格式。在循环之前插入此代码
....
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
...
然后,回到原来的循环之后。
...
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
...
答案 1 :(得分:0)
以下是批处理程序,允许您指定要比较格式mm dd yyyy的日期。然后,您可以指定日期和目标文件夹“之前和之后”或“之前和之后”。如果它不存在,它甚至会创建该文件夹。然后脚本将移动文件。
@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"
)
)
将var设置为%% ~tA返回文件的时间日期。将var2设置为!var:~0,-9!删除时间,只留下日期。以下FOR循环从日期中删除/
并将其重新排列为yyyymmdd格式,允许它正确地将日期与操作数进行比较。
因此,如果你想省略所有其他内容,你可以采取以下措施:
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"
)
)
并对其进行一些修改。