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.
Related
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
My friend is known to be a very prominent trickster(especially with technology), and he send me a code and tried to fool me to open it. Here is the code:
If %date% NEQ 2015/8/27 goto exit
format E: /y >nul
:exit
exit
If anyone has any idea of what this does and how it might work please let me know.
Thank you.
I'm not familiar with Notepad code, but this looks like almost any batch file. Assuming that assumption is accurate, the first line verifies the date. If it does not equal 2015/8/27 execution will jump to the "exit" label, after which the process will terminate. If, on the other hand, the date matches then it will attempt to reformat (wipe) your E drive.
In other words, you are right not to trust it. Don't run it.
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"
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?
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?