Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch File to Move Files By date

Posted on 2014-10-29
12
Medium Priority
?
259 Views
Last Modified: 2014-12-26
Below is an example of the script that I am currently using but would like to expand a little.  First I would like to just list all the directories that need it to run in instead of having to repeat the entire script each time.  I have about 30 different directories to maintain.  Also I am looking to find a way to leave out the last months worth of files because they are required to be left in the source directory.  I would also like this to not include zip and rar files in the moving process..


REM \\NAS_01\EDI\204\Customer_A\Archive

      set "source= \\NAS_01\EDI\204\Customer_A\Archive"
      for %%F in ("%source%\*") do (
        for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
            if not exist "%source%\%%C" mkdir "%source%\%%C"
            move "%%~fF" "%source%\%%C"
      echo "%%~fF"
      ))
REM C:\EDI\204\Customer_B\Archive

      set "source= C:\EDI\204\Customer_B\Archive"
      for %%F in ("%source%\*") do (
        for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
            if not exist "%source%\%%C" mkdir "%source%\%%C"
            move "%%~fF" "%source%\%%C"
      echo "%%~fF"
      ))
0
Comment
Question by:oddessy
  • 5
  • 3
  • 2
12 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 40410835
What is the difference between:

set "source= \\NAS_01\EDI\204\Customer_A\Archive"

and

set "source= C:\EDI\204\Customer_B\Archive"

Are the source locations on two different machines?

If they the Customer_* folders are all in the same source are there any other folders in that folder?  

What is the significance of the "204"? Will it change?
0
 

Author Comment

by:oddessy
ID: 40410906
The difference between the following two is that they are in different paths local and on a remote machine.

set "source= \\NAS_01\EDI\204\Customer_A\Archive"
and
set "source= C:\EDI\204\Customer_B\Archive"

The 204 is just the path and does not have a significance, yes it does change.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40411175
So there are different sources that can change.  

What about the destination folder?

if not exist "%source%\%%C" mkdir "%source%\%%C"

Where is %%C defined?

You are placing the moved files on the same system as the source in both cases?
(NAS_01 and C respectively)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:oddessy
ID: 40411356
The destination is a subfolder of the source that is the 4 digit year which represents the date modified or created I am not sure now.

%%C is not defined but it work to create a folder with the year.  Moved files are almost always in the same directory as the source unless I use the bellow code..

Echo off
      set "source=E:\EDI\EDI204\CustomerA\Archive\Archive 2012-12-11"
      set "dest=E:\EDI\EDI204\CustomerA\Archive"
      for %%F in ("%source%\*") do (
        for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
            if not exist "%dest%\%%C" mkdir "%dest%\%%C"
            move "%%~fF" "%dest%\%%C"
      echo "%%~fF"
      ))
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40411796
***   THIS IS UNTESTED  ***

I have commented out the actual move and added an echo line to tell you what it would have done for testing purposes.

Here is what I have come up with. I am sure that someone else could do a better job or perhaps suggest a better method such as VBS or Robocopy.


REM ***********************************
REM * Assign the source folders to a variable *
REM ***********************************

set source[1]=\\NAS_01\EDI\204\Customer_A\Archive
set source[2]=C:\EDI\204\Customer_B\Archive

REM *** For each source variable repeat incrementing the number ***

for /L %%i in (*%start%,1,%end%) do (

            for %%F in ("%L%\*") do (
                 for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
                      if not exist "%L%\%%C" mkdir "%L%\%%C"

	       REM *** hide .zip and .rar files so they don't get moved ***
	       ATTRIB +H *.zip
                           ATTRIB +H *.rar

 REM                 move "%%~fF" "%L%\%%C"
                           echo I would have moved "%%~fF" to "%L%\%%C" 

REM *** if satisfied then uncomment the "move" line and comment out the "echo" line above ***

            echo "%%~fF"

                          REM *** un-hide .zip and .rar files ***
                          ATTRIB -H *.zip
                          ATTRIB -H *.rar
       )
   )
)     

Open in new window


I did not address the issue of leaving some files based on date. Do you want to leave the last 30 days or only the current month?
0
 

Author Comment

by:oddessy
ID: 40411825
When I tried the above code it wants to move the files on my C;\ root directory not the source directory..
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40412184
I see some issues. I need to play and see what I can come up with.  I have to leave for the day though.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40439679
I'll take a different approach. First, taking your original script, it is most simple to just apply a small change to allow repeated execution for different paths:
@echo off
setlocal EnableDelayedExpansion
for %%a in (
  \\NAS_01\EDI\204\Customer_A\Archive
  C:\EDI\204\Customer_B\Archive
 "C:\Path with Spaces\and (parens)"
) do call :movefiles "%%~a"
exit /b

:movefiles
set source=%1
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do set year=%%C
  if not exist "%source%\!year!" mkdir "%source%\!year!"
  echo %%~xF | findstr /v /L /i ".ZIP .RAR" >nul && echo move "%%~fF" "%source%\!year!" && echo %%~fF
)

Open in new window

That will leave out ZIP and RAR files. The MOVE is echoed only for testing.
The 30-days-stuff is hard to do in a batch. VBS or PowerShell are better suited for that. The mentioned RoboCopy isn't suited well either, as we would have to call it on single files because of the year subfolder. If that can be dropped, and just using the actual year, it's a cinch.
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40439722
This is the PowerShell way, including the 30-days restriction.
$recent = (get-date).AddDays(-30)

foreach ($source in
   \\NAS_01\EDI\204\Customer_A\Archive,
   C:\EDI\204\Customer_B\Archive,
  "C:\Path with Spaces\and (parens)"
) {
  dir $source\* -exclude *.zip, *.rar | 
    ? { $_.LastWriteTime -lt $recent } |
    % {
      $dest = "$source\$($_.LastWriteTime.Year)"
      if (!(Test-Path $dest -PathType Container)) { mkdir $dest }
      $_ | move-item -Destination $dest -whatIf
    }
}

Open in new window

Again, test mode - remove -whatIf if you want to apply the magic.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40440218
Thanks Qlemo, I got called away on a family situation and just got back today.  The powershell method looks very good.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This article is about my experience upgrading my consulting machine to Windows 10 Version 1709 (The Fall 2017 Creator Update)
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question