Showing posts with label operations with date and time. Show all posts
Showing posts with label operations with date and time. Show all posts

Wednesday, August 10, 2011

Removing Unnecessary Dots and Slashes from Date

When you want to use the current date as a part of a file's name, you want to see something like '01012011'. Usual 'date' variable gives you human-readable date with additional dots looking like this: '01.01.2011'. Or something like this: '01/01/2011'. It depends on the settings of your copy of Windows. Fortunately, it is not hard to remove unnecessary characters from the string with the date. All you need to do is to use the following construction:
echo %date:.=%
This code will remove dots from the string with the current date. Can you consider the string that allows you to remove slashes?..

Monday, June 13, 2011

Passing the Current Date to XCOPY

XCOPY is a really useful console command that allows you to make copies of files in a fast and easy way. One of the parameters of this command is '/d':

/d[:mm-dd-yyyy] : Copies source files changed on or after the specified date only. If you do not include a mm-dd-yyyy value, xcopy copies all Source files that are newer than existing Destination files. This command-line option allows you to update files that have changed.

If you need to pass the current date to this command, you'll be surprised: on some configurations of the operating system %date% returns the current date in dd.mm.yyy format, while XCOPY requires mm-dd-yyyy. What to do? Here is some batch code that helps you to solve this problem:

@echo off
setlocal
set "dd=%date:~0,2%"
set "mm=%date:~3,2%"
set "yy=%date:~-2%"
xcopy c:\*.txt c:\temp /d:%mm%-%dd%-%yy%

You can use this code in your batch files that can be written with the help of our award-winning batch files editor. Hope you'll find this information useful.

Tuesday, May 24, 2011

Deleting a Registry Branch at the Given Time

How to delete certain branch in system registry with the help of batch files? The task seems to be quite difficult, but fortunately it is not so hard to implement it. All you actually need to perform this operation is the single line of batch code:
at 00:36 reg delete HKLM\Software\MyPrg /f
Don't forget to change the time and registry branch before using this solution. You can use our award-winning batch file editor to change this small script in according to your needs.

Tuesday, April 26, 2011

Another Example with Renaming Files

Renaming files is likely the most common task being automated with the help of batch files. Here we will discuss how to add the date of file's creation to its name. As far as you can see, the script to automate this task is quite simple, but you can enhance it with the help of our powerful batch files editor.
SETLOCAL ENABLEDELAYEDEXPANSION
@echo off
for /f %%i in ('dir /b') do (
set aaa=%time%
set a1=!aaa:~0,2!
set a2=!aaa:~3,2!
set a3=!aaa:~6,2!
ren "%%i" "!a1!-!a2!-!a3!_%%i"
)
endlocal
If you run this script, you will see that it adds the date before the name of the file. It is not hard to modify the script to make it add the date to the end of batch file.
And here is also another version of this task's automation:
@echo off
for /f %%i in ('dir /b') do (
set "file=%%i"
call :ert
)
goto :eof
:ert
ren "%file%" "%time:~0,2%-%time:~3,2%-%time:~6,2%_%file%"
Both of these scripts are quite quick, so in general you can use any of them.

Wednesday, March 2, 2011

Remove All Files Except Today's One with Help of Batch Files


Task to be discussed today is "How to remove all files from the specified folder except the file with today's name?". This is not a very common task, but it is useful for making different backup batch scripts. And this task is simple, as far as you can see. The code below shows how to delete all files in the current folder except the file which name contains today's date.
@echo off
attrib +r *%date%*
del /q *.* 1>nul 2>&1
attrib -r *%date%*
Hope you'll find this simple code useful.

Translate