Windows 起動後, ネットワーク接続が安定するまで動作を待つためのプログラム

2024/02/09 19:23

Windows 起動時のスタートアップ処理として、ネットワークドライブにドライブレターを割り付けるバッチファイル (中身は net use コマンドなど) を呼び出しているのですが、思いのほかネットワーク接続の確立に時間がかかり、動作に失敗してしまうことがあります。バッチファイルから呼び出す形で、ネットワーク接続が安定するまで次のコマンドの実行を遅延させるプログラムを書きました。

ソースコード waitnet.vb

' Copyright 2023 FUKUDA Satomi (https://satomichan.jp/)
' 
' Licensed under the Apache License, Version 2.0 (the “License”);
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an “AS IS” BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' 
' See the License for the specific language governing permissions and
' limitations under the License.

Public Module WaitNet
    Public Function Main(args As String()) As Integer
        'コマンドライン引数
        If args.Length <> 4 Then
            Console.WriteLine("USAGE:  waitnet <host> <port> <timeout> <retry>")
            Console.WriteLine()
            Console.WriteLine("ex)  waitnet.exe www.nhk.or.jp 80 2 3")
            Return 255
        End If

        Dim host    As String  = args(0)
        Dim port    As Integer = Integer.parse(args(1))
        Dim timeout As Integer = Integer.parse(args(2))
        Dim retry   As Integer = Integer.parse(args(3))

        Dim tcp As New System.Net.Sockets.TcpClient



        ' 接続
        Dim IsConnectSuccess As Boolean
        Do
            IsConnectSuccess = true
            Try
                Dim task = tcp.ConnectAsync(host, port)
                If Not task.wait(timeout * 1000) Then
                    IsConnectSuccess = false
                End If
            Catch ex As System.Exception
                IsConnectSuccess = false
            End Try

            If IsConnectSuccess Then Exit Do
            If retry = 0 Then Exit Do

            Console.WriteLine("ReTry: More " & retry & " times To Go.")
            retry = retry - 1
            System.Threading.Thread.Sleep(5000)
        Loop While retry >= 0



        ' 結果
        Console.WriteLine("IsConnectSuccess: " & IsConnectSuccess)
        If IsConnectSuccess Then
            Return 0
        Else
            Return 1
        End If

    End Function
End Module

VB.NET で書かれていて、Windows 附属のコンパイラ (vbc.exe) でコンパイル出来ます。

コンパイルのしかた (例)

>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\vbc.exe waitnet.vb
Microsoft (R) Visual Basic Compiler version 14.8.9037
for Visual Basic 2012
Copyright (c) Microsoft Corporation.  All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to Visual Basic 2012, which is no longer the latest version. For compilers that support newer versions of the Visual Basic programming language, see http://go.microsoft.com/fwlink/?LinkID=533241


>

ネットワーク接続が安定するまで遅延実行させたい処理の書き方 (バッチファイルの例)

@echo off
C:\waitnet.exe 192.168.1.1 445 5 10
if ERRORLEVEL 1 goto ERR

net use N: \\192.168.1.1\nas passwwww /user:user2

:ERR
exit

数式参照先シフト Excel など表計算ソフト用 数式参照先セルの座標を必要な分だけ上下左右にずらした式を得るためのツール

2023/12/26 12:42

Excel などの表計算ソフト利用時に、数式内で参照しているセルの座標を必要な分だけ上下左右にずらしたいときがあります。一つや二つならばマウスでドラッグすることで容易にずらせますが、対象の数が多いと大変です。そこで、この作業を簡単に行えるツールをつくりました。

Excel などから数式をコピーして次のテキストボックスに貼り付けて、移動する数を指定し「実行」ボタンを押します。そうするとその下のテキストボックスに移動後の数式が現れるので、それを全て選択してコピー、Excel などに戻り先ほどコピーしたのと同じ場所で貼り付けをします。絶対参照のときにもシフトするかどうかはチェックボックスで選択できます。

JavaScript で実装しています。入力された数式などのデータはどこにも送信せず、いまこのページをご覧のコンピュータ内のみで処理しています。

ソース (ここに Excel から数式を貼り付ける)



行 増分 下へセル (上へ移動させる場合はマイナス値を入力)  
列 増分 右へセル (左へ移動させる場合はマイナス値を入力)  

結果 (ここから数式を Excel にコピーする)


数式のコピーのしかた

Excel での 数式のコピーのしかた

例えば Excel だと、①「数式」メニュー、②「数式の表示」を順にクリックするとセル内に数式が表示されるので、必要な範囲をドラッグで選択して、右クリック・メニューから「コピー」をします。