PTH11108
Link : Up
FTP : OK
EJ111080318.ZIP R111080318.TXT T111080318.TXT : File Not Found
PTH11122
Link : Up
FTP : OK
EJ111220318.ZIP R111220318.TXT T111220318.TXT : File Not Found
PTH11128
Link : Up
FTP : OK
EJ111280318.ZIP R111280318.TXT T111280318.TXT : File Not Found
PTH11136
Link : Up
FTP : OK
EJ111360318.ZIP R111360318.TXT T111360318.TXT : File Not Found
PTH11138
Link : Up
FTP : OK
Upload File : Success
PTH11142
Link : Up
FTP : OK
EJ111420318.ZIP R111420318.TXT T111420318.TXT : File Not Found
PTH11144
Link : Up
FTP : OK
EJ111440318.ZIP R111440318.TXT T111440318.TXT : File Not Found
PTH11148
Link : Up
FTP : OK
EJ111480318.ZIP R111480318.TXT T111480318.TXT : File Not Found
PTH11190
Link : Up
FTP : OK
EJ111900318.ZIP R111900318.TXT T111900318.TXT : File Not Found
PTH11209
Link : Up
FTP : OK
Upload File : Success
Assistant
To accomplish your goal of extracting and storing the codes from the log file provided until you encounter a blank line or the dashed line (indicating the end), you can use a batch script. Below is a modified version of your provided script that reads the file, captures the codes from the specified positions, and stops when it reaches the end of a useful data section.
User
need it to short code for some thing like this
for /f ........ in (....) do call process %%i
goto end
Assistant
If you want to simplify the script to process each line directly within a loop, calling another label (or command) to handle the logic, you can adapt your batch script to something akin to the following. This version uses CALL to process each line immediately while keeping things concise.
Here’s how you can do it:
batch
User
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "usebackq delims=" %%i in ("REPAIR_LOG.txt") do (
call :processLine "%%i"
)
goto :eof
:processLine
set "line=%~1"
rem Check if line is empty or if it contains only dashes
if "%line%"=="" exit /b
if "%line:~0,4%"=="----" exit /b
rem Extract the code from the line (5 characters starting from index 0)
set "st=!line:~0,5!"
ping pth!st! -n 1 -w 1000 > null
if not error ( echo store !st! is Online ) else ( echo store !st! is Offline)
exit /b
endlocal
pause
Assistant
User
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "usebackq delims=" %%i in ("REPAIR_LOG.txt") do (
call :processLine "%%i"
)
goto :eof
:processLine
set "line=%~1"
rem Check if line is empty or if it contains only dashes
if "!line!"=="" exit /b
if "!line:~0,4!"=="----" exit /b
rem Extract the code from the line (5 characters starting from index 0)
set "st=!line:~0,5!"
rem Ping the host constructed from the code
ping pth!st! -n 1 -w 1000 > nul
if !errorlevel! == 0 (
call :download
)
exit /b
:download
ftp get .....
unzip ......
ftp put ....
goto next store
:end
endlocal
pause
Assistant
User
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "usebackq delims=" %%i in ("REPAIR_LOG.txt") do (
call :processLine "%%i"
)
goto :eof
:processLine
set "line=%~1"
rem Check if line is empty or if it contains only dashes
if "!line!"=="" exit /b
if "!line:~0,4!"=="----" exit /b
rem Extract the code from the line (5 characters starting from index 0)
set "st=!line:~0,5!"
rem Ping the host constructed from the code
ping pth!st! -n 1 -w 1000 > nul
if !errorlevel! == 0 (
call :download !st!
)
PTH11231
Link : Up
FTP : OK
EJ112310318.ZIP R112310318.TXT T112310318.TXT : File Not Found
PTH17001
Link : Up
FTP : OK
EJ170010318.ZIP R170010318.TXT T170010318.TXT : File Not Found
PTH17008
Link : Up
FTP : OK
EJ170080318.ZIP R170080318.TXT T170080318.TXT : File Not Found
PTH15067
Link : Up
FTP : OK
EJ150670318.ZIP R150670318.TXT T150670318.TXT : File Not Found
PTH15068
Link : Up
FTP : OK
EJ150680318.ZIP R150680318.TXT T150680318.TXT : File Not Found
PTH21090
Link : Up
FTP : OK
EJ210900318.ZIP : File Not Found
PTH21260
Link : Up
FTP : OK
EJ212600318.ZIP R212600318.TXT T212600318.TXT : File Not Found
PTH21583
Link : Up
FTP : OK
EJ215830318.ZIP R215830318.TXT T215830318.TXT : File Not Found
PTH22283
Link : Down
PTH22400
Link : Down
PTH22640
Link : Up
FTP : OK
EJ226400318.ZIP R226400318.TXT T226400318.TXT : File Not Found
PTH22696
Link : Up
FTP : OK
R226960318.TXT T226960318.TXT : File Not Found
PTH31109
Link : Up
FTP : OK
EJ311090318.ZIP R311090318.TXT T311090318.TXT : File Not Found
PTH62013
Link : Up
FTP : OK
EJ620130318.ZIP : File Not Found
PTH62014
Link : Up
FTP : OK
EJ620140318.ZIP R620140318.TXT T620140318.TXT : File Not Found
PTH65008
Link : Up
FTP : OK
EJ650080318.ZIP : File Not Found
PTH65009
Link : Up
FTP : OK
EJ650090318.ZIP : File Not Found
PTH81002
Link : Up
FTP : OK
EJ810020318.ZIP : File Not Found
PTH82003
Link : Up
FTP : OK
EJ820030318.ZIP R820030318.TXT T820030318.TXT : File Not Found
PTH41003
Link : Up
FTP : OK
EJ410030318.ZIP R410030318.TXT T410030318.TXT : File Not Found
PTH41011
Link : Up
FTP : OK
EJ410110318.ZIP R410110318.TXT T410110318.TXT : File Not Found
PTH41016
Link : Up
FTP : OK
EJ410160318.ZIP R410160318.TXT T410160318.TXT : File Not Found
PTH41126
Link : Up
FTP : OK
EJ411260318.ZIP R411260318.TXT T411260318.TXT : File Not Found
PTH41156
Link : Up
FTP : OK
EJ411560318.ZIP R411560318.TXT T411560318.TXT : File Not Found
PTH41167
Link : Up
FTP : OK
EJ411670318.ZIP R411670318.TXT T411670318.TXT : File Not Found
PTH41181
Link : Up
FTP : OK
EJ411810318.ZIP : File Not Found
PTH41198
Link : Up
FTP : OK
EJ411980318.ZIP R411980318.TXT T411980318.TXT : File Not Found
i need store code from 5 digit of each line till found "" or ----- for end
REPAIR_LOG.txt
Here's how you can do it:
batch
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "delims=" %%i in (REPAIR_LOG.txt) do (
set "line=%%i"
rem Check if line is empty or if it contains only dashes
if "!line!"=="" (
goto :endLoop
)
if "!line:~0,4!"=="----" (
goto :endLoop
)
rem Extract the code from the line (5 characters starting from index 0)
set "code=!line:~0,5!"
rem Store the code
set codes=!codes! !code!
)
:endLoop
echo Extracted Codes: !codes!
endlocal
pause
Explanation:
Initialization: The script starts by initializing a variable codes to store all extracted codes.
Date Retrieval: It fetches the formatted date (MMDD) from SQL Server and displays it.
Loop Through the Log File:
Each line is read from the REPAIR_LOG.txt file.
If the line is empty or starts with dashes (indicating the end of relevant data), it jumps to the :endLoop label.
It extracts the first 5 characters of each line as the code and appends it to the codes variable.
Output: After processing, it echoes all extracted codes.
Pause: The script pauses to allow you to see the output before closing the command window.
Notes:
Make sure REPAIR_LOG.txt is in the same directory as your batch script or provide a full path to the file.
Modify the database connection details as necessary for your environment.
This script assumes that the codes will always be at the start of the lines as indicated and will capture those correctly. Adjustments may be needed if the format changes.
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "usebackq delims=" %%i in ("REPAIR_LOG.txt") do (
call :processLine "%%i"
)
goto :end
:processLine
set "line=%~1"
rem Check if line is empty or if it contains only dashes
if "%line%"=="" exit /b
if "%line:~0,4%"=="----" exit /b
rem Extract the code from the line (5 characters starting from index 0)
set "code=!line:~0,5!"
rem Store the code
set codes=!codes! !code!
exit /b
:end
echo Extracted Codes: !codes!
endlocal
pause
Breakdown of the Changes:
Functionality of CALL: The CALL command is used to invoke the :processLine label for each line read from REPAIR_LOG.txt.
Parameter Handling: The line is passed as a parameter into the :processLine subroutine.
Exiting the Subroutine: Inside the subroutine, the script checks for an empty line or a dashed line, exiting the subroutine if encountered.
Code Extraction: The first five characters are extracted, and the result is added to the codes variable.
End of Input Handling: After processing all lines, the script outputs all extracted codes.
Usage:
This script will read REPAIR_LOG.txt, extract the first five characters of each relevant line, and compile a list of them.
Be sure to adjust file names and paths as necessary for your environment.
This approach keeps your script concise, organized, and easier to manage by separating logic into distinct labels.
Your script appears to aim at processing lines from REPAIR_LOG.txt, extracting the initial five characters (which are presumably codes) from each line, and then checking if a host corresponding to each code is online using ping. Below are a few improvements to ensure the script works correctly and avoids any potential issues.
Revised Script
Here's the corrected version of your batch script:
batch
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "usebackq delims=" %%i in ("REPAIR_LOG.txt") do (
call :processLine "%%i"
)
goto :eof
:processLine
set "line=%~1"
rem Check if line is empty or if it contains only dashes
if "!line!"=="" exit /b
if "!line:~0,4!"=="----" exit /b
rem Extract the code from the line (5 characters starting from index 0)
set "st=!line:~0,5!"
rem Ping the host constructed from the code
ping pth!st! -n 1 -w 1000 > nul
if !errorlevel! == 0 (
echo store !st! is Online
) else (
echo store !st! is Offline
)
exit /b
:end
endlocal
pause
Key Changes and Corrections:
Error Level Check: The ping command uses %errorlevel%, so ensure your conditional checks are against !errorlevel! which reflects the result of the last command executed.
Output Redirection: > null was replaced with > nul (which is the correct implementation) to suppress the command output from being displayed on the console.
Consistent Use of ! and %: The script maintains consistent usage of delayed variable expansion (!var!) for properly accessing variables within the loop and subroutine.
No Need to Use goto :end: The script terminates with a goto :eof after processing, and the :end label is not necessary.
Unnecessary Local Storage of codes: If you're not using the codes variable later on, you may consider removing it, but I've kept it in there in case you plan to add functionality that uses it.
Usage:
Save this code as a .bat file.
Make sure REPAIR_LOG.txt is in the same directory as the script, or provide its full path.
Ensure you can successfully execute the sqlcmd command without errors related to authentication or connectivity to your SQL Server instance.
This script will now output whether each store associated with the extracted codes is online or offline based on the results of the ping command.
You seem to be crafting a batch script that pings a host constructed from a code extracted from a log file, and upon successful pinging, performs some FTP and unzip operations. Below, I've provided a more complete structure for your script, including additional explanation for the missing FTP and unzip commands, as well as proper handling of your workflow.
Revised Batch Script
batch
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "usebackq delims=" %%i in ("REPAIR_LOG.txt") do (
call :processLine "%%i"
)
goto :eof
:processLine
set "line=%~1"
rem Check if line is empty or if it contains only dashes
if "!line!"=="" exit /b
if "!line:~0,4!"=="----" exit /b
rem Extract the code from the line (5 characters starting from index 0)
set "st=!line:~0,5!"
rem Ping the host constructed from the code
ping pth!st! -n 1 -w 1000 > nul
if !errorlevel! == 0 (
call :download !st!
)
exit /b
:download
set "storeCode=%~1"
echo Downloading files for store: !storeCode!
ftp -s:ftp_commands.txt
unzip !storeCode!.zip -d !storeCode!
ftp -s:upload_commands.txt
goto :eof
:end
endlocal
pause
Important Notes and Instructions:
FTP Command Files:
Replace ftp -s:ftp_commands.txt and ftp -s:upload_commands.txt with actual FTP command files or commands appropriate for your environment.
For example, ftp_commands.txt could contain:
code
open yourftpserver.com
username
password
get yourfile.zip
bye
Similarly, upload_commands.txt would contain commands necessary to upload files back to the FTP server.
Unzipping the File:
The unzip command is used to unzip the files. Make sure you have the unzip utility installed in your system's PATH or replace it with a Windows-compatible unzip command.
Ensure that <yourfile.zip> matches your expected file format names if you wish to modify the script to provide dynamic filenames as needed.
Echoing for Debugging:
The line echo Downloading files for store: !storeCode! is added for clarity and debugging. It helps you track what the script is processing.
Maintaining Structure:
I removed the unnecessary goto next store line, as the flow returns naturally to the original loop through the :processLine call.
Error Checking:
You might want to add error checking to ensure that each stage (e.g., FTP get, unzip, FTP put) proceeds successfully or logs an appropriate error message.
How to Execute the Script:
Save this script as, for example, process.bat.
Ensure your REPAIR_LOG.txt and any required files are present.
Run the script in a command prompt window and monitor for output and errors.
This should give you a structured approach to automate your tasks. Adjust the FTP commands and unzip paths as per your requirements.
It looks like you're building a script to handle a combination of FTP operations, file downloads, and unzipping tasks based on codes extracted from REPAIR_LOG.txt. However, there are a few adjustments and clarifications needed to ensure that this batch file works as intended. I will provide a revised version of your script along with explanations.
Revised Batch Script
batch
@echo off
setlocal enabledelayedexpansion
rem Initialize variables
set codes=
rem Read the date information
for /f "delims= " %%i in ('sqlcmd -S .\SQLEXPRESS -U sa -P password -Q "SET NOCOUNT ON; SELECT FORMAT(GETDATE()-1, 'MMdd');" -h-1') do (
set MMDD=%%i
echo Date: !MMDD!
)
rem Process the REPAIR_LOG.txt file to extract codes
for /f "usebackq delims=" %%i in ("REPAIR_LOG.txt") do (
call :processLine "%%i"
)
goto :eof
:processLine
set "line=%~1"
rem Check if line is empty or if it contains only dashes
if "!line!"=="" exit /b
if "!line:~0,4!"=="----" exit /b
rem Extract the code from the line (5 characters starting from index 0)
set "st=!line:~0,5!"
rem Ping the host constructed from the code
ping pth!st! -n 1 -w 1000 > nul
if !errorlevel! == 0 (
call :download !st!
)
exit /b
:download
set "storeCode=%~1"
echo Downloading files for store: !storeCode!
set "fileToDownload=file!storeCode!_!MMDD!.txt"
set "zipFile=!storeCode!.zip"
rem Download the file using FTP
ftp -s:ftp_commands.txt > nul
rem Unzip the downloaded file
if exist "!zipFile!" (
unzip "!zipFile!" -d "!storeCode!" "!fileToDownload!"
) else (
echo "Zip file not found: !zipFile!"
)
rem Upload the file back to FTP
ftp -s:upload_commands.txt > nul
goto :eof
:end
endlocal
pause
Key Changes and Explanations
Set MMDD Globally: Ensure that your MMDD variable is correctly referred to in the :download section. The extraction needs to be consistent, so place it in variables clearly.
Dynamic File Names: The script now correctly constructs the filename for downloading and unzipping.
In set "fileToDownload=file!storeCode!_!MMDD!.txt", this will dynamically create the filename based on the store code and the date.
FTP Command Redirection:
Using > nul to suppress output from the FTP commands. Make sure your ftp_commands.txt includes the relevant commands needed to connect, authenticate, and download the file. Valid FTP command example:
code
open ftp.yourserver.com
user your_username
your_password
get file!storeCode!_!MMDD!.txt
bye
Check for Existence of Zip File: Before attempting to unzip, the script checks that the zip file actually exists using if exist "!zipFile!".
Feedback on Missing Files: If the zip file does not exist, an appropriate message will be echoed to help with debugging.
Error Handling: Using checks helps prevent crashes or unintended behavior if certain files are not found.
Testing and Execution
Make sure all paths are correct, and necessary tools like FTP and Unzip are available in your environment.
Ensure permissions are appropriate for file operations and network commands.
Execute the script in a command prompt to see the output and ensure everything behaves as expected.
With these adjustments, your script should successfully handle downloading, unzipping, and uploading files based on the store codes extracted from the log.