XCOPY /d - does it support time as well as date? - windows-xp

XCOPY /d is documented to take a date in the form dd-mm-yyyy but I can't find any mention of similar support for time.
Is there (or a workaround even) or do I have to download another command line tool for winxp?

There's no way to include time as a criteria in XCOPY. This is true for ROBOCOPY as well, even though it has many more configuration options than XCOPY.
The XXCOPY application will do what you ask -- but as you say, this costs money (for commercial uses, anyway).
http://www.xxcopy.com/xxtb_001.htm - search for "/DA:".

Related

using powershell to only read last 1month of folders. copy in 7 days worth of folders then using robocopy

apologies to start as im new to powershell and robocopy.
i have a robocopy command that pulls in any files within its many subfolders that are within a maxage of 7. however, the main folder has a huge amount of folders dating back years(and i only need last 7 days each week it runs) so its slow reading each file in each folder before it even copies using robocopy.
it looks like powershell commands may be a way for me to limit the search of files for my robocopy, would this be possible? currently robocopy search each files in each folder in my main folder, ideally i would want it to be smart enough to only search even a months worth of files and then copy over last 7 days. this would speed up the run time hugely.
if possible even further, i only want csv files in each of the folders in my main folder but current robocopy searches the other folders and its files as well which takes time. all the csv files are in a folder called "run" in each parent folder(parent folder is a unique number within the "mainfolder".
my robocopy command:
robocopy \\server\mainfolder \\server\new_main_folder /S /maxage:7 /r:0 /w:0
I was going to point to you either FastCopy or FreeFileSync, both handle long file name paths and work well for me. But found problems running FastCopy when trying to filter folders the way you described. I wasn't getting the results I expected, so that leaves FreeFileSync. There is a little bit of a learning curve with FreeFileSync, but really, the only problem/complaint I've had with it is the xml based batch script that you can use to automate the program kept changing formats and they haven't been providing a way to read the old xml batch scripts with the new version of the software. Maybe that has changed, I haven't looked into that lately.
Maybe other people have had better experience with RoboCopy, but I found it to take literally many multiples longer to do the same job as many other copy programs. I don't think FreeFileSync is as fast as FastCopy, but I've never seen it act as bad as what I experienced with RoboCopy.
The way FreeFileSync works is:
You define 1 or more source/destination pairs.
There is a global setting at the top to set the defaults for all copy pairs.
There are individual settings per each copy pair that when set override the global settings.
In the filter tab of the settings you can set "Time span:" to "Last x days:" and set it to the 7 days that you want.
You can change include from * to something like \run\*.csv. I didn't try that exact pattern, but the patterns I did try worked as expected (Unlike FastCopy).
The Synchronization tab is the tricky/fun one. You can do logs, versioning, tell the system to shutdown or restart when done, maintain a database for tracking moved files ("Detect moved files" checkbox), and all kinds of adjustments to how it behaves when files don't match.
When done, there is I believe at least 2 options for saving the configuration - though I've always just created the xml based batch script and called that from another scripting language or an icon on the desktop.

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

Batch script to move zip files based on year modified

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"

Create new power plan

I have found all sorts of references to using PowerShell to change the active power plan, and I have found instructions for manually creating a new power plan, but I can't seem to find anything about using Powershell to automate the creation of a new plan. Is this something that can be done, and I need to keep looking? Or am I not finding it because it can't be done?
And, a little context, I am automating the setup of lab machines for a three day conference. The machines come from various vendors, and I have no idea nor control over what settings their Windows image is going to provide. Usually laptops are set to power down the screen at 10-15 minutes, which is crazy for a lab, where you will often go more than that listening to instruction, then when you go to try something you need a password. My goal is to have a script create a new power plan with the settings I want, and then a second script that makes that plan current for the user. I also need to make this work in PSv2 as 99% of the time we get Windows 7, and I am not in a position to demand a PS update. Eventually we will automate the OS install too, and eliminate a few more variables, but for now we are working with the OS image we get.
Apparently you need to wrap powercfg calls in your script to produce a power plan modification. One good thing that you can call powercfg -import <file> <GUID>, and you can prepare the file by setting correct parameters on a test PC and call powercfg -export with a given plan. So you just create a .bat file with a power plan export result, and call that at startup to set the power plan. You can also modify current power plan by calling powercfg -x. See powercfg -? for details.
Well I had to look into a few different places to solve exactly this problem and I came up with this following little script that does exactly this but using a batch file instead
#ECHO OFF
SET "src_profile=High performance"
SET "new_profile=DAW Optimised"
:CREATE_PLAN
REM Create new power plan based on the existing one specified, rename it and specify settings
echo Setting up new power plan
for /f "tokens=4" %%f in ('powercfg /list ^| findstr /C:"%src_profile%"') do set GUID=%%f
for /f "tokens=4" %%I in ('powercfg -duplicatescheme %GUID%') do set dest_GUID=%%I
powercfg /changename %dest_GUID% "%new_profile%"
powercfg /setactive %dest_GUID%
I'm sure that conference is long over but hopefully this helps someone in a similar situation

Batch file: Get yesterday's date in format: M_d_yyyy

How does one get yesterday's date format in a batch file?
I'd like it to look like so: M_d_yyyy
Note that if there's a single digit day and month, I'd like it to be single digits.
Example: 8_5_2013 is August 5th, 2013.
I looked around for a few days but couldn't find a solution.. any lead is much appreciated.
Nothing wrong with free 3rd party executables, but some of us are not allowed to use them on our work machines.
I have written a powerful hybrid JScript/batch utility called getTimestamp.bat that can do nearly any date and time computation on a Windows machine.
There are a great many options for specifying the base date and time, many options for adding positive or negative offsets to the date and time, many options for formatting the result, and an option to capture the result in a variable. Both input and output can be directly expressed as local time, UTC, or any time zone of your choosing. Full documentation is embedded within the script.
The utility is pure script that will run on any modern Windows machine from XP forward - no 3rd party executable required.
Assuming getTimestamp.bat is in your current directory, or better yet, somewhere within your PATH, then the following simple call will define a dt variable containing yesterday's date in M_D_YYYY format:
call getTimestamp -od -1 -f {m}_{d}_{yyyy} -r dt
Note: when I put a date in a file name, I like to use YYYY_MM_DD format because that format will sort chronologically when getting a directory listing.
I think you should get date.exe from UnxUtils.
date.exe --date="1 day ago" "+%-m_%d_%Y"
Download: http://sourceforge.net/projects/unxutils/files/unxutils/current/
Man page: http://www.ss64.com/bash/date.html
#echo off
setlocal
set magic="c:\unx\usr\local\wbin\date.exe" --date="1 day ago" "+%%-m_%%d_%%Y"
for /f %%i in ('%magic%') do set yesterdate=%%i
echo yesterdate = %yesterdate%
If you want to do it with just batch language, you'll end up with nearly 100 lines of incomprehensible batch code. UPDATE: or use dbenham's hybrid batch/JScript solution posted in the answer below, which at least uses sane Windows APIs.
See Also:
How to get current datetime on Windows command line, in a suitable format for using in a filename?