![]() |
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-02 10:20 |
48,037 topics / 350,122 posts / today 0 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » Ask for a batch script to delete files |
| Printable Version 2,835 / 27 |
| Floor1 onlykier | Posted 2006-11-05 15:10 |
| 初级用户 Posts 23 Credits 50 | |
|
Suppose the directories are as follows:
d:\a\b d:\a\c\a1 d:\a\c\a2 And there are several files in each of the 3 directories. Now, we want to delete all the files with the earliest creation time in each directory. How to write the command? Thank you, everyone |
|
| Floor2 lxmxn | Posted 2006-11-05 18:41 |
| 版主 Posts 4,938 Credits 11,386 | |
|
What does the "earlier files" mentioned by the original poster refer to? Do you want to delete the first few files with an earlier creation time? If you want to delete the first few files with an earlier creation time, you can use the following code: |
|
| Floor3 onlykier | Posted 2006-11-05 20:09 |
| 初级用户 Posts 23 Credits 50 | |
|
Brother of 2f, I want to delete all files of the earliest creation day, but I don't know the specific number. How should I do it?
Suppose there are 60 files in the directory, with 4 files generated each day, that is, there are files for 15 days. Now I want to delete the 4 files of the earliest day. How should I do it? If the number of files in the directory is unclear, the number generated each day is unclear and not fixed, and now I want to delete all files of the earliest day. How should I do it? All the above are based on the file creation time. [ Last edited by onlykier on 2006-11-5 at 07:35 AM ] |
|
| Floor4 onlykier | Posted 2006-11-05 21:13 |
| 初级用户 Posts 23 Credits 50 | |
|
Go back to sleep, waiting for the good news from brothers tonight..
|
|
| Floor5 onlykier | Posted 2006-11-05 21:24 |
| 初级用户 Posts 23 Credits 50 | |
|
Got an idea, mark it here, and I'll come back to work on it at night.
Use dir /a-d/tc >>1.txt. Use a loop to save the first number in each line of 1.txt, which is the time. Set a variable in the loop. Because the first file must be deleted. When the loop reaches the second time, compare the time with the time of the first file. If they are the same, continue the loop and delete; if not, jump out of the loop. |
|
| Floor6 vkill | Posted 2006-11-06 07:15 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
| Floor7 onlykier | Posted 2006-11-06 21:55 |
| 初级用户 Posts 23 Credits 50 | |
|
Brother upstairs, the examples in your link all have parameters, and the question I asked is not like that...
|
|
| Floor8 onlykier | Posted 2006-11-06 22:47 |
| 初级用户 Posts 23 Credits 50 | |
|
Please check why the following code is wrong
@echo off dir /s /a-d /tc|sort|find "-"|find /v "Sequence" set c=0 for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "Sequence"') do ( if c equ 0 (set a=%%i echo %%j )else ( set b=%%i if "b"=="a" (echo %%j set a=b) set /a c+=1)) The syntax of the command is incorrect. [ Last edited by onlykier on 2006-11-6 at 10:24 AM ] |
|
| Floor9 onlykier | Posted 2006-11-06 23:35 |
| 初级用户 Posts 23 Credits 50 | |
|
Is anyone here? Waiting online
|
|
| Floor10 redtek | Posted 2006-11-07 01:35 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
If there is an error when running, remove the "@echo off" and then debug, and you will see the information on which command the system error occurred at : )
In this sentence, is there a special symbol in "dir /s /a-d /tc|sort|find "-"|find /v "Sequence"? Think about which symbol needs to be escaped with the escape character (^)? (If under the @echo off state, you will not see more detailed error information) Please take a look at the help of IF /? again, and then look at your usage of this IF : ) "The ELSE clause must appear on the same line after IF..." Do you see if your usage of IF is not in line with the requirements? ...... Another: for …… if ....) else (.... if ...() ).... Among your so many statements, there are many parentheses, especially the last two " )) " parentheses, When the amount of code is very large, if the code is not written in a standardized way, whether it is yourself or others, it is simply unbearable to find errors, And it is not conducive to clarity and debugging... Another: You have assigned a value earlier: set c=0 Then the following... if c equ 0 (If c is equal to 0, then...) Will this c variable be "recognized" by if? Do you think there is a difference between if %c% equ 0 and if c equ 0? [ Last edited by redtek on 2006-11-7 at 01:38 AM ] |
|
| Floor11 onlykier | Posted 2006-11-07 02:47 |
| 初级用户 Posts 23 Credits 50 | |
|
dir /s /a-d /tc|sort|find "-"|find /v "序列"
The meaning of this sentence is /s displays the files in the current directory and subdirectories /a-d removes the information of folders from the result /tc displays by creation time |sort sorts the result of dir /s/a-d/tc, sorting from earliest to latest creation time |find "-" filters the result of dir /s /a-d /tc|sort to only show lines with "-" |find /v "序列" functions as, since there is a line of result that is not desired in the result of dir /s /a-d /tc|sort|find "-", it removes the line with "序列" from the result of dir /s /a-d /tc|sort|find The result of dir /s /a-d /tc|sort|find "-"|find /v "序列" is similar to the following 2006-10-01 19:49 123,392 asdfasdfasd060921-060927.xls 2006-10-01 19:49 123,904 asdfasdfasd060914-060920.xls 2006-10-01 19:49 161,280 asdfasdfasd060831-060906.xls 2006-10-01 19:49 167,936 asdfasdfasd060907-060913.xls 2006-10-07 19:08 160,768 asdfasdfasd060824-060830.xls 2006-10-07 19:13 124,928 asdfasdfasd060928-061004.xls 2006-10-12 00:17 124,416 asdfasdfasd061005-061011.xls 2006-10-19 08:18 124,928 asdfasdfasd061012-061018.xls 2006-10-26 12:22 124,928 asdfasdfasd061019-061025.xls The above files are in different directories The key is the sentence for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "序列"') I want to only get the first column and the fourth column in the above result, that is, the time and the file name But this for shows an error that there should not be | here. You can test the following sentence for /f "tokens=1,4 delims= " %%i in ('dir /s /a-d /tc|sort|find "-"|find /v "序列"') do echo %%i As for the loop body in for, maybe I expressed it poorly, everyone can run the following code set b=0 for /l %%i in (1,1,4) do ( echo %b% set /a b+=1) I want to display 0 1 2 3 4 But it shows as 0 0 0 0 0 Please ask the expert how to do it? [ Last edited by onlykier on 2006-11-6 at 01:52 PM ] |
|
| Floor12 onlykier | Posted 2006-11-07 02:51 |
| 初级用户 Posts 23 Credits 50 | |
|
By the way, what's the difference between %%i %i% !i! %i?
|
|
| Floor13 redtek | Posted 2006-11-07 03:07 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
%%i comes from for %%i in (...) do ... The double % sign is used when running in batch processing, representing a variable.
%i% comes from Set i=Redtek. This form can be used in both batch processing or the command line. %i comes from directly typing a command in non-batch processing (in the command line), for example: C:\> for %i in (...) do .... It also represents a variable. %0 %1 .... also represent variables (referring to parameters), the running parameters brought in when running the batch processing. For example: Dir /? This /? represents a parameter. ---------------------------- onlykier develops too fast and has skipped many interesting contents. It is suggested that Brother onlykier must go to see the following post~:) Then do every line of code from the beginning to the end of the content on the following post~:) [Simple Index]About SET variable, delayed variable, usage of various symbols... http://www.cn-dos.net/forum/viewthread.php?tid=24549&fpage=1 In addition: I hope onlykier does every content from the beginning to the end in the link of the above post~:) Treat it as a forum assignment, hee-hee... [ Last edited by redtek on 2006-11-7 at 03:11 AM ] |
|
| Floor14 onlykier | Posted 2006-11-07 03:18 |
| 初级用户 Posts 23 Credits 50 | |
|
Brother upstairs! i! How to use this?
And I want to display this result 0 1 2 3 4 How to do it |
|
| Floor15 redtek | Posted 2006-11-07 03:28 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
Originally posted by onlykier at 2006-11-7 03:18: First method: |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |