中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-12 11:37
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Help] Grabbing file content, a bit complicated
Printable Version  3,465 / 26
Floor1 acmilanxr Posted 2010-10-25 10:00
新手上路 Posts 12 Credits 12
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.
Floor2 clinttt Posted 2010-10-25 14:32
新手上路 Posts 16 Credits 18
This is very simple!
Floor3 acmilanxr Posted 2010-10-25 15:13
新手上路 Posts 12 Credits 12
Then how to write that, I basically don't understand this.
Floor4 a113432 Posted 2010-10-25 16:22
新手上路 Posts 1 Credits 1
Not quite understand, help you push it up
Floor5 clinttt Posted 2010-10-25 18:31
新手上路 Posts 16 Credits 18
Are the date formats fixed? Is it the format of 2010-02-01 or 2010-2-1?
Floor6 clinttt Posted 2010-10-25 19:25
新手上路 Posts 16 Credits 18
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!
Floor7 clinttt Posted 2010-10-25 19:27
新手上路 Posts 16 Credits 18
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))
Floor8 acmilanxr Posted 2010-10-26 09:23
新手上路 Posts 12 Credits 12
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 ]
Floor9 acmilanxr Posted 2010-10-26 10:23
新手上路 Posts 12 Credits 12
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...
Floor10 clinttt Posted 2010-10-26 14:05
新手上路 Posts 16 Credits 18
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.
Floor11 acmilanxr Posted 2010-10-26 14:09
新手上路 Posts 12 Credits 12
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?
Floor12 clinttt Posted 2010-10-26 14:51
新手上路 Posts 16 Credits 18
@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=
Floor13 acmilanxr Posted 2010-10-26 15:23
新手上路 Posts 12 Credits 12
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=
Floor14 clinttt Posted 2010-10-26 16:08
新手上路 Posts 16 Credits 18
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?
Floor15 acmilanxr Posted 2010-10-26 16:25
新手上路 Posts 12 Credits 12
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.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023