Batch script to move zip files based on year modified - date

I realize there are a lot of questions already answered about doing something similar. I have researched what I am trying to do and didn't find anything that seemed to apply to what I am trying to do. There are a few factors combined in what I am trying to do and I don't know a lot about batch scripting.
First, I need to look at zip files with a certain naming scheme only. The way these are named is file1234.zip, file2345.zip, file3456.zip, etc... This naming scheme is automatically generated and already in place. The number in the name has nothing to do with the date it was modified or created. There are other zip files in the source directory I would want to ignore. This can be solved with wildcards. "file*.zip"
Next, I need to move only certain files fitting the above criteria, that were modified within a specific year. i.e. move zip files modified in 2000 but leave all other files alone. The year the files were modified would be a constant and would be designated in the script.
When combined, if file2345.zip was modified in 2000 and the other files were last modified in other years, then file2345.zip would be moved and all others would be ignored.
What I have learned:
Wildcards are valid characters in the middle of a file name.
To get the dates modified for all files matching my naming criteria
forfiles /M file*.zip /C "cmd /c echo #file #fdate"
With the above, how do I look at just the four-digit year?
I have no objections to using xcopy or robocopy for the actual file move.
Any suggestions or ideas would be greatly appreciated.

Assuming this command dir file*.zip |find /i "file" shows something like this:
18/08/2015 11:58 45,617 file2456.zip
18/08/2015 11:58 156,789 file36789.zip
then this code should work:
#echo off
md "d:\new folder" 2>nul
for /f "tokens=3,*" %%a in ('dir file*.zip ^|find /i "file" ^|find "/2000 "') do move "%%b" "d:\new folder"

Related

Date Comparisons in Batch - Looking for advice

I'm trying to build a batch script that will iterate over a set of folders and give me the most recently modified file (and later check if the date of that file is 180 days old). Right now, what I'm working with is this:
setlocal EnableDelayedExpansion
for /f %%f in ('dir /b /s /od /tw "<some UNC path>\*.*"') do (
REM Make sure the variable is defined the first time we try to compare it
if NOT DEFINED fileDateTime set fileDateTime=%%~tf
if %%~tf LEQ !fileDateTime! (
set fileDateTime=%%~tf
set fileName=%%nxf
set filePath=\\%%~pf
))
This does not work.Specifically, everything except the comparison at the start works. It seems completely arbitrary how the computer parses the LEQ; I can't find a consistent pattern. It definitely isn't comparing two dates with each other.
The variable !fileDateTime! always has the same format as %%~tf, basically by definition. But Batch doesn't know what to do with it, or rather, I'm not sure how to tell Batch what to do with it.
I have tried using ForFile, but the path in question is a network share, so it fails (and for some reason the workaround with Net Use doesn't like to work either.)
Is there an easier way to get the most recent file in a folder and check how old it is?
(Also, the server holding the UNC path is a linux server, so if this is substantially easier in Bash, I could do that too.)
EDIT: if anyone is wondering how I fixed this, my solution was "realize that there's absolutely no reason I should be doing this is batch to begin with, install Python on the server, and script it in that instead." This will now be my go-to solution for batch problems in the future. But thank you all very much for the advice, which definitely would have helped if I didn't change tack.
The date you get is a string, not a date object like you'd get in an object-oriented language.
And the LEQ operator can only compare integers, not arbitrary strings.
Worse still, the date string you get is in a format that depends on your OS localization, AND on user preferences.
To do a meaningful comparison, you have to first convert your date strings to a julian date (An integer counting the number of days since an initial reference date.) Then compare those integers together.
For that I recommend that you use the :jdate function there:
https://www.dostips.com/DtCodeCmdLib.php#Function.jdate

Compare different versions of the same directory (by date modified)

This is a multi-part question. I can fill in details once I get to a working prototype.
Situation: Due to a comedy of errors, I have three copies of a very large directory, each copy has some new files/versions of files that are unique. I would like to combine these, keeping the newest version of every file.
Breakdown of things I don't know: How to compare, recursively, directories to one another (probably going to do two at a time; 1 vs 2 = 1+2, then 1+2 vs 3 = 1+2+3). Step crucial to this, how to use the path/filename of a file in directory 1 to first see if it can be found in directory 2, then, if found, use date modified to determine whether to make a copy from 1 or 2 to the new combined directory.
I think with these 3 pieces of information (recursively compare files b/t two directories, by path, and by date modified), I can piece together how to script this. While I can look up these bits separately, it's going to be tough to convince myself this process was done correctly and I'd like to have a little help with the actual assessment/moving step so I have less worry that I've overlooked some small but crucial detail.
Will post the script when I have it put together, along with any caveats about my confidence in it.
Don't waste time writing a script when robocopy is built for file copying and has enough options to cover pretty much any situation...
By default it will only copy a file if the source and destination have different time stamps or different file sizes.
Using /XO will exclude older files that differ, so you will only end up with the newest files in destination.
/E includes subfolders inc empty ones, change to /S to not include empty.
robocopy C:\source1 C:\destination /E /XO
robocopy C:\source2 C:\destination /E /XO
[etc]

Xcopy transfer not working

I am using the xcopy command to copy files from one directory to another.
I am saving in the text editor as a .bat file and then running as admin.
Here is the command.
xcopy C:\Users\Public\Public Documents\Saved Spectral Files Z:\Contamination_Control\MOC Measurements\MOC Plates Data\Perkin Elmer Data Back-Up\ /d /f /p
The problem is it doesn't work. Not even a notification that anything has happened.
Any help would be appreciated.
Thanks,
N
You have to insert quotation marks around the two file paths, because without them, in my experience with xcopy, doesn't recognize the files. Sidenote: the same applies to the copy function.
As an add-on to the previous answer:
The problem is your empty spaces " " within the paths. For the command to know where one path starts and ends, always use the quotation marks.

Move batch file on specific date

I'm new to batch. I want to move a file hello.bat to the startup folder, but only on a specific date.
How do I insert "if then" statements (e.g. If "date" Then "execution")?
Furthermore, how do I move a file?
I've tried this using what I've gathered from Google:
If %date% NEQ 2015/12/25 goto asdf
move c:\Users\USERNAME\AppData\hello.bat
c:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start
Menu\Programs\Startup
:asdf
It doesn't seem to be working, however - the move part works fine, but when I insert the If statement, it doesn't compile.
Can someone offer me a solution to this problem? I feel I would learn more from an example than reading something online.
The %date% variable is different depending on your system settings. To check the format of %date%, run the following command in a cmd window echo %date%.
In my system, the date format is Day 00/00/0000. So the following would be needed (string manipulation to remove the first four characters of the date).
if "%date:~4%" NEQ "12/25/2015" goto asdf
As a side note; you can simply goto :EOF (End Of File) if you just want the script to end.

how to remove a few lines from a Unicode registry file using batch commands in Windows?

I have a program who's generating some data in registry.
I save it with "reg export HKCU\Software\ProgramName\Data data.reg" (Unicode format).
I need to take it to other computer and import it there so the program from that computer could use the data.
But I have to remove some text lines from data.reg. The text lines are easy to find because they contain some strings (for example paths to exe and dlls, specific program settings like "name1=value1", "name2=value2",...).
Now I'm doing this manually (using Wordpad) every few days but maybe there is another way...
Oh and I can't install other programs on these computers (the access is restricted) so I have to use batch/cmd files.
What I tried so far:
- redirecting the export to "con" but is visual only not in a variable;
- using "for /F ..." but this works only with ANSI and removes blank lines.
The lines must be removed before importing because the settings of the program from the first computer must not be loaded into the registry keys of the program from the second computer.
Can somebody please help me...?
Thank you.
Use this code to loop the file contents line by line
FOR /F %z IN (yourfile.reg) DO ...
Then use conditionals to determine if this line is one you want to keep
IF (%z)==[put your key string here]
If so (or if not), then write that variable to the target file
#echo %z >> output.reg
You can replace the filenames with command line arguments, use %1, %2, etc.
All together, then:
FOR /F %z IN (%1) DO IF NOT (%z)==[skip this line] #echo %z >> %2
Since you didn't give some critical details, I am making some assumptions here. Further reading can be found at this excellent resource: http://www.robvanderwoude.com/batchfiles.php
An example would be nice, but could you use reg delete to delete the keys/values you want after importing your .reg file?