Windows Batch scripting to copy files from last week - batch-file

I'm trying to automatically copy 5 .xlsx files from the server to my desktop. I will do this every Monday. The files are from previous week (Monday, Tuesday, Wednesday, Thursday, Friday)
My date scripting is not working. Can someone help me?
C:\Users\jgi>forfiles /P C:\Users\jgi\Documents /S /D +(today'date - 7 days)
ERROR: Invalid argument/option - '-'.
Type "FORFILES /?" for usage.

According to your requirements, you will need something like this:
#echo off
forfiles /P C:\Users\jgi\Documents /S /M *.xlsx /D -8 /C "cmd /c copy #file D:\Desktop\"
/P option specifies the directory to search for the files.
/S is used to say forfiles to search files in all subdirectories of location specified in /P.
/M specifies which files should be processed.
/D Selects files with a last modified date greater than or equal to (+), or less than or equal to (-).
/C specifies command to be run for each file found.
cmd /c tells system to open a new cmd which will will carry out the command specified by string and then it will terminate.
Now, copy #file (forfiles variable; returns the name of the file) to a custom destination.

Related

Command Line 3 days rolling backup using RoboCopy

what I would like to accomplish is have a three day rolling backup of the Source Folder, which will be modified every day. I want anything removed longer than 3 Days as worst case I will have to revert to the most recent backup but having that 3 Days allows me to revert back to a previous changes if need being.
What I have done in Command Line is as follows:
SET STAMP=%DATE:/=-% %TIME::=.%
SET SOURCE="C:\Users\PersonA\Documents\Test\Source"
SET DEST="C:\Users\PersonA\Documents\Test\Destination\%STAMP%"
robocopy %SOURCE% %DEST% /e
forfiles -p "C:\Users\PersonA\Documents\Test\Destination\" -s -m *.* -d -3 -c "cmd /c echo #file"
I have timestamped and concatenated the FolderName and Current Date and Time.
However the bit I am struggling with his removing anything longer than 3 Days, currently the process is just copying, but what I need is a process to detect modified date or perhaps created date and remove directory anything greater than 3 days with the code I produced. The deleted command I have added above doesn't seem to do anything at all.
If anyone can help I will be most grateful.
Thank you in Advance
I would recommend something like this:
SET STAMP=%DATE:/=-% %TIME::=.%
SET SOURCE="C:\Users\PersonA\Documents\Test\Source"
SET DEST="C:\Users\PersonA\Documents\Test\Destination\%STAMP%"
REM Set Robocopy to only retry once after waiting 1 second; copy all
REM file attributes, security, etc; and create a log file in the
REM temp folder with the same name as the date.
robocopy %SOURCE% %DEST% /e /r:1 /w:1 /COPYALL /LOG:%TEMP%\%STAMP%.LOG
C:
CD \Users\PersonA\Documents\Test\Destination\
REM Recursively and quietly delete all directories older than
REM 3 days
FORFILES /S /D -3 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
This is extrapolated from the answer given in this post.

Delete all *.Zip files older than today using Windows batch file

Assuming you must use a Windows batch file, (not powershell), and one wants to delete all files ending in .zip that are in the current active directory. How to do this?
All attempts so far are failing:
forfiles -p "C:\temp\test" -s -m *.zip -d 1 -c "cmd /c del *.zip"
For this it says
ERROR: No files found with the specified search criteria.
As suggested in my comment, your problem could easily be solved by reading the usage information for your command, (available when entering FORFILES /? at the Command Prompt).
Based on your questions criteria, "delete all files ending in .zip that are in the current active directory":
You don't need to use the /P option because as stated in the usage information, "The default folder is the current working directory (.)".
There is nothing in your question regarding recursing subdirectories of the current directory, so the /S option which "Instructs forfiles to recurse into subdirectories" is not required.
For the /D option you are looking for files with a last modified date less than yesterday, i.e. "the current date minus "dd" days", /D -1.
Because you're wanting to delete files in the current directory, there's no need to use the "Full path of the file", #path, so what you need is the "The name of the file", #file.
FORFILES /M *.zip /D -1 /C "CMD /C DEL #file"
You did not mention anything about subdirectories or windows version so I'm assuming somewhat. You have an old version syntax. In Windows 7 and further the syntax changed a little bit.
For windows 7:
forfiles /P "C:\temp\test" /S /M *.zip /D -1 /C "cmd /c del #path"

Batch file to start all documents 3 days old or newer in a folder

I am trying to create a batch file that will:
Start in a minimized state, all documents 3 days old or newer, in a folder.
It appears this must be a combination of the for, forfiles and start /min commands.
I have tried:
This script properly opens in my date range, the first document in the directory but will not open any more documents until I close the first document. It will not start them minimized:
forfiles /p "C:\Users\user\Documents\test" /s /d -3 /c "cmd /c #path"
This opens all documents at once, each in its own command prompt window but does not show the document in its appropriate default program, i.e. Microsoft Word for .doc files. It also does not open them minimized:
forfiles /p "C:\Users\user\Documents\test" /s /m *.* /c "cmd /c Start #path" /d -3
This opens all docs but not minimized or in my date range:
for %i in (c:\Users\user\Documents\test\*) do %i
I do not have a script that will open a documents in a minimized state.
Please help!
Many thanks!

forfiles with compact

I wanted to compress all the files under a directory which are older than today.
The command i am using is
forfiles /p E:\testbeg /s /d -1 /c "CMD /c Compact "
In the E:\Testbeg there are folders and files which are created today and some of them are older one.
I want to compress all the files which are older than today and keep today files as it is.
The following will compact files that have not been modified within the last day, but apparently it is not what you are looking for.
forfiles /p E:\testbeg /s /d -1 /c "CMD /c if #isdir==FALSE Compact /c #path"
Based on your comment, you want to compact all files that exist within folders that were created more than 1 day ago. I presume you also want to compact the folder itself so that new files that are added to it will automatically be compacted.
I don't think FORFILES will help, because it uses the "last modified date", not the "creation date". If you add or modify a file within a folder, then the folder's last modified date is updated.

How to copy all files created between two dates using Command prompt?

I have written code in window batch file (eq. getFiles.bat ) that get all files within select date range.
eq.
xcopy /S /D:01-10-2011 *.* C:\todaysFiles
But I want get all files in between two dates including From date and To date.
file extension is .cmd or .bat
If you're on Vista/Win7/WinServer2008 you can use robocopy like so:
robocopy c:\source c:\destination *.* /MAXAGE:20101231 /MINAGE:20111001
On XP, I'm not sure if there are built-in solutions short of using Powershell and the like.
The title of this question is slightly misleading. Based on the questioner's xcopy example, I believe the questioner wants to know "How to copy all files created...", not how to get all files (which I equate with list).
The questioner also states "file extension is .cmd or .bat". This could mean that the questioner wants to copy only files with these extensions but I think it means the questioner would like a batch script solution.
To use the following solution, open a command prompt and chdir to the folder where the files to be copied are located. Then enter the commands listed below, changing the SET values as appropriate.
SET DESTINATION=C:\destination
SET DATE_FROM=01/01/2005
SET DATE_TO=01/01/2007
> nul forfiles /S /D +%DATE_FROM% /C "cmd /C if #isdir==FALSE 2> nul forfiles /M #file /D -%DATE_TO% && > con ( echo #path && copy /V #path %DESTINATION% )"
Note: this will copy files in subfolders as well as files in the top-level folder.
The SET values could be hard-coded directly into the > nul forfiles... line, meaning only one line is required, but for clarity I've used variable substitution.
A caveat is that it is based on date modified (original question asked for date created)
Credit to aschipfl (https://stackoverflow.com/a/36585535/1754517) for providing the inspiration for my answer.
You can also use the forfiles command. It can search recursively in all subfolders /s for files created in the selected root folder /p <Path>, execute commands on the selected files /c "<Command>", apply masks to search /m <SearchMask>, between a date range /d [{+|-}][{<Date>|<Days>}]
more info here, and here
Here's a batch for viewing files by creation date by year. Easily changed to cmd prompt by removing the extra percentage symbols.
for %%a in (2011 2012 2013 2014 2015 2016 2017) do (
for /f %%i in ('xxcopy i:\podcasts\*.* /LL /ZS /Q /FC /DA:%%a-01-01 /DB:%%a-12-31 ^| find /c /v ""') do echo %%a: %%i
)

Resources