(
user hotrn
hotrn
cd Aldata/LIFE/HEAD/SAV/DOWN/POS
get !brandFile! d:\FTPRoot\TransfIN!brandFile!
get !departmentFile! d:\FTPRoot\TransfIN!departmentFile!
get !productFile! d:\FTPRoot\TransfIN!productFile!
bye
) > ftp_wprx.dat
make bat file cmd to create ftp_wprx.dat
Assistant
To create a batch file that generates the ftp_wprx.dat file as specified, you can follow these steps. Below is an example of how you can structure the batch file to create ftp_wprx.dat with the desired FTP commands.
Open Notepad (or any text editor).
Copy the following code into it:
@echo off
(
echo user hotrn
echo hotrn
echo cd Aldata/LIFE/HEAD/SAV/DOWN/POS
echo get !brandFile! d:\FTPRoot\TransfIN\!brandFile!
echo get !departmentFile! d:\FTPRoot\TransfIN\!departmentFile!
echo get !productFile! d:\FTPRoot\TransfIN\!productFile!
echo bye
) > ftp_wprx.dat
echo ftp_wprx.dat has been created.
pause
Save the file with a .bat extension, for example, create_ftp_wprx.bat.
User
sql command trim space D:\temp_bat>osql -U TRANSF -PFSNART -SPTH63003 -Q "set nocount on;select @@servername" -h-1
Assistant
To trim spaces in SQL queries using T-SQL commands, you can use the LTRIM and RTRIM functions, or the TRIM function if you are using SQL Server 2017 or later versions. Let's see how to apply this in your osql command.
Yes, you can convert the stcode column to a VARCHAR during your SQL query. In SQL Server, you can do this using the CAST() or CONVERT() functions. Here’s how you can modify your osql command to include the conversion:
Using CAST
You can use the CAST() function to convert stcode to VARCHAR. For example:
osql -U TRANSF -P FSNART -S PTH63003 -Q "set nocount on; select CAST(stcode AS VARCHAR(255)) as StCodeVarchar from estore..pospara" -h-1
Using CONVERT
Alternatively, you can use the CONVERT() function:
osql -U TRANSF -P FSNART -S PTH63003 -Q "set nocount on; select CONVERT(VARCHAR(255), stcode) as StCodeVarchar from estore..pospara" -h-1
Notes:
Replace 255 with the desired length for your VARCHAR type based on your needs.
The alias as StCodeVarchar makes the output more readable by giving the resulting column a name.
If you want to extract only the last part of the server name (in your case, 63003) and store it in a variable within a batch script, you can utilize string manipulation techniques available in batch scripting after executing the SQL query.
User
cmd bat command to get mm=!date:~5,2! หมายความว่า
Assistant
The line mm=!date:~5,2! is a command used in a Windows batch file to extract a substring from the system date and assign it to a variable named mm. Let's break down what this command means:
Explanation
!date!: This refers to the environment variable date, which typically contains the current date in the format defined by the system locale. For example, a common date format might be , , or .
User
ถ้า set=!st:!~2,5! จากค่า st=" 63003 "
Assistant
คำสั่ง set=!st:~2,5! ใน Windows Batch script หมายถึงการนำสตริงที่ถูกกำหนดในตัวแปร st มาตัดโดยเริ่มต้นที่ตำแหน่งที่ 2 และตัดความยาว 5 ตัวอักษรออกมา
::Get Store Code
osql -UTRANSF -PFSNART -SPTH63003 -Q "set nocount on; select stcode from estore..pospara" -h-1' > stcode.txt
set /p st_id=<stcode.txt
set st_id=!st_id:~1,5!
for /f "delims=" %%a in ("!st_id!") do set "st_id=%%a"
@echo off
setlocal enabledelayedexpansion
:: Get Store Code
osql -U TRANSF -P FSNART -S PTH63003 -Q "set nocount on; select stcode from estore..pospara" -h-1 > stcode.txt
:: Read the first line from stcode.txt into st_id
set /p st_id=<stcode.txt
:: Trim leading spaces
for /f "tokens=* delims= " %%a in ("!st_id!") do set "st_id=%%a"
:: If you need to also trim trailing spaces:
for /f "delims=" %%a in ("!st_id!") do set "st_id=%%a"
:: Display the result
echo Store ID: !st_id!
endlocal
User
@echo off
setlocal EnableDelayedExpansion
::Get Yesterday
osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "set nocount on; select convert(varchar(10), getdate(), 120)" -h-1 -w150 > date.dat
set /p day=<date.dat
set mm=!day:~6,2!
set dd=!day:~9,2!
del /q date.dat > nul
::Get Store Code
osql -UTRANSF -PFSNART -SPTH63003 -Q "set nocount on; select stcode from estore..pospara" -h-1' > stcode.txt
set /p st_id=<stcode.txt
::set st_id=!st_id:~1,5!
for /f "tokens=* delims= " %%a in ("!st_id!") do set "st_id=%%a"
for /f "delims=" %%a in ("!st_id!") do set "st_id=%%a"
::del /q stcode.dat > nul
set "brandFile=BRAND_!mm!!dd!.!st_id!"
set "departmentFile=DEPARTMENT_!mm!!dd!.!st_id!"
set "productFile=PRODUCT_!mm!!dd!.!st_id!"
echo !brandFile!
echo !departmentFile!
echo !productFile!
::FTP WPRX
(
echo user hotrn
echo hotrn
echo cd ..
echo cd..
echo cd Aldata/LIFE/HEAD/SAV/DOWN/POS
echo get !brandFile!
echo get !departmentFile!
echo get !productFile!
echo bye
) > ftp_wprx.dat
ftp -n -s:ftp_wprx.dat 10.4.9.128
del /q ftp_wprx.dat > nul
Run the batch file by double-clicking on it, and it will create ftp_wprx.dat with the specified FTP commands.
Important Notes:
Make sure that the bat file is in the correct directory where you want to create the ftp_wprx.dat file or navigate to that directory before running the script.
The commands !brandFile!, !departmentFile!, and !productFile! are assumed to be set or defined elsewhere when the FTP process is run, as the ! notation suggests they're variables. You may need to ensure these variables are defined depending on your environment.
This script will echo each line of the FTP commands into a new file called ftp_wprx.dat, and then it will pause to indicate that the process is complete and allow you to see the message.
Trimming spaces from the server name:
If you are using SQL Server 2017 or later, you can use the TRIM function:
These modifications will correctly return the server name stripped of any extra space.
The set nocount on; statement remains useful to suppress row count messages, making the output cleaner.
These commands will effectively convert the stcode values to VARCHAR before returning them in your query results.
However, SQL Server doesn’t allow you to directly assign values to batch variables within the SQL query itself. Here’s how you can achieve the desired outcome by capturing the output from your osql command and then processing it in a batch file.
Here’s a complete method on how to do this:
1. Modify Your OSQL Command
You’ll want to run the osql command to get the server name and then manipulate that output in the batch script. You can modify your command as follows to simply fetch the server name.
2. Store and Process the Output in a Batch File
Here’s how you can write a batch file to do this:
@echo off
setlocal
:: Get the server name and store in a variable
for /f "delims=" %%a in ('osql -U TRANSF -P FSNART -S PTH63003 -Q "set nocount on; select RTRIM(@@servername)" -h-1') do (
set "servername=%%a"
)
:: Extract the last 5 characters (the part after "pthx")
set "last_part=!servername:~-5!"
:: To display
echo Extracted part: !last_part!
:: Or if you want to export to a variable (for other use)
set "SERVERID=!last_part!"
:: Done!
endlocal
pause
Explanation of Steps:
for /f "delims=" %%a in (...): This command captures the output of the osql command and stores each line into the variable %%a. The delims= option ensures that the entire line is captured, even if it contains spaces.
set "servername=%%a": This assigns the captured output to the variable servername.
set "last_part=!servername:~-5!": This uses substring manipulation to take the last 5 characters of the servername. You can adjust the number 5 if your server names vary in length.
echo Extracted part: !last_part!: This line is just for output to confirm the result. You could remove this in production.
set "SERVERID=!last_part!": This can be used to save or process the extracted value further.
Important Note:
Make sure to run your batch file in an environment that has access to the osql utility and that the SQL Server credentials and connection details are correct. Adjust any SQL commands or server details as needed based on your actual setup.
MM/DD/YYYY
DD/MM/YYYY
YYYY-MM-DD
!date:~5,2!: This syntax is used to extract a substring from the date string:
~5: This specifies the starting index for the substring, which means it starts extracting from the 6th character (since the index starts from 0).
,2: This specifies the number of characters to extract, which in this case is 2 characters.
**mm=: This creates a variable named mm and assigns it the extracted substring.
Example
Assuming your system date is formatted as MM/DD/YYYY (for example, 03/15/2024):
The date variable would hold the value 03/15/2024.
!date:~5,2! would extract 15 (the 6th and 7th characters in the string, which in this case are the day).
If your system date is in a different format, the extraction will yield different results:
For example:
If the date is 2024-03-15, then !date:~5,2! will return 03.
Using in a Batch File
In a batch file, you would need to use setlocal enabledelayedexpansion to make use of delayed variable expansion with the exclamation marks. Here is an example of how you might use this in a batch script:
@echo off
setlocal enabledelayedexpansion
:: Get the current date
set date=%date%
:: Extract the month
set mm=!date:~5,2!
:: Display the month
echo The month is: !mm!
endlocal
Conclusion
In summary, the command mm=!date:~5,2! assigns the month (from the current date) to the variable mm. The specific output would depend on the date format your system is configured to use.
ด้วยตัวแปรที่กำหนดอย่างนี้ ค่า set จะมีค่าที่ถูกตั้งเป็น 63003 หากคุณใช้คำสั่งนี้ใน BAT file ค่าที่ถูกตัดออกมาและตั้งค่าให้กับตัวแปรใหม่จะไม่มีช่องว่างนำหน้าและตามหลังครับ ทำให้มันสะอาดและพร้อมใช้งานในส่วนอื่น ๆ ของสคริปต์
63003
กรณีที่มีช่องว่างต่อท้าย
ถึงแม้ว่าในสตริง st จะมีช่องว่างต่อท้ายจำนวนมากหลังจาก 63003, แต่ค่า mm จะยังคงถูกตั้งตรงตามที่อธิบายไว้ ซึ่งจะไม่รวมช่องว่างเมื่อเรียกดูจากการตัดตามตำแหน่งที่กำหนด:
ตัวแปร mm จะเป็น 63003
เสนอวิธีเพิ่มประสิทธิภาพการจัดการค่าช่องว่าง
อย่างไรก็ตาม หากคุณต้องการให้แน่ใจว่าค่าที่ได้ไม่มีช่องว่างนำหน้าหรือช่องว่างตามหลัง คุณสามารถใช้คำสั่ง SET ร่วมกับ CALL เพื่อให้มั่นใจว่าได้ค่าที่ไม่มีช่องว่าง:
@echo off
setlocal enabledelayedexpansion
:: กำหนดค่าให้กับ st
set st= " 63003
"
:: ตัดค่าเพื่อเอา 5 ตัวอักษรจากตำแหน่งที่ 2
set mm=!st:~2,5!
:: ใช้ CALL เพื่อดูค่าของ mm โดยตัดช่องว่าง
for /f "delims=" %%a in ("!mm!") do set "mm=%%a"
:: แสดงผลลัพธ์
echo ค่า mm ที่ได้: !mm!
endlocal
pause
คำอธิบาย:
for /f "delims=" %%a in ("!mm!") do set "mm=%%a" จะใช้การวนซ้ำเพื่อจัดการกับช่องว่างของตัวแปร mm โดยการนำไปใส่ใน %%a และทำการตั้งค่ามันใหม่เพื่อให้ไม่มีช่องว่าง
โค้ดนี้จะแสดงผลลัพธ์ของ mm ซึ่งจะทำให้มั่นใจได้ว่าค่านั้นไม่มีช่องว่างเพิ่มเติม!
คุณสามารถใช้วิธีการกำจัดช่องว่างนำหน้าและช่องว่างตามหลังในตัวแปรด้วย for loop เพื่อให้แน่ใจว่าค่าที่ได้ในตัวแปร st_id ไม่มีช่องว่างหรือตัวอักษรที่ไม่ต้องการเก็บอยู่
นี่คือการปรับปรุงสคริปต์ของคุณ:
@echo off
setlocal EnableDelayedExpansion
:: Get Yesterday
osql -U TRANSF -P FSNART -S bgcpitsd-mn01 -d master -Q "set nocount on; select convert(varchar(10), getdate(), 120)" -h-1 -w150 > date.dat
set /p day=<date.dat
set mm=!day:~6,2!
set dd=!day:~9,2!
del /q date.dat > nul
:: Get Store Code
osql -U TRANSF -P FSNART -S PTH63003 -Q "set nocount on; select stcode from estore..pospara" -h-1 > stcode.txt
set /p st_id=<stcode.txt
:: Trim leading and trailing spaces from st_id
for /f "tokens=* delims= " %%a in ("!st_id!") do set "st_id=%%a"
for /f "delims=" %%a in ("!st_id!") do set "st_id=%%a"
del /q stcode.txt > nul
:: Construct filenames
set "brandFile=BRAND_!mm!!dd!.!st_id!"
set "departmentFile=DEPARTMENT_!mm!!dd!.!st_id!"
set "productFile=PRODUCT_!mm!!dd!.!st_id!"
:: Debugging output
echo !brandFile!
echo !departmentFile!
echo !productFile!
:: FTP WPRX
(
echo user hotrn
echo hotrn
echo cd ..
echo cd..
echo cd Aldata/LIFE/HEAD/SAV/DOWN/POS
echo get "!brandFile!"
echo get "!departmentFile!"
echo get "!productFile!"
echo bye
) > ftp_wprx.dat
:: Execute FTP
ftp -n -s:ftp_wprx.dat 10.4.9.128
del /q ftp_wprx.dat > nul
endlocal
การปรับแก้ที่สำคัญ:
การตัดช่องว่าง:
ตอนนี้ใช้ for /f "tokens=* delims= " %%a in ("!st_id!") do set "st_id=%%a" ซึ่งจะช่วยตัดช่องว่างที่นำหน้าออกไป
ใช้ for /f "delims=" %%a in ("!st_id!") do set "st_id=%%a" เพื่อตัดค่าได้นำมาใส่ให้แน่ใจว่าก็ไม่มีช่องว่างข้างหลัง
ใช้ Double Quotes:
ในไฟล์ FTP คำสั่ง get ใช้เครื่องหมาย "!brandFile!", "!departmentFile!", และ "!productFile!" เพื่อจัดการชื่อไฟล์ที่มีช่องว่างในชื่อได้อย่างถูกต้อง
หากยังมีปัญหาในการดาวน์โหลด ตรวจสอบว่าไฟล์ที่ตั้งชื่ออยู่จริง ๆ มีอยู่บนเซิร์ฟเวอร์ FTP ที่คุณพยายามดาวน์โหลดจาก मेरिऍ की אסרד侠
If you're looking for alternative methods to handle the leading and trailing whitespace in your st_id variable without using FOR loops, there are a few approaches you can take. Let's explore some different strategies:
1. Using PowerShell for Trimming Whitespace
You can invoke PowerShell from the batch script to trim whitespace. Here's how to integrate it into your existing code:
@echo off
setlocal EnableDelayedExpansion
:: Get Yesterday
osql -U TRANSF -P FSNART -S bgcpitsd-mn01 -d master -Q "set nocount on; select convert(varchar(10), getdate(), 120)" -h-1 -w150 > date.dat
set /p day=<date.dat
set mm=!day:~6,2!
set dd=!day:~9,2!
del /q date.dat > nul
:: Get Store Code
osql -U TRANSF -P FSNART -S PTH63003 -Q "set nocount on; select stcode from estore..pospara" -h-1 > stcode.txt
set /p st_id=<stcode.txt
:: Use PowerShell to trim whitespace
for /f %%a in ('powershell -command "[Console]::OutputEncoding = [Text.Encoding]::UTF8; (Get-Content stcode.txt | ForEach-Object { $_.Trim() })"') do (
set "st_id=%%a"
)
del /q stcode.txt > nul
:: Construct filenames
set "brandFile=BRAND_!mm!!dd!.!st_id!"
set "departmentFile=DEPARTMENT_!mm!!dd!.!st_id!"
set "productFile=PRODUCT_!mm!!dd!.!st_id!"
:: Debugging output
echo !brandFile!
echo !departmentFile!
echo !productFile!
:: FTP WPRX
(
echo user hotrn
echo hotrn
echo cd ..
echo cd..
echo cd Aldata/LIFE/HEAD/SAV/DOWN/POS
echo get "!brandFile!"
echo get "!departmentFile!"
echo get "!productFile!"
echo bye
) > ftp_wprx.dat
:: Execute FTP
ftp -n -s:ftp_wprx.dat 10.4.9.128
del /q ftp_wprx.dat > nul
endlocal
Key Points
In this script, PowerShell is used to read the contents of the stcode.txt, trim any whitespace, and output the cleaned value back to the batch variable st_id.
2. Storing Output Directly in a Variable
Alternatively, another method is to read the output directly into the variable without temporarily saving it, ensuring you trim non-desired characters. This avoids having to create files unnecessarily:
@echo off
setlocal EnableDelayedExpansion
:: Get Yesterday
osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "set nocount on; select convert(varchar(10), getdate(), 120)" -h-1 -w150 > date.dat
set /p day=<date.dat
set mm=!day:~6,2!
set dd=!day:~9,2!
del /q date.dat > nul
:: Get Store Code directly into st_id variable trimming whitespace
for /f "tokens=* delims=" %%a in ('osql -UTRANSF -PFSNART -SPTH63003 -Q "set nocount on; select stcode from estore..pospara" -h-1') do (
set "st_id=%%a"
)
:: Construct filenames
set "brandFile=BRAND_!mm!!dd!.!st_id!"
set "departmentFile=DEPARTMENT_!mm!!dd!.!st_id!"
set "productFile=PRODUCT_!mm!!dd!.!st_id!"
:: Debugging output
echo !brandFile!
echo !departmentFile!
echo !productFile!
:: FTP WPRX
(
echo user hotrn
echo hotrn
echo cd ..
echo cd..
echo cd Aldata/LIFE/HEAD/SAV/DOWN/POS
echo get "!brandFile!"
echo get "!departmentFile!"
echo get "!productFile!"
echo bye
) > ftp_wprx.dat
:: Execute FTP
ftp -n -s:ftp_wprx.dat 10.4.9.128
del /q ftp_wprx.dat > nul
endlocal
Key Points
This approach uses FOR /F directly while fetching the stcode from the database without creating intermediate files, thereby ensuring you have direct control over the output.
It trims whitespace effectively and assigns it to st_id.
Additional Considerations
While the above methods efficiently trim spaces, there's always a consideration for maintaining code readability and maintainability. Integrating PowerShell can be powerful, but using native batch commands keeps things simple if your environment supports it.
Adapt the method that best fits your needs and context. If there are specific limitations or preconditions in your environment, please let me know, and I can provide guidance accordingly!
Yes, DOS (Disk Operating System) and its successor, the command prompt (CMD) in Windows, can operate a wide range of tasks on a PC, such as file manipulation, system management, and networking. While it may not have the extensive graphical capabilities found in modern operating systems, DOS and the command prompt provide powerful command-line interfaces (CLIs) that can be leveraged for various functions. Below are key capabilities and functionalities of DOS/Command Prompt:
Key Features of DOS/Command Prompt
File Management:
Create, copy, move, delete, and rename files and directories using commands like COPY, MOVE, DEL, REN, MKDIR, etc.
View the contents of files with commands like TYPE or MORE.
System Configuration:
Use commands to configure system settings and manage system resources, such as FORMAT (to format disks), CHKDSK (to check for disk errors), and ATTRIB (to change file attributes).
Batch Scripting:
Create batch files (scripts) to automate repetitive tasks. Batch files can contain a series of DOS commands to run commands in sequence.
Network Operations:
Manage network connections and troubleshoot with commands like PING, TRACERT, IPCONFIG, and NET.
Disk Operations:
Utilize commands like DISKCOPY, BACKUP, and RESTORE to manage disk and backup operations.
System Utilities:
Access various system utilities directly from the command line. This includes tools for managing tasks, such as TASKLIST and TASKKILL.
Environment Variables:
Set and manipulate environment variables, which can affect the behavior of running processes using commands like SET, ECHO, and SETX.
Accessing Other Drives:
Change to different drives (e.g., D:) and navigate directories across the filesystem with CD.
Using Help System:
Use the HELP command or command /? (replace "command" with the actual command name) to get information about the syntax and options for DOS commands.
Limitations
While DOS/Command Prompt offers various functionalities, it also has limitations:
Lack of GUI: There is no graphical user interface in traditional DOS; users must input commands by typing.
Limited by Operating System: Modern systems primarily use graphical interfaces, and certain advanced features may require a GUI or scripting languages like PowerShell.
Legacy Commands: Some DOS commands may be considered outdated or less effective in modern contexts compared to newer tools.
Example Command Usage
Here's a few examples of basic DOS commands:
List directory contents:
dir
Change directory:
cd path\to\directory
Copy files:
copy source.txt destination.txt
Ping a network host:
ping www.example.com
Operating Systems with DOS Capabilities
While DOS itself is not commonly used in modern systems, the command prompt in Windows (CMD) serves many similar functions. Additionally, systems like FreeDOS provide a free implementation of DOS for various applications.
Conclusion
DOS and command prompt functionality provide powerful tools for operating systems. By leveraging these capabilities, users can efficiently interact with the operating system, automate tasks, and manage system settings. The skills learned in working with the command line are valuable, especially in environments where graphical interfaces may not be available or practical. If you have any specific tasks you'd like to explore using command-line operations, feel free to ask!