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

オーダーメイド栄養価計算シート生成サービス Nucalgen について

2022/03/08 3:45
オーダーメイド栄養価計算シート生成サービス Nucalgen を公開しました。
この CGI のバックで動かしている Excel ファイル生成プログラムは、 Java で書きました。
ソースコードは GitWeb で公開しています。

ライセンス

Copyright 2020-2022 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.

実行方法の例

java -jar nucalgen.jar -std-food-comp-table 20201225-mxt_kagsei-mext_01110_012.xlsx [-use-processed-table] -columns columns.xml  -output output.xlsx  -lines 20 -bright-colored-vegetables-list bright-colored-vegetables.xml [-sheets 朝食 昼食 夕食 集計] [-addition addition.xml] [-set-protect]
-std-food-comp-table <filename>必須日本食品標準成分表 Excel ファイル
-use-processed-table任意日本食品標準成分表 Excel ファイルの加工をしない
-columns <filename>必須列定義ファイル
-output <filename>必須出力ファイル
-lines <n>必須行数
-bright-colored-vegetables-list <filename>必須緑黄色野菜定義ファイル
-sheets <sheet_name_1> <sheet_name_2> ... <sheet_name_n> <sheet_name_sum>任意(複数可)複数シート(食事区分+集計)を生成する
-addition <filename>任意(複数可)付加行構成ファイル
-set-protect任意入力可能セル(「食品番号」「摂取量」)以外のセルをロックする


続きを読む

2020/04/05(日)mplayer用のPlayListを生成する

2020/11/03 11:32
Perl で書きました。Linux 等向けの mplayer に読ませるために、カレントディレクトリ以下の音楽ファイル(.mp3/.wma)があるディレクトリに、プレイリストを生成していきます。
「ディレクトリを再帰的にたどりファイル一覧を取得したい その1 - Perl - とあるエンジニアの闇歴史帳」(http://tech.katsubemakito.net/perl/dir_recursion1) を参考にさせていただきました。

音楽ファイルは文字コード順で並び替えられるのが前提(例: 00_zzz.mp3 , 01_yyy.mp4 , ...)です。

ソースはこちら。

続きを読む

2020/04/05(日)PDFファイルにインデックス・タブを付加する

2020/11/03 11:31
index_tab.png

このようなページ頭出し用のインデックス・タブ(黒い箱)を PDF ファイルに付け加えます。
奇数ページ/偶数ページで位置(左右)を変えています。A5サイズで調整したので、適宜変更してください。
beta.png という名前の 単色で塗りつぶした画像ファイルを別途用意してください(サイズは何でもいいです。)。

使い方の例
./tab.pl in.pdf out.pdf 1 15,29,44 2
【第0引数】オリジナルPDFファイル名
【第1引数】インデックス・タブ付加後のPDFファイル名
【第2引数】初期 index 位置(上から 1, 2, 3, ...)デフォルト=1
【第3引数】index 位置が変わるページNo. (最初のページは p.1) (ex '15,29,44') デフォルト=なし(一度も変えない)
【第4引数】目次ページNo. (全てのインデックス・タブを描画するページ) デフォルト=目次ページなし
第2引数~第4引数は省略可能です。

Perl で書いています。要PDFtk。
ソースはこちら。

続きを読む