Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Batch File to Move Files By date

Posted on 2014-10-29
12
249 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 

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 69

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 69

Accepted Solution

by:
Qlemo earned 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
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…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

860 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