getlog.cmd 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. @echo off
  2. REM Usage:
  3. REM Normal mode: getlog <caseno> [filename]
  4. REM Setup mode: getlog setup
  5. REM Folder setup: getlog foldersetup
  6. REM Install: getlog install
  7. set "SCRIPT_DIR=%~dp0"
  8. set "SCRIPT_PATH=%~f0"
  9. set "CONFIG_FILE=%SCRIPT_DIR%getlog_config.dat"
  10. set "FOLDER_CONFIG=%SCRIPT_DIR%getlog_folder.dat"
  11. REM Check for setup modes
  12. if /i "%~1"=="setup" (
  13. call :setup
  14. goto :eof
  15. )
  16. if /i "%~1"=="foldersetup" (
  17. call :foldersetup
  18. goto :eof
  19. )
  20. if /i "%~1"=="install" (
  21. call :install
  22. goto :eof
  23. )
  24. if /i "%~1"=="uninstall" (
  25. call :uninstall
  26. goto :eof
  27. )
  28. REM Normal mode - check for required arguments
  29. if "%~1"=="" (
  30. echo Usage: %~n0 ^<caseno^> [filename]
  31. echo.
  32. echo Examples:
  33. echo %~n0 1010011 support.tgz - Download specific file
  34. echo %~n0 1010011 - Download entire case directory
  35. echo.
  36. echo Setup mode: %~n0 setup
  37. echo Folder setup: %~n0 foldersetup
  38. echo Install: %~n0 install
  39. exit /b 1
  40. )
  41. REM Load username from config or use default
  42. set "USERNAME=pashwani"
  43. if exist "%CONFIG_FILE%" (
  44. set /p USERNAME=<"%CONFIG_FILE%"
  45. )
  46. REM Load folder preference or use default (SJLNT structure)
  47. set "FOLDER_MODE=SJLNT"
  48. if exist "%FOLDER_CONFIG%" (
  49. set /p FOLDER_MODE=<"%FOLDER_CONFIG%"
  50. )
  51. set "CASE=%~1"
  52. REM Check if filename is provided
  53. if "%~2"=="" (
  54. set "DOWNLOAD_MODE=dir"
  55. echo No filename provided. Will download entire case directory.
  56. ) else (
  57. set "DOWNLOAD_MODE=file"
  58. set "FILE=%~2"
  59. )
  60. REM Determine destination based on folder mode
  61. if /i "%FOLDER_MODE%"=="OLD" (
  62. set "DEST=%USERPROFILE%\Downloads\Traces n Logs\%CASE%"
  63. echo Using OLD folder structure: Traces n Logs
  64. ) else (
  65. set "DEST=%USERPROFILE%\Downloads\SJLNT\%CASE%"
  66. echo Using NEW folder structure: SJLNT
  67. )
  68. REM Create destination folder if it doesn't exist
  69. if not exist "%DEST%" (
  70. mkdir "%DEST%" 2>nul
  71. if errorlevel 1 (
  72. echo ERROR: Cannot create directory: %DEST%
  73. echo Please check your permissions or disk space.
  74. exit /b 1
  75. )
  76. echo Created folder: %DEST%
  77. )
  78. REM Perform download based on mode
  79. if "%DOWNLOAD_MODE%"=="dir" (
  80. echo Downloading entire case directory using username: %USERNAME%
  81. echo Remote source: %USERNAME%@sjanalysis.citrite.net:/upload/ftp/%CASE%
  82. echo Local destination: %DEST%
  83. echo.
  84. echo This may take some time depending on the directory size...
  85. echo.
  86. REM First, let's test the connection and check if the remote directory exists
  87. echo Testing connection to remote server...
  88. ssh -o BatchMode=yes -o ConnectTimeout=5 "%USERNAME%@sjanalysis.citrite.net" "ls -la /upload/ftp/%CASE%/ 2>/dev/null || echo 'Directory not found'" >nul 2>&1
  89. echo Starting download...
  90. REM Use scp -r for recursive directory copy - REMOVE trailing slash from source
  91. REM The issue was likely with the trailing slash in the source path
  92. scp -rp "%USERNAME%@sjanalysis.citrite.net:/upload/ftp/%CASE%" "%DEST%"
  93. if errorlevel 1 (
  94. echo.
  95. echo SCP failed with error code: %errorlevel%
  96. echo Possible issues:
  97. echo 1. Case directory %CASE% doesn't exist on remote server
  98. echo 2. Network connectivity issue
  99. echo 3. Incorrect username
  100. echo.
  101. echo You can run 'getlog setup' to reconfigure username.
  102. echo To check if directory exists: ssh %USERNAME%@sjanalysis.citrite.net "ls -la /upload/ftp/%CASE%/"
  103. exit /b 1
  104. )
  105. echo.
  106. echo Entire case directory downloaded successfully to: %DEST%
  107. echo.
  108. REM Show downloaded contents
  109. echo Directory contents:
  110. echo -------------------
  111. dir /b "%DEST%" 2>nul || echo (No files downloaded or directory empty)
  112. ) else (
  113. echo Downloading file using username: %USERNAME%
  114. echo Remote file: /upload/ftp/%CASE%/%FILE%
  115. scp -p "%USERNAME%@sjanalysis.citrite.net:/upload/ftp/%CASE%/%FILE%" "%DEST%"
  116. if errorlevel 1 (
  117. echo SCP failed with error code: %errorlevel%
  118. echo Please check:
  119. echo 1. File exists on remote server
  120. echo 2. Username is correct
  121. echo 3. Network connection
  122. echo.
  123. echo Run 'getlog setup' to reconfigure username.
  124. exit /b 1
  125. )
  126. echo.
  127. echo File downloaded successfully to: %DEST%
  128. echo Full path: %DEST%\%FILE%
  129. )
  130. goto :eof
  131. :setup
  132. echo Current username: %USERNAME%
  133. echo.
  134. set /p "NEW_USER=Enter new username: "
  135. if "%NEW_USER%"=="" (
  136. echo Username cannot be empty.
  137. goto :setup
  138. )
  139. echo %NEW_USER%>"%CONFIG_FILE%"
  140. echo Username saved successfully.
  141. goto :eof
  142. :foldersetup
  143. echo.
  144. echo Current folder structure: %FOLDER_MODE%
  145. echo.
  146. echo Available options:
  147. echo 1. SJLNT (new) - %USERPROFILE%\Downloads\SJLNT\<caseno>
  148. echo 2. OLD (legacy) - %USERPROFILE%\Downloads\Traces n Logs\<caseno>
  149. echo.
  150. set /p "FOLDER_CHOICE=Enter folder structure (1 for SJLNT, 2 for OLD): "
  151. if "%FOLDER_CHOICE%"=="1" (
  152. echo SJLNT>"%FOLDER_CONFIG%"
  153. echo Folder structure set to SJLNT (new).
  154. ) else if "%FOLDER_CHOICE%"=="2" (
  155. echo OLD>"%FOLDER_CONFIG%"
  156. echo Folder structure set to OLD (legacy).
  157. ) else (
  158. echo Invalid choice. Using default (SJLNT).
  159. echo SJLNT>"%FOLDER_CONFIG%"
  160. )
  161. echo.
  162. echo Run: getlog ^<caseno^> [filename] to download files.
  163. goto :eof
  164. :install
  165. echo Installing getlog for global access...
  166. echo ========================================
  167. echo.
  168. REM Check if already in PATH
  169. echo %PATH% | find /i "%SCRIPT_DIR%" >nul
  170. if not errorlevel 1 (
  171. echo Script is already in PATH.
  172. echo Location: %SCRIPT_DIR%
  173. goto :check_admin
  174. )
  175. REM Check for administrator privileges
  176. net session >nul 2>&1
  177. if %errorlevel% neq 0 (
  178. echo Warning: Administrative privileges required for system-wide installation.
  179. echo You can still install for current user only.
  180. echo.
  181. choice /c SU /m "Install for: [S]ystem (admin required) or [U]ser"
  182. if errorlevel 2 goto :user_install
  183. goto :system_install
  184. )
  185. :system_install
  186. echo Installing for all users (requires admin)...
  187. echo.
  188. REM Add to system PATH using setx
  189. setx PATH "%PATH%;%SCRIPT_DIR%" /M >nul
  190. if errorlevel 1 (
  191. echo Failed to update system PATH. Trying user PATH...
  192. goto :user_install
  193. ) else (
  194. echo Successfully added to system PATH.
  195. echo Script directory: %SCRIPT_DIR%
  196. echo.
  197. echo You may need to restart your terminal for changes to take effect.
  198. )
  199. goto :eof
  200. :user_install
  201. echo Installing for current user only...
  202. echo.
  203. REM Add to user PATH
  204. setx PATH "%PATH%;%SCRIPT_DIR%" >nul
  205. if errorlevel 1 (
  206. echo Failed to update user PATH.
  207. echo.
  208. echo Manual installation steps:
  209. echo 1. Copy this script to a directory already in your PATH
  210. echo 2. Or add this directory to your PATH manually:
  211. echo %SCRIPT_DIR%
  212. ) else (
  213. echo Successfully added to user PATH.
  214. echo Script directory: %SCRIPT_DIR%
  215. echo.
  216. echo You may need to restart your terminal for changes to take effect.
  217. )
  218. goto :eof
  219. :check_admin
  220. echo.
  221. echo You can now run 'getlog' from any directory.
  222. echo.
  223. goto :eof
  224. :uninstall
  225. echo Uninstalling getlog...
  226. echo ======================
  227. echo.
  228. echo Warning: This will only remove the script directory from PATH.
  229. echo The script files and configuration will not be deleted.
  230. echo.
  231. echo Script location: %SCRIPT_DIR%
  232. echo.
  233. choice /c YN /m "Are you sure you want to remove getlog from PATH? [Y/N]"
  234. if errorlevel 2 (
  235. echo Uninstall cancelled.
  236. goto :eof
  237. )
  238. echo.
  239. echo Removing from PATH...
  240. REM Create a temporary PowerShell script to safely remove from PATH
  241. set "PS_SCRIPT=%TEMP%\remove_from_path.ps1"
  242. (
  243. echo $currentPath = [Environment]::GetEnvironmentVariable('PATH', 'User')
  244. echo $systemPath = [Environment]::GetEnvironmentVariable('PATH', 'Machine')
  245. echo $scriptDir = "%SCRIPT_DIR:\=/%"
  246. echo $newUserPath = ($currentPath -split ';' ^| Where-Object { $_ -ne "%SCRIPT_DIR%" } ^| Where-Object { $_ -ne "" }) -join ';'
  247. echo $newSystemPath = ($systemPath -split ';' ^| Where-Object { $_ -ne "%SCRIPT_DIR%" } ^| Where-Object { $_ -ne "" }) -join ';'
  248. echo [Environment]::SetEnvironmentVariable('PATH', $newUserPath, 'User')
  249. echo [Environment]::SetEnvironmentVariable('PATH', $newSystemPath, 'Machine')
  250. echo "PATH updated. You may need to restart your terminal."
  251. ) > "%PS_SCRIPT%"
  252. powershell -ExecutionPolicy Bypass -File "%PS_SCRIPT%"
  253. del "%PS_SCRIPT%" >nul 2>&1
  254. echo.
  255. echo getlog has been removed from PATH.
  256. echo Note: Script files and configuration remain in: %SCRIPT_DIR%
  257. goto :eof