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
)
Related
I'm trying to create a batch file in Win7 that will copy any files that have been created or modified today and copy them to a destination with a similar directory structure. This is what I have so far:
set today="20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MINAGE:%today%
I know that /e copies empty directories and /xf excludes all files, but I'm not sure if that helps me. The code above seems to copy all files regardless of date, so I'm a little lost here.
Assigning quotes to your variables is not a best practice and will cause problems with some commands if you try to quote the variable later on. Regardless that was not your problem. Your problem is you need to use the /MAXAGE option. Reading the help file you should see this:
/MAXAGE:n : MAXimum file AGE - exclude files older than n days/date.`
So your code should be:
set "today=20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MAXAGE:%today%
Going to assume you thought the options were for INCLUDE.
robocopy's /MINAGE//MAXAGE options regard the full date and time, so specifying something like /MAXAGE:1 filters for files that have been modified within the last 24 hours.
If you want to process files which have been modified today only, hence regarding the date but not the time, you could use forfiles and its '/D' option, like this:
set "DEST=D:\backup\temp"
forfiles /P "C:\temp" /D +0 /C "cmd /C if #isdir==FALSE for %%Z in (#relpath) do #(2> nul md 0x22%DEST%\%%~Z\..0x22 & copy #relpath 0x22%DEST%\%%~Z0x22)"
I am a rookie at programming.
I have created a batch script to move files from one location to other based on date.
I want to move all files expect the files having modification date 3 days lesser than the current date.Also I want to log the files which has been moved in a new text document.
I am able to move the files but not able to log the moved files.
Command which i used is
forfiles /p C:\Users\Desktop\batchtest\ /s /m . /d -3 /c "cmd /c move #FILE \"C:\Users\Desktop\nov""
What i need is to create a textfile which shows the files moved by the above command.Could someone help me on this?
This is not same to Just deleting the file based on the date I suppose.
You could redirect all command output in to a file, like so:
forfiles /p C:\Users\\Desktop\batchtest\ /s /m . /d -3 /c "cmd /c move #FILE \"C:\Users\Desktop\nov"">log.txt
Notice the ">log.txt". That will redirect all non-error output into the provided file. It will create it if it isn't there. If you want to redirect errors, too, put a 2>log.txt >log.txt at the end instead. Source
I am trying to add the dates to copied files from one directory to another directory. This is how it should look like
Original file name: XEsalary.csv
Result file name: XEsalary-2013-02-15.csv
Here is my code:
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
cd %LOCAL%
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\*.csv"
:: assist with turning this into a for loop
ren %LOCAL%\archive\*.csv %LOCAL%\archive\*%PVM%.csv
echo. >>%LOG%\%PVM%.log
copying works just file. It is the renaming part which is not working. Any help please?
Thx in advance :-)
Using wildcards with RENAME is tricky. There is no good official documentation, but I have experimented and published rules as to how it works: See How does the Windows RENAME command interpret wildcards?
You can accomplish your rename with the following command, as long as none of your file names include dots (the last extension dot is OK).
ren "%LOCAL%\archive\*.csv" ????????????????????-%PVM%*
The number of ? must be greater than or equal to the longest name in the folder.
The result will be incorrect if a file name contains a dot. For example, part1.part2.csv would become part1-2013-02-15.part2.csv.
Another option is to use a FOR loop. You can safely append the date to any file using this technique.
for %%F in ("%LOCAL%\archive\*.csv") do ren "%%F" "%%~nF-%PVM%%%~xF"
BUT, both solutions have a problem if you rerun the process on a later date. New files will be added to the archive, but both new and old archive files will be renamed to the current date. That won't work.
Better to copy and rename the file in one step using a FOR loop as suggested in rojo's answer.
Or better yet, create a new archive folder with the date in the folder name, and then copy the files to the dated archive folder without renaming :-)
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
cd %LOCAL%
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
md "%LOCAL%\archive\%PMV%
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\%PVM%"
You might want to include the time in the folder name if the process could be run multiple times in the same day.
Windows doesn't let you rename with a wildcard. You have to name each file in a for loop. Might as well do the renaming as you're copying. Does this do what you intended?
#echo off
setlocal
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
pushd "%LOCAL%"
for %%I in (*.csv) do (
rem Uncomment the following line to log the copy to archive.
rem echo copy "%%I" -^> "%LOCAL%\archive\%%~nI-%PVM%.csv" >>%LOG%\%PVM%.log
copy "%%I" "%LOCAL%\archive\%%~nI-%PVM%.csv">NUL
)
echo. >>%LOG%\%PVM%.log
popd
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.
I'm trying to create a batch script that will delete all sub directories which are older than 30 days.
I'm really new to batch scripting so I'm just hoping that someone can help me out on this.
I was able to find a script that delete all sub directories in specified location.
#for /f "tokens=*" %%a in ('dir /s /b "C:/temp/test" 2^>NUL') do rd /s /q "%%a"
Can someone tweak this script so it deletes only directories which are 30 days old?
I think FORFILES is going to do it...
FORFILES -D 30 will iterate all files last modified over 30 days ago
So, something like...
FORFILES /S /D -30 /C "cmd /c IF #isdir == TRUE rd #path"
Should (untested) recursively remove all folders older than 30 days :)
Btw - Check out http://ss64.com/nt/ for more command line goodness
This is working fine for me:
Posted by Matt Roberts --> FORFILES /S /D -30 /C "cmd /c IF #isdir == TRUE rd #path"
Be careful when you start the ".bat" file with "Run as administrator", it's not using the current location of the file it takes this location --> "C:\Windows\system32"
For savety reasons add the /P "C:\temp"
FORFILES /P C:\temp /S /D -30 /C "cmd /c IF #isdir == TRUE rd #path"
Assuming you are restricted to batch files... check this - but if you can use VBScript, I think you can do a bit better.
Now that you have clarified your real problem, please understand that if you meant to use System.exec() you will have to contend with plenty of drawbacks.
Alternatively if you really want to use JNDI (you mention "native" but don't make clear what you mean) maybe this will be of help: http://mindprod.com/products1.html#FILETIMES
Do you have powershell on your system?
dir | ? { $_.LastWriteTime -lt [DateTime]::Now.AddDays(-30) } | % { rm $_.FullName }
I know this is an older topic; however, I was recently looking to do this and was able to by building off of what Matt Roberts suggested. I used the following FORFILES syntax:
FORFILES /P \path\to\dir /S /C "cmd /c IF #isdir == TRUE rmdir /S #path /Q" -D -30
You will need to replace \path\to\dir with the path (including drive letter) to the directory (/P) you want to run this on. This will then recursively (/S) search through the directory and run (/C) rmdir (again recursively). Without /Q you are asked if you want to delete each dir/file so if you are planning on running this silently in a script you will need to have it. You can change the amount of days (-D) by changing -30 to something else.
I then used Task Scheduler to automate this once every week (confirmed working). To make sure it works, set the task with 'run with highest privileges'
Note: This setup will irrevocably delete the files. They will not appear in your recycle bin. So once they are gone they're gone (unless you have backups).
You might want to consider using Windows Script Host, in which you could run a VBScript to do just that. Think of it like a batch file with more flexibility and power.
See http://www.computing.net/answers/programming/wsh-delete-files-in-folder-by-date/16255.html
This is not going to be a simple tweak. As it stands, there is no easy way (that I know of) to do the date-diff operation in the cmd interpreter.
First you'll have to parse the current date:
C:\>date /T
Thu 08/05/2010
Once you have the day, month, year, you have to get the date for each item in the directory listing, which you can do by removing the /b param in the dir command and modifying the tokens line to give you tokens 1 and 4, i.e. tokens=1,4, and then doing the date math yourself, which will be a pain because you have to handle different lenght months, and December -> January transitions (new years), etc, and you're almost certain to get it wrong.
Also add the /ad param to get just directories.
I would recommend using PowerShell or some other scripting technology that gives you the tools you need to manipulate dates.