Is there a way to move files in a directory (we have 1.7 million) to folders based upon a date?
WOuld like to move all files created between 1-1-2010 and 2-1-2010 to a specific folder
You can use the Robocopy feature. It comes as default in Windows Vista and Windows 7 and you can download it in Windows XP in the microsoft website.
If your Windows is 64 bit it even move files that have a path longer than 256 characters, unlike the CTRL+C, CTRL+V on Windows Explorer (I can't understand why). To see the program help you can write the following in the DOS Prompt (example, usually you can't write to the root):
robocopy /? > c:\robocopyhelp.txt
Use the switches "/MINAGE" for setting the minimum age of the file to be copied/moved and "/MAXAGE" for setting the maximum age.
I've never moved files before and never tried to filter them by age, but I think the syntax should be (from drive F to G, for example, and only 2011 files):
robocopy F:\ G:\ /MOVE /MAXAGE:20110101 /MINAGE:20111231
Plus other parameters described on the "robocopy /?". Usually I add "/R:0 /W:0", for it not try to access systems files (can help if you run the batch file with administrator privileges) 1 million times with a wait time of 2 seconds for each system file it can't copy/move (2 million seconds or 23 days for just pagefile.sys and hiberfil.sys). And the "/A-:H" switch to un-hide the hidden files.
Keep in mind also the existence of NTFS Junctions (infinite loop in the C:\users directory) and encrypted directories and use the according switches.
This won't be very nice, but you could use forfiles twice. Once to move all files with a date greater than 2010-01-01 to the folder and a second time to move all files with a date greater than 2010-??-?? (can't parse your date format reliably) back to the original folder.
Not pretty, definitely.
Related
I have a batch file that runs a simple "burst" (a.k.a. split) multi-page PDF into single pages scripts, then moves them to a mapped network drive (Z:\). This batch file is triggered by the user and has been working without a hitch for at least two years. Last week, there was a flurry of Windows 10 (x64) updates and it stopped working. Now, instead of looping over every PDF in the source dir, the batch file will generate single pages for the first PDF in the iteration, then stops (although not showing any errors in output). I have tried a multitude of fixes, including using a full timestamp in the target file names to prevent overwriting, though I don't think that's the problem. It seems too coincidental that it just stopped working shortly after the updates (per my User). I'm not very experienced with batch files, and just drew this up based on a PDFTK example I saw. Here it is (not sure why line 2 is being split in the markup, but in the batch file lines 2 & 3 here represent a single line; PS the last line is also being weird, should read: del "C:\Users\My User\Desktop\PHYS_SRCDIR'BACKSLASH''STAR''DOT''STAR'" /F /Q):
cd C:\Users\My User\Desktop\PHYS_SRCDIR
for /r %%i in (*.pdf) do (pdftk "%%i" burst output "Z:\PHYSICALS_IN\%date:~10,4%%date:~4,2%%date:~7,2%_%%~ni_%%03d.pdf")
del Z:\PHYSICALS_IN\doc_data.txt /F /Q
del "C:\Users\My User\Desktop\PHYS_SRCDIR\*.*" /F /Q
I haven't seen any new responses lately, and have to divert my attention elsewhere, so my solution was simply to move the batch file to the server w/ "Z:" on it (allowing a limited login to the User), reverse it so it copies from User's PC mapped drive to the Server, and move on to the bigger fish I have to fry. I know, it's not really a solution but that is my circumstance. I'll check in from time to time to see if there are any other suggestions. Since the batch file works perfectly from a WS2012R2 box my only thoughts are that something in Windows 10 "broke" either PDFTK Server or batch files/command-line in general. Since it works singly and only breaks when trying to loop over several multi-page PDFs, I'm leaning toward batch files/CLI. Thanks for the suggestions, keep 'em coming!
I am attempting to write a series of scripts to copy a folder full of non-predetermined files and possibly subdirectories from a Windows 7/10 environment to a MS-DOS 7.0 environment (that's Windows 95 in DOS-Mode) running in Vanilla DOSBox 0.74-3. The copy process, which cannot be worked around or changed, destroys long file names. The software on the MS-DOS 7.0 end requires these long file names to run properly.
My plan is to have a Windows batch script with a simple dir /b /on /s > lfn.txt to keep track of all the long file names.
Then next step ideally, and here is where I am stumped, reads back through the lfn.txt and renames all the files from top to bottom to an increasing integer starting at 1 and never resetting. My goal is to be able to copy and tweak this script to work on MS-DOS where it does the reverse. If the solution, even conceptually, can be made that portable I would prefer that solution even if it is more verbose or inefficient.
I apologize if this has been asked and answered elsewhere. If it has please link to it before closing this thread.
I want to create a backup Batch file that meets these requirements:
Requirement:
1-Only copy the source files if the source file got modified.
2-If the destination contains the files/folders that do not exist in the source, then the destination files/folders will be deleted.
3-Copies all subdirectories, even if they are empty.
4-If something happened that make the coping process uncompleted (ie copied unsuccessfully), the program will revert & keep the old version of the destination folder.
5-The batch file should run at the time i turn off my PC or at 7pm Daily.
So, i tried this
xcopy C:\MyProject N:\backup\MyProject /V /Y /E /D
Note: /v=Verify, /y=No prompting /e=subdirs /d=Copy only changed
However, after running the batch, it did not delete the files/folders mentioned in requirement 2. Also, i don't know how to do the requirement 4 & 5.
I searched many questions but seem they didn't have the code to meet requirement 2 & 4 & 5.
I don't want to use backup tool in Win7 since it is very heavy & it requires a big backup disk space. I prefer something simple (why didn't Window make things simple?? ).
Backup is very important when u do the project, I hope my question will help a lot of other programmers.
Can anyone know a good solution for this?
If you want a mirror backup, which only copies new and changed files, removes files that no longer exist in the source - then Robocopy is built in and can do that. A batch file that is scheduled at 7pm daily will work.
Your requirement #4 is something that I haven't seen in any backup programs, including Robocopy.
I recently wrote a batch file like this for an old XP machine, since I didn't trust the format produced by NTbackup to be readable on future machines. E.g. Windows 7 and 8 require a download of some sort of converter: forget that. My batch file lives in the root C:\ directory, and I have a shortcut in my "tools" program list.
Write the file in Notepad, and save as e.g. c:\backup.bat. Run it from the "Run" command. The first time it runs, all the directories you specify will be copied exactly to your backup drive, including all subdirectories (I use a 4 Gbyte thumb drive: you may well need larger). The first time, my small 3 GB or so of stuff took about 4 minutes on an old XP machine.
Interestingly, the total uncompressed backup produced this way is no larger than the backup produced by NTbackup in Windows XP in that program's unreadable format.
Here's a batch file, with comments (REM)...
REM backup.bat
The xcopy line below is for stuff in directory c:\anydir and its subdirectories, being copied to a directory \anydir on an e: drive (e.g. USB drive).
REM The actual command:
xcopy c:\anydir e:\anydir /i /s /y /d
For other directories, just change the c:\directory and e:\directory names in the xcopy command. Watch out, however, for names like My Pictures, with a space: you must rename them (just delete the space), so that DOS recognizes them.
REM finish
exit
The /i means make any (sub)directories that don't exist on the backup drive. This therefore copies the whole branch from the c: directory where you start.
The /s copies source directories and subdirectories unless they are empty.
The /y says don't ask about over-writing existing files.
The /d says only copy files of a later date: perfect for backup, and very fast.
The files are exactly as original: no weird backup format.
With regard to the questioner's question #4, I think just running the backup again would overwrite any failure. But if that fails, wipe your backup drive and start over. And question #5...XP (and I think 7) allow scheduled tasks from the Startup menu: just insert c:\backup.bat in the list. But I haven't tried that.
I am trying to copy all .txt files that I have scattered throughout several subdirectories of one main directory into another directory using a batch file. I have research this site and found lots of answers at this link: batch file Copy files with certain extensions from multiple directories into one directory. Like the code below from Jay:
set dSource=C:\Main directory\sub directory
set dTarget=D:\Documents
set fType=*.doc
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
copy /V "%%f" "%dTarget%\" 2>nul
)
My question is how to modify this code or other codes on this link to batch copy the files with time stamps, like I only want to copy .txt files created from Jan 1, 2012 to Nov 1, 2012.
My suggestion for finding and moving *.txt files in a directory tree of a drive, or the entire drive, or even multiple drives with last modification date in a definite time period is:
Start Windows Explorer.
Click on button Search.
Open advanced search options for finding files and folders.
Select/enter to search for files by last modification date.
Enter the two dates to specify time period or select the time period.
Run the search.
Select all found files in search result, for example with Ctrl+A.
Press Ctrl+X to mark the found files for being cut (moved).
Open the folder into which the files should be moved.
Press Ctrl+V to paste the files (move them).
That's it.
Nobody needs to code a batch job for this task if this find + move files job should not be done periodically using a scheduling task.
The exact steps for doing such an advanced find for files in a definite time period with Windows Explorer depends on version of Windows. See for example the computer tips
Find files by date modified in Windows for Windows 8/7/Vista, or
for Windows XP: How do I search for a file on my computer?
And of course there are many freeware and shareware tools which support also finding files according to various search criterias like last modification date within a specified time period and move them.
Well, that does not really answer the question as it does not contain the batch code for doing the job. So I answer this question with another question:
Why thinking about coding a batch file for such a task hard to adapt for varying dates if dozens of GUI applications including Windows Explorer exist doing the same by simple user input with no need on coding skills and therefore very easy to use, and the find + move must be done only once or from time to time with changed criterias?
I have an Oracle archives folder on windows, which I need to take an incremental backup everyday at 6:00AM.
I need to copy all the files generated during the previous day, and place them in a folder with today's date.
What is needed is that, the files generated after the last backup was taken, only should be copied [i.e, the file names with the sequence after yesterday's backup's last file].
I tried xcopy, but it doesn't provide any facility for copying files based on modified time.
I need to write a batch script for this, please help me out!
xcopy provides methods for copying files based on their Archive attribute. The option you would likely want is /M, which copies only files with the Archive attribute set, and resets that attribute. It kind of relies upon the Archive attribute being set, but Windows does this by default (I think) when creating or modifying a file.
For example (a rubbish example, but an example nonetheless):
C:\tmp>echo hello > out.txt
C:\tmp>xcopy /M *.* ..
C:out.txt
1 File(s) copied
C:\tmp>xcopy /M *.* ..
0 File(s) copied
C:\tmp>echo hello > out2.txt
C:\tmp>xcopy /M *.* ..
C:out2.txt
1 File(s) copied
Only files that are new/modified since the last copy are copied.
Alternatively, depending on your Windows version, you could look into the much more powerful (and hence more confusing) robocopy.
Unless the database is shutdown, you can have data inconsistency problems if you attempt to backup the raw files on the disk.
A really great reference for all things backup is an O'Reilly title: Backup & Recovery
Each database will have it's own methods of running backups while the database is live. Here's Oracle's page.