China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-25 12:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Grabbing file content, a bit complicated View 3,178 Replies 26
Original Poster Posted 2010-10-25 10:00 ·  中国 上海 移动
新手上路
Credits 12
Posts 12
Joined 2010-10-21 14:34
15-year member
UID 176258
Gender Male
Status Offline
The text you provided is in Chinese and the request involves writing a script to handle date range and file naming. Here is a Python example to achieve the functionality you described (assuming you are using Python):

```python
import datetime

def get_week_range(start_date_str):
start_date = datetime.datetime.strptime(start_date_str, '%Y-%m-%d'
week_start = start_date - datetime.timedelta(days=start_date.weekday())
week_end = week_start + datetime.timedelta(days=6)
return week_start.strftime('%Y-%m-%d', week_end.strftime('%Y-%m-%d'

def filter_and_save(input_file, output_file):
with open(input_file, 'r' as f_in, open(output_file, 'w' as f_out:
for line in f_in:
date_str = line.split()[0]
date = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S'
if week_start <= date.date() <= week_end:
f_out.write(line)

if __name__ == '__main__':
start_date_str = input("Please enter the start date (YYYY-MM-DD): "
week_start, week_end = get_week_range(start_date_str)
input_file_path = 'your_input_file_path.txt' # Replace with the actual path of your input file
output_file_name = datetime.datetime.now().strftime('%Y%m%d' + '.txt'
filter_and_save(input_file_path, output_file_name)
```

You need to replace `'your_input_file_path.txt'` with the actual path to your input text file. This code first determines the start and end dates of the week based on the input start date, then filters the lines in the input file that fall within that week range and saves them to a new file named with the current date.

Please note that you may need to adjust the code according to your actual running environment and specific file path situations.

The above is the Python code implementation. If you want to implement it in other programming languages, the general idea is similar, but the specific syntax will be different. For example, in Java:

```java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class WeekFilter {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd";
String startDateStr = "2010-10-18"; // You can change this to get input from user
Date startDate = sdf.parse(startDateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 1);
Date weekStart = cal.getTime();
cal.add(Calendar.DATE, 6);
Date weekEnd = cal.getTime();

String inputFilePath = "your_input_file_path.txt"; // Replace with actual input file path
String outputFileName = new SimpleDateFormat("yyyyMMdd".format(new Date()) + ".txt";
try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath));
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFileName))) {
String line;
while ((line = br.readLine()) != null) {
String dateStr = line.split(" "[0];
Date currentDate = sdf.parse(dateStr.split(" "[0]);
if (!currentDate.before(weekStart) &&!currentDate.after(weekEnd)) {
bw.write(line);
bw.newLine();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```

Again, replace `your_input_file_path.txt` with the actual path to your input text file. This Java code also first determines the start and end dates of the week, then filters the relevant lines and saves them to a new file named with the current date.
Floor 2 Posted 2010-10-25 14:32 ·  中国 广东 深圳 南山区 电信
新手上路
Credits 18
Posts 16
Joined 2010-01-06 15:16
16-year member
UID 158100
Gender Male
Status Offline
This is very simple!
Floor 3 Posted 2010-10-25 15:13 ·  中国 上海 移动
新手上路
Credits 12
Posts 12
Joined 2010-10-21 14:34
15-year member
UID 176258
Gender Male
Status Offline
Then how to write that, I basically don't understand this.
Floor 4 Posted 2010-10-25 16:22 ·  中国 山东 泰安 联通
新手上路
Credits 1
Posts 1
Joined 2010-10-24 14:31
15-year member
UID 176450
Gender Male
Status Offline
Not quite understand, help you push it up
Floor 5 Posted 2010-10-25 18:31 ·  中国 广东 深圳 南山区 电信
新手上路
Credits 18
Posts 16
Joined 2010-01-06 15:16
16-year member
UID 158100
Gender Male
Status Offline
Are the date formats fixed? Is it the format of 2010-02-01 or 2010-2-1?
Floor 6 Posted 2010-10-25 19:25 ·  中国 广东 深圳 南山区 电信
新手上路
Credits 18
Posts 16
Joined 2010-01-06 15:16
16-year member
UID 158100
Gender Male
Status Offline
Is there only one week in the file? If there are many weeks, what do you want to get? Only the content of one week? Or output by week. Please make it clear!
Floor 7 Posted 2010-10-25 19:27 ·  中国 广东 深圳 南山区 电信
新手上路
Credits 18
Posts 16
Joined 2010-01-06 15:16
16-year member
UID 158100
Gender Male
Status Offline
echo Start counting: >c:\%date:~0,10%.txt
for /f "tokens=1,2,3" %%c in (c:\123.txt) do (
if %%c GEQ 2010-10-24 if %%c LSS 2010-10-25 (echo %%c %%d %%e >>c:\%date:~0,10%.txt))
Floor 8 Posted 2010-10-26 09:23 ·  中国 上海 移动
新手上路
Credits 12
Posts 12
Joined 2010-10-21 14:34
15-year member
UID 176258
Gender Male
Status Offline
Originally posted by clinttt at 2010-10-25 19:25:
Is there only one week in the file? If there are many weeks, what do you want to get? Only the content of one week? Or output by week. Please make it clear!



This file is just an example. In reality, all date data is in it. The output is determined by the dates in the file. It grabs data from Monday to Sunday once a week. For example, the first time it grabs from October 18, 2010 to 24th, the next run grabs from October 25, 2010 to 31st, and so on. (Grabbing once a week, this has nothing to do with the script. I can define it in the task scheduler.) The date format is random, it can be 2010-02-01 or 2010-2-1.

[ Last edited by acmilanxr on 2010-10-26 at 10:18 ]
Floor 9 Posted 2010-10-26 10:23 ·  中国 上海 移动
新手上路
Credits 12
Posts 12
Joined 2010-10-21 14:34
15-year member
UID 176258
Gender Male
Status Offline
Originally posted by clinttt at 2010-10-25 19:27:
echo Now start statistics: >c:\%date:~0,10%.txt
for /f "tokens=1,2,3" %%c in (c:\123.txt) do (
if %%c GEQ %start_date% if %%c LSS %end_date% (echo %%c %%d %%e >>c:\%date:~ ...


This one needs to be modified further. According to the content, determine to capture the content from Monday to Sunday, and also, when I run your script, the output file is not generated...
Floor 10 Posted 2010-10-26 14:05 ·  中国 广东 深圳 南山区 电信
新手上路
Credits 18
Posts 16
Joined 2010-01-06 15:16
16-year member
UID 158100
Gender Male
Status Offline
It didn't generate because you didn't prepare the file according to the script, and you need to know the path of the input file.
Floor 11 Posted 2010-10-26 14:09 ·  中国 上海 移动
新手上路
Credits 12
Posts 12
Joined 2010-10-21 14:34
15-year member
UID 176258
Gender Male
Status Offline
Originally posted by clinttt at 2010-10-26 14:05:
It didn't generate because you didn't prepare the files according to the script and need to know the path of the input file.



Now it's okay, but how to write about judging the date from the file content? For example, October 18, 2010 is Monday, the 24th is Sunday, and the 25th is Monday again. How to judge?
Floor 12 Posted 2010-10-26 14:51 ·  中国 广东 深圳 南山区 电信
新手上路
Credits 18
Posts 16
Joined 2010-01-06 15:16
16-year member
UID 158100
Gender Male
Status Offline
@Echo Off
set date0=2017-07-26

set /a year1=%date0:~0,4%%%4
set /a month1=%date0:~5,2%-1
set /a month2=%date0:~5,2%+1
set /a reye=(%date0:~0,4%-1970+1)/4+365*(%date0:~0,4%-1970)
if %date0:~5,2% LEQ 2 (set /a remo=%date0:~5,2%/2+30*%month1%) else (
if %date0:~5,2% LEQ 8 (if %year1%==0 (set /a remo=%date0:~5,2%/2+30*%month1%-1) else (
set /a remo=%date0:~5,2%/2+30*%month1%-2)) else (
if %year1%==0 (set /a remo=%month2%/2+30*%month1%-1) else (
set /a remo=%month2%/2+30*%month1%-2)))
set year1=
set month1=
set month2=
set /a reda=%date0:~-2%-1
set date0=
set /a week1=(%reye%+%remo%+%reda%+4)%%7
set reye=
set remo=
set reda=
if %week1%==1 set week0=星期一
if %week1%==2 set week0=星期二
if %week1%==3 set week0=星期三
if %week1%==4 set week0=星期四
if %week1%==5 set week0=星期五
if %week1%==6 set week0=星期六
if %week1%==0 set week0=星期日
set week1=
echo %week0%
set week0=
Floor 13 Posted 2010-10-26 15:23 ·  中国 上海 移动
新手上路
Credits 12
Posts 12
Joined 2010-10-21 14:34
15-year member
UID 176258
Gender Male
Status Offline
Originally posted by clinttt at 2010-10-26 14:51:
@Echo Off
set date0=2017-07-26

set /a year1=%date0:~0,4%%%4
set /a month1=%date0:~5,2%-1
set /a month2=%date0:~5,2%+1
set /a reye=(%date0:~0,4%-1970+1)/4+365*(%date0:~0,4%-1970)
if %date0:~ ...



How to put them together... Combining two scripts, where to insert?

echo Now start statistics:> C:\%date:~0,10%.txt
for /f "tokens=1,2,3" %%c in (C:\OV-BLM-001_2010-10-20_16-02-20.txt) do (if %%c GEQ 2010-10-18 if %%c LEQ 2010-10-24 (echo %%c %%d %%e >> C:\%date:~0,10%.txt))





@Echo Off
set date0=2010-10-26

set /a year1=%date0:~0,4%%%4
set /a month1=%date0:~5,2%-1
set /a month2=%date0:~5,2%+1
set /a reye=(%date0:~0,4%-1970+1)/4+365*(%date0:~0,4%-1970)
if %date0:~5,2% LEQ 2 (set /a remo=%date0:~5,2%/2+30*%month1%) else (
if %date0:~5,2% LEQ 8 (if %year1%==0 (set /a remo=%date0:~5,2%/2+30*%month1%-1) else (
set /a remo=%date0:~5,2%/2+30*%month1%-2)) else (
if %year1%==0 (set /a remo=%month2%/2+30*%month1%-1) else (
set /a remo=%month2%/2+30*%month1%-2)))
set year1=
set month1=
set month2=
set /a reda=%date0:~-2%-1
set date0=
set /a week1=(%reye%+%remo%+%reda%+4)%%7
set reye=
set remo=
set reda=
if %week1%==1 set week0=Monday
if %week1%==2 set week0=Tuesday
if %week1%==3 set week0=Wednesday
if %week1%==4 set week0=Thursday
if %week1%==5 set week0=Friday
if %week1%==6 set week0=Saturday
if %week1%==0 set week0=Sunday
set week1=
echo %week0%
set week0=
Floor 14 Posted 2010-10-26 16:08 ·  中国 广东 深圳 南山区 电信
新手上路
Credits 18
Posts 16
Joined 2010-01-06 15:16
16-year member
UID 158100
Gender Male
Status Offline
Haven't understood what you mean?
Is it like this? You run the script on Wednesday, and you want to output the content from this Sunday to this Saturday?
Only output this week or last week?
Floor 15 Posted 2010-10-26 16:25 ·  中国 上海 移动
新手上路
Credits 12
Posts 12
Joined 2010-10-21 14:34
15-year member
UID 176258
Gender Male
Status Offline
Originally posted by clinttt at 2010-10-26 16:08:
Still don't understand what you mean?
Is it like this? You run the script on Wednesday, and you want to output the content from this Sunday to next Saturday?
Only this week or last week?



Oh, it's like this. This batch script will run once every Sunday, capture the data from the text file based on the date judgment, which is the column of 2010-10-25, grab the data from this Monday to this Sunday, and send it to another new text file. For example, if it runs this Sunday, it grabs all the data of this week, and if it runs next Sunday, it grabs the data of next week, and so on. The file name is named according to the date of the running day.
Forum Jump: