Windowsのコマンドプロンプトで同じ階層のファイルをyyyy-mmフォルダに移動するバッチ例

単発小ネタ　Windowsバッチ

# 20240927_Windowsのコマンドプロンプトで同じ階層のファイルをyyyy-mmフォルダに移動するバッチ例  
  
## 機能  
- 起動するとbatの置いてあるディレクトリのファイルを、更新月のディレクトリ作成して移動します。
- 拡張子batはスルーする仕様です。
- 写真を取り敢えず整理したい時に作りました。

```file_move_to_mtime_dir_v001.bat

@echo off

setlocal EnableDelayedExpansion

set CDIR=%~dp0
cd /d %CDIR%
IF ERRORLEVEL 1 GOTO ERR

ECHO.
echo. checking files...

set CNTCMD=dir /a-d /b ^|find /c /v ""
for /f "usebackq delims=" %%A in (`!CNTCMD!`) do set FILENUM=%%A

ECHO.
ECHO **************** start ****************

echo. mkdir and move to yyyy-mm dir
echo. target dir : %CDIR%
echo. target file num : !FILENUM!
echo. yyyy-mm is each file's mod time

ECHO.
SET /p pushKey04="OK? (y/n) : %pushKey04%"

IF "%pushKey04%"=="y" (
  ECHO.
  ECHO *************** select execute ***************
  GOTO MAIN

) ELSE IF "%pushKey04%"=="n" (
  ECHO.
  ECHO *************** select halt ***************
  GOTO HALT

) ELSE (
  ECHO.
  ECHO type y or n
  GOTO HALT
)

:MAIN
COLOR 3F

SET CDATE=%date:~0,4%%date:~5,2%%date:~8,2%
IF "%time:~0,1%"==" " (
  SET CTIME=0%time:~1,1%%time:~3,2%%time:~6,2%
) ELSE (
  SET CTIME=%time:~0,2%%time:~3,2%%time:~6,2%
)


SET LOGFNAME=move_result
mkdir %LOGFNAME%\ 2> nul 1>&2

SET LOGF=%LOGFNAME%\%CDATE%_%CTIME%_move_log.txt

ECHO. > %LOGF%
ECHO !date! !time! >> %LOGF%
ECHO **** **** **** ****  start **** **** **** ****  >> %LOGF%
echo. target dir : %CDIR% >> %LOGF%
ECHO. >> %LOGF%

set CNTCMD=dir /a-d /b ^|find /c /v ""
for /f "usebackq delims=" %%A in (`!CNTCMD!`) do set FILENUM=%%A
echo file num : !FILENUM!
echo.

ECHO.
ECHO *************** move start ***************
ECHO.
ECHO. target dir : %CDIR%

set /a total_file_num=0
set /a total_file_size=0

for  %%f in (*.*) do (
  set /a total_file_num=!total_file_num! + 1
  echo **** [now : !total_file_num! / !FILENUM!] %%f ****
  echo **** [now : !total_file_num! / !FILENUM!] %%f **** >> %LOGF%
  
  if %%~xf==.bat (
    echo skip bat file
    echo. skip bat file >> %LOGF%
  ) else (
    set SIZE=%%~zf
    set /a total_file_size=!total_file_size! + !SIZE!
    set MTIME=%%~tf
    set MTIMED=!MTIME:~0,4!-!MTIME:~5,2!
    mkdir "!MTIMED!" > nul 2>&1
    move "%%f" "!MTIMED!" > nul 2>&1
  )
)

echo.
echo **** end **** **** **** 
echo.

set /a total_file_size_kb=!total_file_size! / 1024
set /a total_file_size_mb=!total_file_size_kb! / 1024
set /a total_file_size_gb=!total_file_size_mb! / 1024

ECHO.
ECHO **************** move end ****************
ECHO. >> %LOGF%
ECHO **** **** **** ****   end  **** **** **** ****  >> %LOGF%
ECHO. total_file_num : !total_file_num!
ECHO. total_file_size : !total_file_size! byte
ECHO. total_file_size(kb) : !total_file_size_kb! kb
ECHO. total_file_size(mb) : !total_file_size_mb! mb
ECHO. total_file_size(gb) : !total_file_size_gb! gb
ECHO. total_file_num : !total_file_num! >> %LOGF%
ECHO. total_file_size : !total_file_size! byte >> %LOGF%
ECHO. total_file_size(kb) : !total_file_size_kb! kb >> %LOGF%
ECHO. total_file_size(mb) : !total_file_size_mb! mb >> %LOGF%
ECHO. total_file_size(gb) : !total_file_size_gb! gb >> %LOGF%
ECHO. >> %LOGF%
ECHO !date! !time! >> %LOGF%
ECHO. >> %LOGF%

COLOR 07

ECHO.
ECHO nomal end

:END
ECHO.
ECHO close after 4s
ECHO.
ECHO **************** end ****************
ping 127.0.0.1 -n 4 > nul 2>&1

pause
exit

:ERR
COLOR 4E

ECHO.
ECHO error
ECHO maybe UMC path not supported. like "\\192...."
ECHO mount and assign drive letter
GOTO END

:HALT
COLOR 70

ECHO.
ECHO halt 

GOTO END

```
## その他
- いつか出会うと思われる「遅延環境変数展開」と戯れることができます。