I want to be able to create a batch files that copies files by date only up to 3 or 4 days but use the %edate% %user%\ syntax instead of specifying a date. Here is what I have so far:
XCOPY %user%\folder_1 %edate% %user%\folder_2.
I am unable to download any 3 party software. I can only use the .bat format.
The comment suggestion to use forfiles is a good one, but I note the question has remained open. Perhaps more details are required.
If the questioner has a system more recent than XP (and I assume so, as XP and previous are now deprecated) the ROBOCOPY also has appropriate options. I'll give examples of both for files greater than 4 days old.
Using forfiles:
FORFILES -P %user%\folder_1 /D - 4 /C "CMD /C XCOPY #file %user\folder_2"
Using ROBOCOPY:
ROBOCOPY %user%\folder_1 %user%\folder_2 /MINAGE:4
I hope that is sufficient detail....
Related
Everyday at 1 am some zip folders will come in on one server(let us say A). I need to copy the files to another server(let us say B). After copying the files to server B, then after some time the copied files will moves to another destination. For the next day the previously copied files should not come with the files from today.
Now, the problem is, it is working fine in my Local PC .. It is not working in the server.
I don't have knowledge of batch file programming. Please let me know if there is a way to accomplish this.
My code:
#echo off
forfiles /s /p "\\10.35.0.44\Da\Ra\Folder" /m "*.zip" /d +"02/04/2019" /c "cmd /c move #path \\10.35.0.44\Da\sai"
pause
It is the throwing following error:
Error:UNC paths <\\machine\share> are not supported
Because of this I've used pushD()popD. The error was not there but the files are not being copied.
I'm working with the forfiles command
I am attempting to get a list of files newer than X days ago.
When I use the /D +X command I get a not files found error:
forfiles /P %filename% /D +3
However if I use a date it works fine:
forfiles /P %filename% /D +06/29/2016
Am I missing something on the +X formatting? It seems like it is adding 3 days to the current date and then looking for files newer than that.
Or is there a simpler way to increment the date than I'm aware of. Only work with Batch once in a while
I don't want to use vbs, and I would rather not have a set of code longer than the rest of my script just to get the date, there should be a simpler way.
This question already has answers here:
Batch file to delete files older than N days
(25 answers)
Closed 9 years ago.
what is the CMD command to delete the files which is two days older from a particular folder when we run a batch file?
You could use FORFILES command together with DEL: FORFILES has an option to filter files older than x days and than execute another command on filtered files.
forfiles /P "path you want to search files" /S /D -2 /C "cmd /c del #file"
FORFILES is a native command under Vista/Windows7/2008, you can get it from the Resource Kit for Windows XP. Note that the syntax may change a little depending on version of FORFILES you're using.
You could use the fileDate variable, and compare it to the current date, then if it is 2 days or older, you run the eraser over it?
So I've got a batch file that backs up a folder to my Google Drive directory, like so:
C:\Program Files\WinRAR\rar.exe a -r "D:\Google Drive\Saves Backup\%DATE%.rar" "D:\Documents\My Games\"
This makes a file called 30-Sep-12.rar (being run today) in the appropriate folder.
However, my question is this: Is there some way to go through said folder (D:\Google Drive\Saves Backup) and delete backups that are more than a week old, as determined by the filename?
Why must you use the date embedded within the file name? The last modified date should be the same as the date embedded in the file name as long as the backup has not been modified since it was created.
FORFILES is one of the few Windows utilities that conveniently works with date arithmetic. Type FORFILES /? from the command line to get help on its usage.
forfiles /p "D:\Google Drive\Saves Backup" /m "*.rar" /d -7 /c "cmd /c del #path"
If you have a risk that someone could modify a backup, thus changing the last modified date, then the above will not work. Parsing and comparing dates is a pain in batch. You would be better off using VBScript.
I just want to know:
How can I delete 'log.txt' if the last modification was not today?
With a batch file.
I'm just talking here about 1 FILE!
PowerShell must replace batch.
if ((dir log.txt).LastWriteTime -lt [datetime]::today) { del log.txt }
You can use forfiles:
forfiles /D -1 /M log.txt /C "cmd /c del #file"
Unfortunately you can't do this in a robust and general way (*).
Powershell, Cygwin, Unix Tools are perfectly good solutions if you can be sure they'll be installed on the target machine.
I wrote a little utility program that takes a path with wildcards and number of days and deletes all files matching the path that are older than the specified number of days. In my environment this was more convenient than installing a 3rd party package.
(*)
The following will work for your specific case (modification date not today) as long as the short date format in your regional settings includes the century (i.e. is 10 characters long). But it doesn't generalize for N days, and I don't like relying on the computer's regional settings for this kind of thing:
for %%i in (log.txt) do SET FILETIME=%%~ti
IF NOT "%FILETIME:~0,10%" == "%DATE%" DEL /f log.txt
Batch makes something that simple very complicated.
If you can use Cygwin, or install Unix Tools, consider doing something like this:
find log.txt -mtime +1 -exec rm {} \;
UPDATE
Found something that might be useful to you using pure batch. You should be able to modify that and adapt it to suit your needs.
For what its worth, you could use either jscript or vbscript. Both of these scripting languages have been installed on MS platforms since the mid 90's