|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
how to copy files using date
|
Original Message
|
Name: nael
Date: January 28, 2003 at 16:40:36 Pacific
Subject: how to copy files using date OS: windows NT CPU/Ram: intel, 512MB
|
Comment: how to copy the files by date from one folder to another...Eg: i want to copy the files dated in 01/01/2003 from OLD_FILES to COPIED-FILES. and how to make a batch file so that it gets executed daily to copy the current day files. your cooperation is highly appreciated. Nael
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Ivo
Date: January 29, 2003 at 02:23:29 Pacific
|
Reply: (edit)To copy the files with a required date from a folder to a new one use the following batch, where leaving blank the last parameter means to copy the current date files: @Echo Off :: CDAT.BAT Syntax: CDat OldDir NewDir [Date] Set ToDay=%3 If not "%ToDay%"=="" GoTo EXEC For /F "Tokens= 1* Delims= " %%a in ('Date /T') Do Set ToDay=%%b :EXEC Dir %1 | Find "%ToDay%" | Find /V "DIR" > %2\%0$1.tmp For /F "Tokens=4 Delims= " %%i in (%2\%0$1.tmp) Do Copy %1\%%i %2 Del %2\%0$1.tmp Set ToDay= I hope this helps.
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: Nael
Date: January 29, 2003 at 06:41:20 Pacific
|
Reply: (edit)I really thank you very much for ur fast reply. but im not actually that sophisticated in DOS and so i would prefer if you do explain that code... if you dont mind Nael
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: IVO
Date: January 29, 2003 at 07:40:32 Pacific
|
Reply: (edit)Well, I sent you the script for a batch file named CDat.bat (Copy by Date) or whatever you want to name it. To use the batch you have to type CDat Dir_From Dir_To [Date] where Dir_From is the folder in which the files to be copied are resident and Dir_To the destination folder. Date is the date belonging to the files you want to be copied and if omitted it is assumed to be the to-day date. Be sure to type the date in the format used in your Country, i.e. the format showed in the list produced by a Dir command. The script first assigns the third parameter (Date) to the environmental variable ToDay and check for its existence (i.e. a date was expressed as last parameter on the command line). If so, the processing begins at label EXEC, otherwise the variable is set to the current date parsing the output of the Date /T command using the For /F statement (be sure to do follow a blank after the = at the end of the keyword Delims). The process begins with a Dir command producing a list of the source folder piped through two succesive Find commands to filter only the desidered lines, i.e. the ones containing files names with the desidered date. The first Find check for the date, the other exclude the DIRectories. The output is redirected to a temporary file %0$1.tmp that expands as CDat$1.tmp being %0 the name of the executing batch. Then the For /F command parses the temporary file and, extracting from each line the filename (%%i as the fourth token in every text line), Copies it from the source directory %1 to the destination folder %2 (the Copy command should be better expressed as Copy "%1\%%i" %2 to account for blanks in filenames). Before ending the script clears the temporary file and the environment variable. To get a complete explanation for the commands used in this sample type them at prompt followed by /?, so: If /?, For /?, Set /? and so on... By the way the script uses the extended batch commands available only under the Win NT/2K/XP NTVDM Emulator not true DOS. I hope my explanation gives you a better understanding of my script that, believe, is quite simple.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Nael
Date: January 29, 2003 at 08:18:49 Pacific
|
Reply: (edit)thank you again for your explanation which made that code very easier to be understood and at least know the concept of it... Now im trying to execute it but it throws out this error: 'Find' is not recognized as an internal or external command, operable program or batch file. though my operating system is windows 2000 professional. i really feel that im exerting you with me but its very important for me.... Regards, Nael
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: IVO
Date: January 29, 2003 at 08:39:09 Pacific
|
Reply: (edit)The command Find is the Find.exe file located in C:\WinNt\System32 folder (%SystemRoot%\system32). I tested my script under Windows 2000 Professional and got no problems. First of all try to localize Find.exe through the search option of the Start Menu and give me back the result.
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Nael
Date: January 29, 2003 at 08:40:39 Pacific
|
Reply: (edit)i tried out that script in another machine and it was goin fine and when i execute it using your syntax it shows that everything is fine... like c:\cdat>cdat.bat c:\cdat c:\copy [29/01/2003] and the result would be: c:\cdat> so the result means that everything has been copied to the c:\copy folder but in fact i doesnt... Thanx, Nael
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: IVO
Date: January 29, 2003 at 08:54:42 Pacific
|
Reply: (edit)I do not understand your last post. Has the script worked or are there still problems? The script may need to be fixed, the test was not stretched... Now I must be away for about three hours than I will back if you need help.
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: Nael
Date: January 29, 2003 at 09:43:36 Pacific
|
Reply: (edit)well, now it doesnt show any error but it doesnt actually copy the files from the directory which files that need to be copied to the destination folder. So what i mean is after executing the batch file and go to check the destinated folder whether the files have been copied or not...i get nothin....so its not copying the files.. thats the problem now.. Regards, Nael
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: IVO
Date: January 29, 2003 at 13:09:24 Pacific
|
Reply: (edit)First question: Was Find located and executed? In your first post you said the command was unrecognized by the System. Second question: Did you type the date as [29/01/2003] or without the square brackets? The brackets mean "non mandatory" operand and are not part of the parameter itself, it is a (well) known notation. If you typed the brackets no file was recognized as copiable in the parsing phase (Find "%ToDay%"). I need to know thouse facts before I can continue my diagnosis.
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: Nael
Date: January 29, 2003 at 14:49:08 Pacific
|
Reply: (edit)well, now everything is fine my dear... it copies the files specified by the date you want but still im not getting about that batch file that should be executed automatically... like knowing the current day from the computer Clock and do copy the files of that particular day by itself...as i could understand that u have to type that command which will execute that batch file manually... is there anyway to modify that so it knows what files should b copied. in other words, i want this task to be done daily in a professional way... Thank you very much for your kind cooperation. Nael
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
Name: IVO
Date: January 30, 2003 at 02:03:03 Pacific
|
Reply: (edit)First of all, use the following version of the script that overrides the problem of file names with embedded blanks and has other minor improvements. @Echo Off :: CDAT.BAT Syntax: CDat OldDir NewDir [Date] Set ToDay=%3 If not "%ToDay%"=="" GoTo EXEC For /F "Tokens=2 Delims= " %%a in ('Date /T') Do Set ToDay=%%a :EXEC Dir %1 /X | Find "%ToDay%" | Find /V "DIR" > %0$1.tmp For /F "Tokens=4 Delims= " %%i in (%0$1.tmp) Do Copy %1\%%i %2 > Nul Del %0$1.tmp Set ToDay= Now, the script acts on ONE source directory and ONE destination folder every time it is called. The date may be expressed or by default got from the system clock. To copy files stored on different directories you must setup another batch file as below: @Echo Off ...... ...... Call CDat Source_1 Dest_1 [Date] Call CDat Source_2 Dest_2 [Date] ..... To start automatically the script must be inserted in the StartUp Folder, if you want it runs at power up, but if you want it to copy the files at the end of a daily shift (e.g. at 19.00 or 7.00 PM) you must set the Task Skeduler Service. Sorry but this issue is beyond my current experience, I can only tell you it can be done (you may find a first source of info in the on line help in the Start Menu, keyword "Task Skeduler"). Ready to help you as far as I can.
Report Offensive Follow Up For Removal
|
|
Response Number 12
|
Name: Secret_Doom
Date: January 30, 2003 at 10:45:54 Pacific
|
Reply: (edit)Nael is on Windows NT, he can use the AT command. To schedule the system to run the batch file everyday at 19:00h, do this at the command prompt: AT 19:00 /every:m,t,w,th,f,s,su "c:\folder\cdat.bat" Where c:\folder\cdat.bat is the batch file full path. Type "at/?" on the command prompt for more information. -- Leonardo Pignataro - Secret_Doom -- secret_doom@hotmail.com www.batch.hpg.com.br
Report Offensive Follow Up For Removal
|
|
Response Number 13
|
Name: Nael
Date: January 30, 2003 at 19:21:43 Pacific
|
Reply: (edit)i dont actually understand the (GOTO EXEC) :EXEC (tokens= ) (delims) ( set ToDay=%3 " why u particularly selected %3 to b the string of ToDay variable...??!!).. I really appreciate all of that amazing ideas, as i really got whats the drift for every little thing in that script but still i want to get the point about the doubts that are mentioned above. Thanx,
Report Offensive Follow Up For Removal
|
|
Response Number 14
|
Name: IVO
Date: January 31, 2003 at 02:20:30 Pacific
|
Reply: (edit)I assign the third parameter (%3) on the command line to the environmental variable ToDay, being the first parameter the Source_Folder (%1) and the second the Destination_Folder (%2) that are mandatory to run the script flawless. The third parameter is optional, so the If statements checks for its presence and if it is not null ("") jumps to the command labeled :EXEC. Otherwise the script assigns the current date value to the variable (default action if the third parameter on the command line is omitted (do you remember square brackets [] in syntax notation?)). To do so the output of the Date /T command is parsed by a For /F statement trapping the second token (token=2) separated by space (delims= ). The output of Date /T is in fact in NT/2K "day dd/nm/year" or something like. So ToDay is set at the value %%a that is dd/mm/year. Dir Source_Folder (%1) /X produces as output a list using Dos short names for files in the 8.3 format so avoiding problems related to embedded blanks in copying. The lines stored in a temp file are parsed to trap in %%b the filename that is the fourth token (token=4) delimited by spaces (delims= ). the ouput of Dir /X is like "dd/mm/year hh.mm FileSize FileName" I want to thank Secret_Doom for is suggestions; he is really the RONALDO of scripting and I learned greatly from his posts.
Report Offensive Follow Up For Removal
|
|
Response Number 15
|
Name: Secret_Doom
Date: January 31, 2003 at 08:57:31 Pacific
|
Reply: (edit)Ivo wrote: > I want to thank Secret_Doom for is > suggestions; he is really the RONALDO > of scripting and I learned greatly > from his posts. I'm glad that you learned from my posts (but on this one, you did all the work, good job!). But you know, this Ronaldo thing is really funny... You refer to the brazilian socker player, right? So, guess what... I'm brazilian too hehehe =D -- Leonardo Pignataro - Secret_Doom -- secret_doom@hotmail.com www.batch.hpg.com.br
Report Offensive Follow Up For Removal
|
|
Response Number 16
|
Name: Nael
Date: January 31, 2003 at 11:06:49 Pacific
|
Reply: (edit)I would like to thank IVO very much for his kind patience in replying to all my Q as clearing my doubts easily and beautifully. Thank you Mr.IVO and Thanks to Mr.Ronaldo too.. By the way, im a brazilian team fan too..:)
Report Offensive Follow Up For Removal
|
|
Response Number 17
|
Name: nael
Date: February 2, 2003 at 15:11:01 Pacific
|
Reply: (edit)Excuse me... i just want this script inside the same batch file which copies files by date and this script actually creates the folder by the current date.. @echo off for /f "tokens=1-4 delims=/ " %%a in ('date /t') do @echo md %%d%%c%%b > md1.bat call md1.bat del md1.bat and this is the script you have written to me:
@Echo Off Set ToDay=%3 If not "%ToDay%"=="" GoTo EXEC For /F "Tokens=2 Delims= " %%a in ('Date /T') Do Set ToDay=%%a :EXEC Dir %1 /X | Find "%ToDay%" | Find /V "DIR" > %0$1.tmp For /F "Tokens=4 Delims= " %%i in (%0$1.tmp) Do Copy %1\%%i %2 > Nul Del %0$1.tmp Set ToDay= i think u know exactly what i mean ...i want those two scripts integrated with each other so that it creates folder by date and copies files by date too.... Thanx,
Report Offensive Follow Up For Removal
|
|
Response Number 18
|
Name: IVO
Date: February 3, 2003 at 03:05:03 Pacific
|
Reply: (edit)If I understood the following script is what you need: @Echo Off :: Cdat.bat Syntax: CDat From_Folder :: Assign the first (and only) param to FromDir :: If you want to freeze the directory key-in it :: directly into the variable i.e Set FromDir=Folder_Name :: use "" if there are embedded blanks Set FromDir=%1 :: Set ToDay as dd/mm/year For /F "Tokens=2 Delims= " %%a in ('Date /T') Do Set ToDay=%%a :: Set ToDir as yearmmdd For /F "tokens=1-3 delims=/" %%x in ("%ToDay%") Do Set ToDir=%%z%%y%%x :: Create the output dir if it not exists (a cautelative statement) If not exist %ToDir%\Nul MD %ToDir% :: Actual processing Dir %FromDir% /X | Find "%ToDay%" | Find /V "DIR" > %0$1.tmp For /F "Tokens=4 Delims= " %%i in (%0$1.tmp) Do Copy /Y %FromDir%\%%i %ToDir% > Nul :: Clear variables and file before leaving If exist %ToDir%\%0$1.tmp Del %ToDir%\%0$1.tmp Set FromDir= Set ToDir= Set ToDay= Del %0$1.tmp :EXIT Each script's line is annoted (::) to give you a better understanding, but if you need more explanation or something other post me.
Report Offensive Follow Up For Removal
|

|

|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Disk Operating System Forum Home
|
|
|