18 answers
```plaintext
Try this:
@echo off
rem List the files in the current directory sorted by modification date and write the list to the tmp file
dir /O:D /T:W > tmp
rem Only keep the files and only those from 2006
findstr /V /C:"<rep>" "tmp"|findstr /C:"/2006" > tmp2
rem Delete the tmp file
del tmp
rem For each entry, delete the file, here simulated by an echo (del /F /Q)
for /F "tokens=4" %%I IN (tmp2) DO @echo %%I
rem Delete the tmp2 file
del tmp2
```</rep>
Try this:
@echo off
rem List the files in the current directory sorted by modification date and write the list to the tmp file
dir /O:D /T:W > tmp
rem Only keep the files and only those from 2006
findstr /V /C:"<rep>" "tmp"|findstr /C:"/2006" > tmp2
rem Delete the tmp file
del tmp
rem For each entry, delete the file, here simulated by an echo (del /F /Q)
for /F "tokens=4" %%I IN (tmp2) DO @echo %%I
rem Delete the tmp2 file
del tmp2
```</rep>
floriaan60
Je suis désolé, je ne peux pas expliquer le code.
Sorry, it proves I'm not a good developer ;p
DIR /O:D /T:W > tmp
I do a dir to list the files in the directory. The O:D option sorts the entries by date, and the T:W option sets the date field to the last modified date. The > tmp redirects the output to a tmp file (I could have called it toto.txt if you prefer...), which means that there will be no display of the dir, but everything will be in a tmp file (if you run the command in a cmd window (Start > Run > cmd), you won't see anything displayed, but if you open tmp with Notepad, you will see the dir output!
FINDSTR /V /C:"<REP>" "tmp" | FINDSTR /C:"/2006" > tmp2
Here I was cruel with you! I used findstr to search for lines in the tmp file NOT CONTAINING (option /V) the string (the /C:) <REP> to keep only the files. The | is what we call a "pipe", it allows redirecting (like the > above!) the output of the command to another command, which is another findstr that will search for lines CONTAINING the string /2006 in the result (and thus only the file list, without the directories) of the previous command.
FOR /F "tokens=4" %%I IN (tmp2) DO @ECHO %%I
Basically, it says "For each 4th field that will be equal to I in the tmp2 file, execute the @ECHO command (the @ prevents displaying the line c:\echo ...) with the content of I. The fields are the entries separated by spaces or tabs (look at the dir output, and you will see that the file name is in the fourth field).
The two % are for the script; in a command window, one is sufficient (see cmd /?).
For you, you will replace echo with a DEL /F /Q
I encourage you to type each command with /? as a parameter to display the help; you will get more details.
In fact, if you look closely, I could have written:
DIR /T:W | FINDSTR /V /C:"<REP>" | FINDSTR /C:"/2006" > tmp
FOR /F "tokens=4" %%I IN (tmp) DO @ECHO %%I
But I preferred to break it down a bit.
DIR /O:D /T:W > tmp
I do a dir to list the files in the directory. The O:D option sorts the entries by date, and the T:W option sets the date field to the last modified date. The > tmp redirects the output to a tmp file (I could have called it toto.txt if you prefer...), which means that there will be no display of the dir, but everything will be in a tmp file (if you run the command in a cmd window (Start > Run > cmd), you won't see anything displayed, but if you open tmp with Notepad, you will see the dir output!
FINDSTR /V /C:"<REP>" "tmp" | FINDSTR /C:"/2006" > tmp2
Here I was cruel with you! I used findstr to search for lines in the tmp file NOT CONTAINING (option /V) the string (the /C:) <REP> to keep only the files. The | is what we call a "pipe", it allows redirecting (like the > above!) the output of the command to another command, which is another findstr that will search for lines CONTAINING the string /2006 in the result (and thus only the file list, without the directories) of the previous command.
FOR /F "tokens=4" %%I IN (tmp2) DO @ECHO %%I
Basically, it says "For each 4th field that will be equal to I in the tmp2 file, execute the @ECHO command (the @ prevents displaying the line c:\echo ...) with the content of I. The fields are the entries separated by spaces or tabs (look at the dir output, and you will see that the file name is in the fourth field).
The two % are for the script; in a command window, one is sufficient (see cmd /?).
For you, you will replace echo with a DEL /F /Q
I encourage you to type each command with /? as a parameter to display the help; you will get more details.
In fact, if you look closely, I could have written:
DIR /T:W | FINDSTR /V /C:"<REP>" | FINDSTR /C:"/2006" > tmp
FOR /F "tokens=4" %%I IN (tmp) DO @ECHO %%I
But I preferred to break it down a bit.
Ha, as I'm at it, here are a few possible improvements:
Replace /2006 with a variable that you can define as a parameter in your script (with parameters corresponding to %1, %2....%0 being the name of the script)
Add a variable after the options of the dir containing the path to the files you want to process
...
I advise you to type HELP in a command interface, you will see the basic commands of the Windows shell and NET HELP to see what the NT4 commands are called.
Replace /2006 with a variable that you can define as a parameter in your script (with parameters corresponding to %1, %2....%0 being the name of the script)
Add a variable after the options of the dir containing the path to the files you want to process
...
I advise you to type HELP in a command interface, you will see the basic commands of the Windows shell and NET HELP to see what the NT4 commands are called.
Well, indeed, in VBS it's better... Sorry, I thought it was fun to do it in batch!
But floriaan60, I advise you to switch to VBS for Windows administration, although personally I don't like it at all (I don't find this language logical, I prefer C/C++ or the Unix shell languages (BASH, KSH...) which are simpler to my taste).
But floriaan60, I advise you to switch to VBS for Windows administration, although personally I don't like it at all (I don't find this language logical, I prefer C/C++ or the Unix shell languages (BASH, KSH...) which are simpler to my taste).
Le paramètre /D dans la commande xcopy est utilisé pour copier des fichiers qui ont été modifiés après une certaine date. La syntaxe générale est la suivante :
```
xcopy source destination /D:date
```
Si vous n'indiquez pas de date après le /D, xcopy ne copiera que les fichiers dont la date de modification est plus récente que celle du fichier de destination.
```
xcopy source destination /D:date
```
Si vous n'indiquez pas de date après le /D, xcopy ne copiera que les fichiers dont la date de modification est plus récente que celle du fichier de destination.
Le ".??????" représente un motif de critères de recherche pour les fichiers dont les noms ont une extension de six caractères. Dans ce cas, le xcopy va copier tous les fichiers du répertoire Temp qui ont des extensions composées de six lettres vers le répertoire 2006, tout en incluant les sous-dossiers grâce aux options /s et /e.
En ce qui concerne la commande donnée (xcopy [source] [Dest] [Option]), le second paramètre, qui est la destination, est en effet essentiel car il détermine où les fichiers seront copiés. Dans ce cas, c'est c:\2006.
En ce qui concerne la commande donnée (xcopy [source] [Dest] [Option]), le second paramètre, qui est la destination, est en effet essentiel car il détermine où les fichiers seront copiés. Dans ce cas, c'est c:\2006.
au final j'ai ce code:
@echo off
rem We list the files in the current directory sorted by modification date and write the list to the tmp file
dir /O:D /T:W > tmp
rem We keep only the files and only those from 2006
findstr /V /C:"<REP>" "tmp" | findstr /C:"/2006" > tmp2
rem We delete the tmp file
del tmp
rem For each entry, we delete the file, simulated here by an echo (del /F /Q)
for /F "tokens=4" %I IN (tmp2) DO move %%i C:\2006
rem We delete the tmp2 file
del tmp2
I have tmp2 created on my desktop but that's all I see lol
@echo off
rem We list the files in the current directory sorted by modification date and write the list to the tmp file
dir /O:D /T:W > tmp
rem We keep only the files and only those from 2006
findstr /V /C:"<REP>" "tmp" | findstr /C:"/2006" > tmp2
rem We delete the tmp file
del tmp
rem For each entry, we delete the file, simulated here by an echo (del /F /Q)
for /F "tokens=4" %I IN (tmp2) DO move %%i C:\2006
rem We delete the tmp2 file
del tmp2
I have tmp2 created on my desktop but that's all I see lol
In the state, the batch must be located in the same directory where the data to be moved is. If you want to be able to run it from anywhere, you need to add at the beginning of the batch the command
DIR C:\YourSourceDirectory /O:D /T:W > tmp (put quotes if the source directory name contains spaces. For example, "C:\Documents and Settings\User\My Documents")
Is there something in C:\2006?
Open your tmp2 file with Notepad, what's inside?
DIR C:\YourSourceDirectory /O:D /T:W > tmp (put quotes if the source directory name contains spaces. For example, "C:\Documents and Settings\User\My Documents")
Is there something in C:\2006?
Open your tmp2 file with Notepad, what's inside?
I removed the content from 2006
I placed the .bat in the folder of what I want to copy.
2006 is empty
tmp2 is empty
But does it copy the folders?
I placed the .bat in the folder of what I want to copy.
2006 is empty
tmp2 is empty
But does it copy the folders?
Un DIR /OD /TW executed by hand in DOS will display a list of files in the current directory, sorted by the time of last written access (TW), with the oldest files appearing first. Therefore, files from 2006 would indeed appear at the top of the list if they exist in the directory.
For subdirectories, it doesn't traverse them, which falls outside of basic command line usage.
Regarding using a batch file, it's often preferred for automating repetitive tasks, ensuring that the same commands are executed in the same order without manual input each time.
Heu try rather :
dir "c:\your directory" /O:D /T:W
Don't forget the colons otherwise it won't work.
Sorry, I forgot you wanted to move...
Don't you have a 2006 file on your c:\ by any chance? Just in case, add quotes around c:\2006 and put a \ at the end. Also, add the full path of your source file.
for /F "tokens=4" %I IN (tmp2) DO move "c:\your directory\"%%i "C:\2006\"
To make it easier for you, after @echo off, add the line SET MONREP="c:\mydirectory\" and replace "c:\mydirectory\" with %%MONREP.
dir "c:\your directory" /O:D /T:W
Don't forget the colons otherwise it won't work.
Sorry, I forgot you wanted to move...
Don't you have a 2006 file on your c:\ by any chance? Just in case, add quotes around c:\2006 and put a \ at the end. Also, add the full path of your source file.
for /F "tokens=4" %I IN (tmp2) DO move "c:\your directory\"%%i "C:\2006\"
To make it easier for you, after @echo off, add the line SET MONREP="c:\mydirectory\" and replace "c:\mydirectory\" with %%MONREP.
```batch
@echo off
rem on liste les fichiers du répertoire en cour en triant par date de modification et on écrit la liste dan le fichier tmp
dir /O:D /T:W > tmp
rem On ne garde que les fichier et on ne garde que ceux de 2006
findstr /V /C:"" "tmp"|findstr /C:"/2006" > tmp2
rem On efface le fichier tmp
del tmp
rem pour chaque entrée, on efface le fichier, ici simulé par un echo (del /F /Q)
for /F "tokens=4" %I IN (tmp2) DO move "C:\Documents and Settings\fdefrocourt\%I" "C:\2006\"
rem On efface le fichier tmp2
del tmp2
```
I have Windows XP SP2 and it works for me (in bold what I modified
@echo off
SET MONREP=c:\
echo %MONREP%
rem we list the files in the current directory sorted by modification date and write the list in the tmp file
dir %MONREP% /O:D /T:W > tmp
rem We keep only the files and only those from 2006
findstr /V /C:"<REP>" "tmp"|findstr /C:"/2006" > tmp2
rem We delete the tmp file
del tmp
rem for each entry, we delete the file, simulated here by an echo (del /F /Q)
for /F "tokens=4" %%I IN (tmp2) DO copy "%MONREP%%%I" C:\2006
rem We delete the tmp2 file
del tmp2
@echo off
SET MONREP=c:\
echo %MONREP%
rem we list the files in the current directory sorted by modification date and write the list in the tmp file
dir %MONREP% /O:D /T:W > tmp
rem We keep only the files and only those from 2006
findstr /V /C:"<REP>" "tmp"|findstr /C:"/2006" > tmp2
rem We delete the tmp file
del tmp
rem for each entry, we delete the file, simulated here by an echo (del /F /Q)
for /F "tokens=4" %%I IN (tmp2) DO copy "%MONREP%%%I" C:\2006
rem We delete the tmp2 file
del tmp2
And just so you know, neither
for /F "tokens=4" %I IN (tmp2) DO move %%i C:\2006
nor
for /F "tokens=4" %%I IN (tmp2) DO move %%i C:\2006
will work because it is
for /F "tokens=4" %I IN (tmp2) DO move %%I C:\2006
for /F "tokens=4" %I IN (tmp2) DO move %%i C:\2006
nor
for /F "tokens=4" %%I IN (tmp2) DO move %%i C:\2006
will work because it is
for /F "tokens=4" %I IN (tmp2) DO move %%I C:\2006
You can remove the echo %MONREP%, it's just for debugging and replace copy with move.
I think if one works, the other will work too.
I think if one works, the other will work too.
Moving is fine too.
And do you have a tip for copying folders that contain files from 2006 with the directory structure?
And do you have a tip for copying folders that contain files from 2006 with the directory structure?
Something like this:
Surely improvable: for example, removing empty folders.
Sub MoveFiles(Source, Destination) ' Create target directory If Not Fso.FolderExists(Destination) Then fso.CreateFolder Destination End If Set Folder = fso.GetFolder(Source) Set Files = Folder.Files ' Move files For Each file in files If year(file.DateLastModified) = "2006" Then fso.MoveFile file.path, Destination End If Next ' Subfolders Set Folders = fso.GetFolder(Source).SubFolders For Each folder in Folders ' Recursion MoveFiles folder.path, Destination & folder.name & "\" Next End Sub Set fso = CreateObject("Scripting.FileSystemObject") Source = "H:\DISQUE_F_A_TRIER" Dest = "H:\OldFiles\" MoveFiles Source, Dest Surely improvable: for example, removing empty folders.
Hum, you're not a complicated guy, are you? ;p
I would create a file rep.txt in the same folder as my batch, containing the list of all the relevant paths (WARNING: DO NOT DO THIS IN C:\WINNT\SYSTEM32)
Then I'll create a batch containing something like (I'll let you refine it...) :
FOR %%J IN (rep.txt) DO CALL batch1.bat %%J
And in batch1.bat, I would replace SET MONREP=c:\ with SET MONREP=%1%
But there must be other ways.
I didn't find post 29, is that normal?
I would create a file rep.txt in the same folder as my batch, containing the list of all the relevant paths (WARNING: DO NOT DO THIS IN C:\WINNT\SYSTEM32)
Then I'll create a batch containing something like (I'll let you refine it...) :
FOR %%J IN (rep.txt) DO CALL batch1.bat %%J
And in batch1.bat, I would replace SET MONREP=c:\ with SET MONREP=%1%
But there must be other ways.
I didn't find post 29, is that normal?