方法1

以下は、Windowsサーバから3台のサーバの共有フォルダにデータをローテーションしながらコピーするためのスクリプトの例です。このスクリプトは、robocopyコマンドを使用しています。

bat

@echo off

REM ローテーション対象の共有フォルダのリスト
set "destinationFolders=\\server1\share \\server2\share \\server3\share"

REM コピー元のフォルダとファイル
set "sourceFolder=C:\Path\To\Source\Folder"

REM ローテーションのインデックスを取得
set "rotationIndexFile=C:\Path\To\Rotation\Index\File.txt"
set /p "currentRotationIndex=" < "%rotationIndexFile%"
if not defined currentRotationIndex (
    set "currentRotationIndex=0"
)

REM ローテーション対象の共有フォルダを選択
for /f "tokens=1,%currentRotationIndex% delims= " %%a in ("%destinationFolders%") do set "destinationFolder=%%a"

REM ファイルをコピー
robocopy "%sourceFolder%" "%destinationFolder%" /E /R:1 /W:1

REM ローテーションのインデックスを更新
set /a "nextRotationIndex=(currentRotationIndex + 1) %% 3"
echo %nextRotationIndex% > "%rotationIndexFile%"

上記のスクリプトでは、以下の手順を実行します。

  1. destinationFoldersで指定された共有フォルダのリストを設定します。
  2. sourceFolderで指定されたコピー元のフォルダとファイルを設定します。
  3. ローテーションのためのインデックスをrotationIndexFileから読み取ります。ファイルが存在しない場合や内容が空の場合は、インデックスを0として扱います。
  4. currentRotationIndexを使用して、次にローテーション対象の共有フォルダを選択します。
  5. robocopyコマンドを使用して、指定されたフォルダとファイルを再帰的にコピーします。
  6. 次のローテーションインデックスを計算し、rotationIndexFileに書き込みます。

このスクリプトを使用すると、毎回次の共有フォルダにデータをローテーションしながらコピーすることができます。適切なパスとフォルダ構造に応じて、スクリプトをカスタマイズしてください。

 

方法2

 

以下は、Windowsサーバから3台のサーバの共有フォルダにデータをローテーションしながらコピーするためのスクリプトの例です。このスクリプトPowerShellを使用しています。

powershell

# ローテーション対象の共有フォルダのリスト
$destinationFolders = @("\\server1\share", "\\server2\share", "\\server3\share")

# コピー元のフォルダとファイル
$sourceFolder = "C:\Path\To\Source\Folder"

# ローテーションのインデックスを取得
$currentRotationIndex = (Get-Item -Path "C:\Path\To\Rotation\Index\File.txt" -ErrorAction SilentlyContinue).Content
if ([string]::IsNullOrEmpty($currentRotationIndex)) {
    $currentRotationIndex = 0
} else {
    $currentRotationIndex = [int]$currentRotationIndex
}

# ローテーション対象の共有フォルダを選択
$destinationFolder = $destinationFolders[$currentRotationIndex]

# ファイルをコピー
Copy-Item -Path $sourceFolder -Destination $destinationFolder -Recurse

# ローテーションのインデックスを更新
$nextRotationIndex = ($currentRotationIndex + 1) % $destinationFolders.Count
$nextRotationIndex | Set-Content -Path "C:\Path\To\Rotation\Index\File.txt"

上記のスクリプトでは、以下の手順を実行します。

  1. $destinationFoldersで指定された共有フォルダのリストを作成します。
  2. $sourceFolderで指定されたコピー元のフォルダとファイルを指定します。
  3. ローテーションのためのインデックスをC:\Path\To\Rotation\Index\File.txtから読み取ります。ファイルが存在しない場合や内容が空の場合は、インデックスを0として扱います。
  4. $currentRotationIndexを使用して、次にローテーション対象の共有フォルダを選択します。
  5. Copy-Itemコマンドレットを使用して、ファイルを指定された共有フォルダに再帰的にコピーします。
  6. 次のローテーションインデックスを計算し、C:\Path\To\Rotation\Index\File.txtに書き込みます。

このスクリプトを使用すると、毎回次の共有フォルダにデータをローテーションしながらコピーすることができます。適切なパスとフォルダ構造に応じて、スクリプトをカスタマイズしてください。