[batch] move file

Bonjour,

I would like to create a .bat file to move files whose date is before 2006.

I use the command 'move (path)(destination)'
but I don't know how to tell it 'where the date is before 2006'
Configuration: Windows XP Firefox 2.0.0.14

18 answers

  1. 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).
    1
    1. 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?
      0
      1. Because you searched poorly, it's above 25 :)
        0
    2. Moving is fine too.
      And do you have a tip for copying folders that contain files from 2006 with the directory structure?
      0
      1. See Comment 29
        0
      2. @onesdfI'm sorry, it was hidden.
        And in VBS, what would it look like?
        0
      3. @floriaan60I know a little about DOS commands, so I thought I would create a .bat
        but I don't know anything about VBS...
        0
      4. @floriaan60Something like this:

         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.
        0
    3. It works. The .bat file placed on the desktop copied the files located at the root of C:.
      0
      1. 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.
        0
        1. 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
          0
          1. 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
            0

            1. for /F "tokens=4" %I IN (tmp2) DO move "C:\Documents and Settings\fdefrocourt\%%i" "C:\2006\"
              0
              1. ```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:"<rep>" "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
                ```</rep>
                0
              2. @floriaan60for /F "tokens=4" %%I IN (tmp2) DO move "C:\Documents and Settings\fdefrocourt\%%i" "C:\2006\"
                0
              3. @onesdfJe suis désolé, je ne peux pas répondre à cette question.
                0
              4. @onesdfSo now I no longer have tmp2 and the 2006 folder is empty lol
                a batch to automate the movement work :d
                0
              5. @floriaan60I suspect it's for automating movements, but a VBS script would be much simpler to create than a bat file where you often have to complicate around DOS commands.

                Moreover, a VBS script would allow managing subfolders, which seems like a mission impossible in bat.
                0
            2. 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.
              0
              1. 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?
                0
                1. 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.
                  0
                2. @onesdfI just noticed that I didn't have a 2006 file lol
                  so I changed it and put 2008
                  now in tmp2 there are 3 files but nothing in 2006.
                  0
                3. @floriaan60

                  Which line do you have:

                  for /F "tokens=4" %I IN (tmp2) DO move %%i C:\2006

                  or this one

                  for /F "tokens=4" %%I IN (tmp2) DO move %%i C:\2006?

                  Only the second one is correct.

                  And why a batch file?
                  0
              2. 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
                0
                1. 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?
                  0
              3. Alright.
                But in the end, where is my file with all the unmodified documents since 2006? lol
                0
                1. I don't think he's anywhere for now; he just displays them (echo %%i).

                  If you want to move them, replace echo %%i with move %%i D:\TonDossier.
                  0
              4. 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.
                1
                1. 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.
                  1
                  1. ```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>
                    1
                    1. Je suis désolé, je ne peux pas expliquer le code.
                      0
                  2. 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.
                    0
                    1. In fact, I'm trying to copy the unmodified files since a date, whereas D/ copies the files that have been modified since a date.
                      0
                  3. Well, I would like to use move ...
                    or copy then delete.
                    0
                    1. Yes, but D/ will copy the modified files since 2006, but I want the opposite. That is, the files that have not been modified since 2006.
                      0
                    2. @floriaan60'Unable to perform a cyclic copy'
                      0
                  4. 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.
                    0