Last active
January 5, 2023 19:29
-
-
Save wcoastsands/46b7ad6ac272ff3c4f8c11d39d630fa2 to your computer and use it in GitHub Desktop.
Move PHD2 log files to avoid deletion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:: MovePHD2Logs.bat | |
:: Written by Nikkolai Davenport (https://github.com/wcoastsands). | |
:: Move PHD2 log files into subfolders to avoid automatic deletion. | |
setlocal | |
set "Title=MovePHD2Logs" | |
set "Version=1.1.9" | |
set "Author=Nikkolai Davenport (https://github.com/wcoastsands)" | |
set "Heading=%Title% (v%Version%)" | |
set "Subheading=Written by %Author%" | |
set "DebugLogDir=DebugLogs" | |
set "GuideLogDir=GuideLogs" | |
set "DebugLogPrefix=PHD2_DebugLog" | |
set "GuideLogPrefix=PHD2_GuideLog" | |
set /A ExitCode=0 | |
if ["%1"]==[""] goto Help | |
if ["%1"]==["/?"] goto Help | |
if /I ["%1"]==["/h"] goto Help | |
if /I ["%1"]==["/help"] goto Help | |
if /I ["%1"]==["/version"] goto Version | |
echo %Heading% | |
echo. %Subheading% | |
echo Checking directories... | |
set "SourceDir=%~f1" | |
call :ValidatePath "Source" "%SourceDir%" | |
if %ExitCode% neq 0 goto End | |
set "SourceDir=%ValidPath%" | |
set "TargetDir=%~f2" | |
if not ["%TargetDir%"]==[""] ( | |
call :ValidatePath "Target" "%TargetDir%" | |
) else ( | |
echo. Target directory not specified. Using source directory as the target. | |
) | |
if %ExitCode% neq 0 goto End | |
set "TargetDir=%ValidPath%" | |
set "DebugLogDir=%TargetDir%\%DebugLogDir%" | |
set "GuideLogDir=%TargetDir%\%GuideLogDir%" | |
echo Checking for log files... | |
call :CountLogFiles "%DebugLogPrefix%" | |
set /A "DebugLogCount=%FileCount%" | |
call :CountLogFiles "%GuideLogPrefix%" | |
set /A "GuideLogCount=%FileCount%" | |
set /A "TotalLogCount=%DebugLogCount%+%GuideLogCount%" | |
if %TotalLogCount% gtr 0 ( | |
echo Preparing to move log files... | |
if %DebugLogCount% gtr 0 call :MakeDirectory "%DebugLogDir%" | |
if %GuideLogCount% gtr 0 call :MakeDirectory "%GuideLogDir%" | |
if %DebugLogCount% gtr 0 call :MoveLogFiles "%DebugLogPrefix%" "%DebugLogDir%" | |
if %GuideLogCount% gtr 0 call :MoveLogFiles "%GuideLogPrefix%" "%GuideLogDir%" | |
) | |
echo DONE. | |
goto End | |
:Help | |
echo %Heading% | |
echo. %Subheading% | |
echo. | |
echo Usage: | |
echo. MovePHD2Logs [ /? ^| /version ^| [SourceDir] ] [TargetDir] | |
echo. | |
echo Options: | |
echo. /? Display this help message. | |
echo. /version Display the version ^(https://semver.org/^). | |
echo. [SourceDir] The log file location used by PHD2. | |
echo. [TargetDir] The parent location of log file subfolders. | |
echo. | |
echo Description: | |
echo. Moves PHD2 log files into subfolders to avoid automatic deletion. | |
echo. | |
echo. PHD2 automatically deletes debug logs older than 30 days, and guide logs | |
echo. older than 60 days. Without an option to disable the feature, this utility | |
echo. provides a means to circumvent it. This utility is intended to run | |
echo. primarily as a scheduled task using the Windows Task Scheduler. | |
echo. | |
echo. Automatically running this utility at regular intervals can help | |
echo. prevent any further unwanted loss of log data. | |
echo. | |
echo. The location of PHD2 log files is determined by the "Log File Location" | |
echo. field found under the "Global" tab of the "Advanced Settings" dialog. | |
echo. | |
echo. Providing a target location is optional. If a target is not provided, the | |
echo. source location will be assigned to the target, and will be used as the | |
echo. parent directory of the log file subfolders. | |
echo. | |
echo. Log Files are sorted into one of two subfolders under the target directory | |
echo. depending on the prefix used in the naming of log files: | |
echo. | |
echo. DebugLogs Subfolder for log files with the PHD2_DebugLog prefix. | |
echo. GuideLogs Subfolder for log files with the PHD2_GuideLog prefix. | |
echo. | |
echo. As a best practice, it is recommended that the target directory be located | |
echo. outside of the source directory to avoid the possiblity of log files being | |
echo. recursively deleted by PHD2. | |
echo. | |
echo. For instance: | |
echo. | |
echo. %%USERPROFILE%% | |
echo. +---Dropbox | |
echo. ^| +---PHD2 Parent directory (Target). | |
echo. ^| +---DebugLogs Subfolder for files with PHD2_DebugLog prefix. | |
echo. ^| +---GuideLogs Subfolder for files with PHD2_GuideLog prefix. | |
echo. +---PHD2 Log file location used by PHD2 (Source). | |
echo. | |
echo Examples: | |
echo. MovePHD2Logs /? | |
echo. MovePHD2Logs /version | |
echo. MovePHD2Logs "%%USERPROFILE%%\PHD2" | |
echo. MovePHD2Logs "%%USERPROFILE%%\PHD2" "%%USERPROFILE%%\Dropbox\PHD2" | |
goto End | |
:Version | |
echo %Version% | |
goto End | |
:End | |
exit /B %ExitCode% | |
:ValidatePath | |
set "PathType=%~1" | |
set "ValidPath=%~2" | |
:: Remove trailing slash to avoid escape issues with double quotes. | |
if ["%ValidPath:~-1%"]==["\"] set "ValidPath=%ValidPath:~0,-1%" | |
if exist "%ValidPath%" ( | |
echo. %PathType% directory ready ^("%ValidPath%"^). | |
) else ( | |
set /A ExitCode+=1 | |
echo. ERROR! %PathType% directory not found ^("%ValidPath%"^). | |
) | |
if %ExitCode% neq 0 exit /b 1 | |
goto :eof | |
:CountLogFiles | |
set /A FileCount=0 | |
set "FilePrefix=%~1" | |
for %%G in ("%SourceDir%\%FilePrefix%*") do ( | |
set /A FileCount+=1 | |
) | |
echo. Found %FileCount% log files with prefix "%FilePrefix%". | |
goto :eof | |
:MakeDirectory | |
set "TargetPath=%~1" | |
if not exist "%TargetPath%" ( | |
mkdir "%TargetPath%" | |
echo. Created new directory: "%TargetPath%" | |
) else ( | |
echo. Subfolder ready: "%TargetPath%" | |
) | |
goto :eof | |
:MoveLogFiles | |
set "FilePrefix=%~1" | |
set "TargetPath=%~2" | |
echo Moving "%FilePrefix%" files... | |
move "%SourceDir%\%FilePrefix%*.txt" "%TargetPath%" | |
goto :eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment