LoginSignup
0
0

More than 5 years have passed since last update.

Yahoo! 知恵袋(q14155643874)

Posted at

「Yahoo! 知恵袋」の以下の質問をやっつけで解いてみました。

q14155643874.bat
@echo off

set SELF=%~n0
set HELP=usage: %SELF% NUMBER
set TEST=1

setlocal enabledelayedexpansion
  set padding=0000000000

  set cwd=%~dp0

  set n=%~1
  if "%n%" == "" (
    >&2 echo %HELP%
    exit /b 1
  )
  (echo %n%) | findstr /r "[^0-9]." && (
    >&2 echo %HELP%
    exit /b 1
  )

  set number_of_files=100
  call :length "%number_of_files%"
  set digits=%ERRORLEVEL%

if %TEST% equ 1 (
  rem make files: file-000
  set /a number_of_files-=1
  for /l %%i in (0, 1, !number_of_files!) do (
    rem n digits with space
    set i=%padding%%%i
    set i=!i:~-%digits%!
    copy NUL "%cwd%\file-!i!.txt" > NUL
  )
)

  set i=0
  for /f "tokens=*" %%f in ('dir /b /a-d "%cwd%"') do (
    if "%%~nxf" == "%~nx0" (
      rem NOP: exclude this file
    ) else (
        rem make directories: directory-000
        set /a remainder=!i!%%%n%
        if !remainder! equ 0 (
          set /a quotient=!i!/%n%
          rem n digits with space
          set d=%padding%!quotient!
          set d=directory-!d:~-%digits%!
          if not exist "%cwd%\!d!\" (
            md "%cwd%\!d!" > NUL
          )
        )

        move "%cwd%\%%f" "%cwd%\!d!" > NUL

        set /a i+=1
    )
  )

  tree /f

if %TEST% equ 1 (
  rem delete files and directories
  for /f "tokens=*" %%d in ('dir /b /ad "%cwd%"') do (
    rd /s /q "%cwd%\%%d"
  )
)

  exit /b 0
endlocal
goto :EOF

:length
setlocal
  set s=%~1

  if "%s%" == "" (
    exit /b 0
  )

  set i=0
  :BOL
    call set c=%%s:~%i%,1%%
    if "%c%" == "" goto EOL
    set /a i+=1
    goto BOL
  :EOL

  exit /b %i%
endlocal
goto :EOF

バッチ変数 TEST の値に 1 をセットします。

set TEST=1

バッチファイルを実行します。

C:\sandbox>q14155643874.bat 3
フォルダー パスの一覧
ボリューム シリアル番号は XXXXXXXX XXXX:XXXX です
C:.
│  q14155643874.bat
│
├─directory-000
│      file-000.txt
│      file-001.txt
│      file-002.txt
│
├─directory-001
│      file-003.txt
│      file-004.txt
│      file-005.txt
│
├─directory-002
│      file-006.txt
│      file-007.txt
│      file-008.txt
│
...
│
├─directory-031
│      file-093.txt
│      file-094.txt
│      file-095.txt
│
├─directory-032
│      file-096.txt
│      file-097.txt
│      file-098.txt
│
└─directory-033
        file-099.txt

最初に 100 個のファイルを作成し、フォルダーを作成しながら各フォルダーの中にファイルが 3 個ずつ移動することを確認しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0