我有一个文件夹,其中包含具有特定格式的文件(321456.done24),我希望每年使用批处理日期归档此文件...这意味着在此文件夹中创建另一个文件夹,如果不是存在,以及过去几年的所有文件 - 除了当前年份 - 将它们移到那里。 我找到并尝试修改的一个例子:`
@echo off
setlocal enabledelayedexpansion
set FolderIncoming=C:\Temp
set FileMask= *.done24
set FolderSorted=C:\Temp
for %%a in ("%FolderIncoming%\%FileMask%") do (
set FileName=%%~na
echo Processing '!FileName!' ...
set TargetFolder=!FileName:~5,8!
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
`
但不知道如何用年份创建文件夹?
答案 0 :(得分:1)
只需对代码段进行必要的更改:
@ECHO OFF >NUL
@SETLOCAL enableextensions enabledelayedexpansion
set "FolderIncoming=D:\Temp"
set "FileMask=*.done24"
set "FolderSorted=D:\Temp"
pushd "%FolderIncoming%"
for %%a in ("%FolderIncoming%\%FileMask%") do (
set "FileName=%%~na"
echo Processing '!FileName!' ... %%a ... %%~ta
rem next tokens and delims valid for 'dd.mm.yyyy HH:mm:ss' datetime format
for /F "tokens=3 delims=. " %%G in ("%%~ta") do set "fileyear=%%G"
echo md "%FolderSorted%\!fileyear!" 2>nul
set "TargetFolder=!FileName:~5,8!"
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)
popd
:: some code here
@ENDLOCAL
@goto :eof