ray88’s diary

お仕事で困ったとき用の自分用の覚書

各種目次

ChatGPT 目次

PostgreSQL 目次 

SQL 指南書集 目次

DOMO 目次

Tableau 目次 

Skack 目次 

テストデータ生成用URL一覧 

テストデータ作成 

python 目次

ChatGPT 目次 

UiPath 目次

Uipath モダンエクスペリエンス 目次 

ExcelVBA 目次

Access VBA 目次 

Excel & Access 連携 目次

Access関数 目次 

VBS 目次

GAS 目次 

Google Colaboratory 目次 

PowerShell 目次

PowerPoint 目次 

.Net 目次

SQL 目次 

SQL SERVER 目次 

HTML 目次

CSS 目次 

JavaScript 目次

正規表現 目次 

はてなブログ編集方法 参考URL集

環境構築関連 目次

VSCode 目次 

IE操作 目次 

Edge操作 目次 

WodowsOs関連 目次

四則演算など計算式を英語で読む一覧 - IT系の英語表現を学ぶ

iphone 4Gしか入らないようにする設定

ロジクール製品関連

Chromeで検索エンジンが勝手にGoogleからYahooに変わってしまう原因とその対処

【裏技】ベン図の中心部分の色をつける方法 #Excel | Excelを制する者は人生を制す

会計 目次 

【iPhone】Googleカレンダーだけ背景を白くしたい!ダークモードを解除する方法を紹介します。 | こぶたのピグちゃん

【iPhone】勝手に背景が黒くなった!「ダークモード」を解除して白に戻す方法を紹介します。 | こぶたのピグちゃん

 

Uipath VBSを実行する(指定した日数よりも前の日付フォルダを削除)

UiPath 目次 - ray88’s diary

※備考:今回はVBScriptでバッチを実行したが、文字コードの問題上、出力引数に結果を日本語格納しようとすると文字化けし、これについての解決方法が最後まで不明だった。なお、入力引数に日本語が含まれており、スクリプト内で処理することについては問題なく動作する。出力引数に格納する時点で文字化けが起こるようだ。UTF-8形式のファイルにしたり、ユニコード対応にチェックをいれるなどしても駄目だった。

指定した親フォルダパスのフォルダ内にあるサブフォルダ名(yyyyMMddHHmmssの日付形式のフォルダ名)
 を取得し、指定した日数よりも前の日付の名前のついたサブフォルダを削除する


■バッチファイル(VBScirpt・ANSI形式・拡張子.vbs)

' 文字列を日付形式に変換する関数
Function ConvertToDate(strDate)
    On Error Resume Next ' エラーハンドリングの開始
    Dim year, month, day, hour, minute, second
    year = Mid(strDate, 1, 4)
    month = Mid(strDate, 5, 2)
    day = Mid(strDate, 7, 2)
    hour = Mid(strDate, 9, 2)
    minute = Mid(strDate, 11, 2)
    second = Mid(strDate, 13, 2)

    ConvertToDate = DateSerial(year, month, day) & " " & TimeSerial(hour, minute, second)

    If Err.Number <> 0 Then
        ConvertToDate = "Invalid Date"
        Err.Clear
    End If
    On Error GoTo 0 ' エラーハンドリングの終了
End Function

' 日付が指定された日数より前かどうかを判定する関数
Function IsOlderThan(dateString, days)
    Dim folderDate
    folderDate = ConvertToDate(dateString)
    
    If folderDate = "Invalid Date" Then
        IsOlderThan = "Invalid Date"
    Else
        IsOlderThan = DateDiff("d", folderDate, Now) > days
    End If
End Function

' サブフォルダを削除するサブルーチン
Sub DeleteSubFolder(folderPath)
    Dim FileSystemObject
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
    
    If FileSystemObject.FolderExists(folderPath) Then
        FileSystemObject.DeleteFolder folderPath
    End If

    Set FileSystemObject = Nothing
End Sub

' UiPathからの引数を受け取る
Dim ParentFolderPath, DaysString
ParentFolderPath = WScript.Arguments(0)
DaysString = WScript.Arguments(1)

' 文字列型の日数を数値型に変換
Dim Days
Days = CInt(DaysString)

Dim FileSystemObject, ParentFolder, SubFolder, olderThanResult
Dim foldersToDelete
Set foldersToDelete = CreateObject("Scripting.Dictionary")

Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

If FileSystemObject.FolderExists(ParentFolderPath) Then
    Set ParentFolder = FileSystemObject.GetFolder(ParentFolderPath)
    For Each SubFolder in ParentFolder.SubFolders
        olderThanResult = IsOlderThan(SubFolder.Name, Days)
        If olderThanResult = True Then
            foldersToDelete.Add SubFolder.Path, SubFolder.Path
        End If
    Next
    
    ' 削除対象のサブフォルダを削除
    Dim folderPathToDelete
    For Each folderPathToDelete in foldersToDelete.Keys()
        FileSystemObject.DeleteFolder folderPathToDelete
    Next
    
    WScript.Echo "OK" ' 正常終了
Else
    WScript.Echo "NG" ' 指定された親フォルダが存在しない場合
End If

Set FileSystemObject = Nothing

■Uipath側の設定
Invoke VBScript アクティビティは入力引数も出力引数も文字型

VBS 指定した日数より前のサブフォルダ名のサブフォルダを削除する部品

VBS 目次 - ray88’s diary

' 文字列を日付形式に変換する関数
Function ConvertToDate(strDate)
    On Error Resume Next ' エラーハンドリングの開始
    Dim year, month, day, hour, minute, second
    year = Mid(strDate, 1, 4)
    month = Mid(strDate, 5, 2)
    day = Mid(strDate, 7, 2)
    hour = Mid(strDate, 9, 2)
    minute = Mid(strDate, 11, 2)
    second = Mid(strDate, 13, 2)

    ConvertToDate = DateSerial(year, month, day) & " " & TimeSerial(hour, minute, second)

    If Err.Number <> 0 Then
        ConvertToDate = "Invalid Date"
        Err.Clear
    End If
    On Error GoTo 0 ' エラーハンドリングの終了
End Function

' 日付が指定された日数より前かどうかを判定する関数
Function IsOlderThan(dateString, days)
    Dim folderDate
    folderDate = ConvertToDate(dateString)
    
    If folderDate = "Invalid Date" Then
        IsOlderThan = "Invalid Date"
    Else
        IsOlderThan = DateDiff("d", folderDate, Now) > days
    End If
End Function

' サブフォルダを削除するサブルーチン
Sub DeleteSubFolder(folderPath)
    Dim FileSystemObject
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
    
    If FileSystemObject.FolderExists(folderPath) Then
        FileSystemObject.DeleteFolder folderPath
    End If

    Set FileSystemObject = Nothing
End Sub

Dim ParentFolderPath, Days
ParentFolderPath = "ここに親フォルダパスを入力" ' 親フォルダのパスを設定
Days = 3 ' ここに日数を入力

Dim FileSystemObject, ParentFolder, SubFolder, olderThanResult
Dim foldersToDelete
Set foldersToDelete = CreateObject("Scripting.Dictionary")

Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

If FileSystemObject.FolderExists(ParentFolderPath) Then
    Set ParentFolder = FileSystemObject.GetFolder(ParentFolderPath)
    For Each SubFolder in ParentFolder.SubFolders
        olderThanResult = IsOlderThan(SubFolder.Name, Days)
        If olderThanResult = True Then
            foldersToDelete.Add SubFolder.Path, SubFolder.Path
        ' ElseIf olderThanResult = "Invalid Date" Then
            ' 日付形式が無効の場合は削除しない
        ' Else
            ' 日数よりも前ではないため削除しない
        End If
    Next
    
    ' 削除対象のサブフォルダを削除
    Dim folderPathToDelete
    For Each folderPathToDelete in foldersToDelete.Keys()
        FileSystemObject.DeleteFolder folderPathToDelete
        ' MsgBox "Deleted: " & folderPathToDelete ' メッセージボックスの表示を削除
    Next
Else
    ' MsgBox "Specified parent folder does not exist: " & ParentFolderPath ' メッセージボックスの表示を削除
End If

Set FileSystemObject = Nothing