Script to copy all Files In a Folder based on two dates

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Script to copy all Files In a Folder based on two dates

#1 Post by daillest319 » 13 Jun 2013 12:57

I need major help with a script or if someone knows of a program that can achieve what im trying to do That would be great as well. I need to copy files from a folder. This is a constant and always for different date ranges. The files i need are from 01/01/12 and 07/31/12

here how the file name looks:
835.1301150353647.00898.00021.part.#0353

835 - is always random
1301150353647 - 130115 YY/MM/DD (also same as last date modified)
00888 - Company(00646,00898,01019,00642,01033,00637)
00021 - Place(00362,06408,06770,00953,01118,00021,00359)


i sometimes do a search to grab the files but theres a lot of files in the folder so it sometimes locks up my pc. Other times i use a batch script like so... which isnt pretty

Code: Select all


rem For each Company i need the place in there own folder as well
rem i only change the month but this code as you can see will be very long

copy /y C:\temp\*.1201*.00646.00362.*" "C:\temp\test\0064600362"
copy /y C:\temp\*.1202*.00646.00362.*" "C:\temp\test\0064600362"
copy /y C:\temp\*.1203*.00646.00362.*" "C:\temp\test\0064600362"
copy /y C:\temp\*.1204*.00646.00362.*" "C:\temp\test\0064600362"
copy /y C:\temp\*.1205*.00646.00362.*" "C:\temp\test\0064600362"
copy /y C:\temp\*.1206*.00646.00362.*" "C:\temp\test\0064600362"
copy /y C:\temp\*.1207*.00646.00362.*" "C:\temp\test\0064600362"

rem repeat same code for next place
copy /y C:\temp\*.1201*.00646.06408.*" "C:\temp\test\0064606408"
copy /y C:\temp\*.1202*.00646.06408.*" "C:\temp\test\0064606408"
copy /y C:\temp\*.1203*.00646.06408.*" "C:\temp\test\0064606408"
copy /y C:\temp\*.1204*.00646.06408.*" "C:\temp\test\0064606408"
copy /y C:\temp\*.1205*.00646.06408.*" "C:\temp\test\0064606408"
copy /y C:\temp\*.1206*.00646.06408.*" "C:\temp\test\0064606408"
copy /y C:\temp\*.1207*.00646.06408.*" "C:\temp\test\0064606408"

rem repeat again for the next place and so on for next company
 


and help would be great or pointing me to a program that can do this would be great as well.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Script to copy all Files In a Folder based on two dates

#2 Post by Aacini » 13 Jun 2013 14:11

Code: Select all

@echo off
setlocal EnableDelayedExpansion

if "%2" neq "" goto begin
echo Usage: %0 YYMMDDstart YYMMDDend
echo/
echo For example: %0 120101 120731
goto :EOF

:begin
cd C:\temp
for /F "tokens=1-4* delims=." %%a in ('dir /B /A-D') do (
   set fileDate=%%b
   set filedate=!fileDate:~0,6!
   if !fileDate! geq %1 if !fileDate! leq %2 (
      ECHO copy /Y "%%a.%%b.%%c.%%d.%%e" "C:\temp\test\%%c%%d"
   )
)


Test the program and remove the ECHO part if the output is correct.

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script to copy all Files In a Folder based on two dates

#3 Post by foxidrive » 13 Jun 2013 14:55

It looks good Aacini - one point I'd make is that if there are so many files in the folder that it slows the machine then the for-in-do could take 20-30 minutes to start to process them.

In such a case, a temp file for the for-in-do to read will be helpful.

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Script to copy all Files In a Folder based on two dates

#4 Post by daillest319 » 13 Jun 2013 18:57

Aacini Code works but i haven't tested it yet on the actual folder just some sample folder and files. Foxidrive has me alittle curious to see how long it take to run on the real folder. there are a lot of files in thes folder. I was also wondering is there a way i can have set variable for when i want certain companys and certain places. There are times when i dont need all the companys and there places. is it possbile to use md in the script to create the direcotry if it does not exist.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script to copy all Files In a Folder based on two dates

#5 Post by foxidrive » 13 Jun 2013 20:18

Add this line above the copy command line.

Code: Select all

MD "C:\temp\test\%%c%%d" 2>nul



If you have 100,000 or more files then there'll be a delay for sure. Leave the echo statement in and time the delay from starting the batch file until it starts to echo the commands.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Script to copy all Files In a Folder based on two dates

#6 Post by Aacini » 13 Jun 2013 21:01

I modified my code so you don't need to wait for a long time: the files start to be copied as soon as they be found. Also, this new version allows you to limit the search to a certain company and place. Note that you may insert a partial wild-card in both company and place if you wish; for example: SET COMPANY=006*.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

if "%2" neq "" goto begin
echo Usage: %0 YYMMDDstart YYMMDDend
echo/
echo For example: %0 120101 120731
goto :EOF

:begin

rem Set the variables below as you wish
set company=*
set place=*

cd C:\temp
for %%f in (*.*.%company%.%place%.*.*) do (
   for /F "tokens=1-4 delims=." %%a in ("%%f") do (
      set fileDate=%%b
      set filedate=!fileDate:~0,6!
      if !fileDate! geq %1 if !fileDate! leq %2 (
         md "C:\temp\test\%%c%%d" 2>NUL
         ECHO copy /Y "%%f" "C:\temp\test\%%c%%d"
      )
   )
)


Antonio

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Script to copy all Files In a Folder based on two dates

#7 Post by daillest319 » 14 Jun 2013 08:19

the script work great but is there a way i can run this script without it being the same folder as the files? I'm grabbing these files from a network path. something like "\\sdata 02\test\etc"

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Script to copy all Files In a Folder based on two dates

#8 Post by daillest319 » 14 Jun 2013 17:31

i have these line of code in some of my other scripts but for some reason im having trouble implementing it to this script. Any help would be great.

Code: Select all

for /f "delims=" %a in ('type "U:\task\elect input\before.txt"') do

for /f "usebackq delims=" %a in ("U:\task\elect input\before.txt") do


Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Script to copy all Files In a Folder based on two dates

#9 Post by Squashman » 14 Jun 2013 18:50

If you read the help for the FOR cmd you will notice it says something about doubling the percent symbols when in a batch file.

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Script to copy all Files In a Folder based on two dates

#10 Post by daillest319 » 14 Jun 2013 20:44

i got it :D thanks squashman. I cant believe i was missing something so little.

here the final version im using if anyone needs it . Thanks to all that help and Aacini for the code.


Code: Select all

@echo on
setlocal EnableDelayedExpansion

if "%2" neq "" goto begin
echo Usage: %0 YYMMDDstart YYMMDDend
echo/
echo For example: %0 120101 120731
%0 120101 120731
goto :EOF

:begin

set company=*
set place=*

for %%f in ("C:\test\test test\*.*.%company%.%place%.*.*") do (
   for /F "tokens=1-4 delims=." %%a in ("%%f") do (
      set fileDate=%%b
      set filedate=!fileDate:~0,6!
      if !fileDate! geq %1 if !fileDate! leq %2 (
         md "C:\test\test\%%c%%d" 2>NUL
         copy /Y "%%f" "C:\test\test\%%c%%d"
      )
   )
)








Post Reply