Creating a folder with today's date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jc001
Posts: 5
Joined: 06 Dec 2018 12:09

Creating a folder with today's date

#1 Post by jc001 » 06 Dec 2018 12:25

Hi

Is it possible in the code below to have the folder that is created to be today's date as the folder name?
At the moment it's copying files and folders to a folder C:\Temp\test\backup\
but I would like the copied contents to go to a folder with the date structure yyyy_mm_dd, e.g C:\Temp\test\backup\2018_12_06 (or whatever the date is that it ran on)

I cannot work out how edit the line that creates the folder as today's date.

Code: Select all

'==================================================================================================

'######################## DELETE OLD BACKUP LOG
del C:\Temp\test\backuplogs\backuplog.txt

'######################## WRITE A LINE IN LOG FILE WITH TIME AND DATE STARTED AND LOG THIS TO FILE
echo C DRIVE %DATE% Starting at %TIME% >>C:\Temp\test\backuplogs\backuplog.txt 2>&1

'######################## WRITE A LINE FOR START OF LOG AND LOG THIS TO FILE
echo Copying files from C DRIVE >> C:\Temp\test\backuplogs\backuplog.txt 2>&1

'######################## STATE WHERE THE SOURCE AND DESTINATION IS FOR COPYING AND LOG THIS TO FILE AND COPY
For /D %%K IN (C:\Temp\test\source\*) DO xcopy "%%K" "C:\Temp\test\backup\%%~nK" /D /E /C /R /H /I /K /Y >>C:\Temp\test\backuplogs\backuplog.txt 2>&1

'######################## WRITE A LINE FOR COPYING OF LOG AND LOG THIS TO FILE
echo Copying log >>C:\Temp\test\backuplogs\backuplog.txt 2>&1

'######################## COPY THE LOG TO THE DESTINATION FOLDER AND LOG THIS TO FILE
xcopy C:\Temp\test\backuplogs C:\Temp\test\backup\backuplogs\ /D /E /C /R /H /I /K /Y >>C:\Temp\test\backuplogs\backuplog.txt 2>&1

'######################## WRITE A LINE IN LOG FILE WITH TIME AND DATE ENDED AND LOG THIS TO FILE
echo C DRIVE Backup %DATE% Finished at %TIME% >>C:\Temp\test\backuplogs\backuplog.txt 2>&1

'######################## TIME AND DATE CODE
REM find date and time
SETLOCAL
FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :fix_date %%G %%H %%I %%J)
   goto :copy_log
:fix_date
   if "%1:~0,1%" GTR "9" shift
   FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
       Set %%G=%1&set %%H=%2&Set %%I=%3)
   goto :eof
'######################## COPY THE LOCAL LOG FOLDER TO THE REMOTE DRIVE AND NAME WITH DATE
:copy_log
   copy C:\Temp\test\backuplogs\backuplog.txt C:\Temp\test\backup\backuplogs_C_BACKUP_%yy%%mm%%dd%.txt
   ENDLOCAL
:eof
'######################## REMOVE LOCAL BACKUP LOG
del C:\Temp\test\backuplogs\backuplog.txt
Last edited by Squashman on 06 Dec 2018 12:32, edited 1 time in total.
Reason: MOD EDIT: Please do not make the text smaller to read. And please use code tags.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Creating a folder with today's date

#2 Post by Squashman » 06 Dec 2018 12:49

I would assume you are aware of the %date% and %time% variables. But those are both region dependent and may not output the same on everyone's computer.

So the other option is to use WMIC to get the date and time because it always outputs in the same format on all computers regardless of region settings.

Code: Select all

REM GET DATE and TIME
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
SET "YYYY=%dt:~0,4%"
SET "YY=%dt:~2,2%"
SET "MM=%dt:~4,2%"
SET "DD=%dt:~6,2%"
SET "HH=%dt:~8,2%"
SET "min=%dt:~10,2%"
SET "sec=%dt:~12,2%"

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Creating a folder with today's date

#3 Post by Ed Dyreen » 06 Dec 2018 13:02

Squashman wrote:
06 Dec 2018 12:49
So the other option is to use WMIC to get the date and time because it always outputs in the same format on all computers regardless of region settings.
I get, "please wait while WMIC is being installed." when I run it for the first time. This is a bummer, when a users selects WMIC to be installed during setup, it should just install it not just put it in some post install limbo state. Probably windows just cannot do this during that stage, but they could run wmic in the background when a user logs in for the first time so it always behaves as expected. So when I require wmic, I set a switch in a file named %first_MAC_with_gateway%.tmp so I can determine if wmic ran on this pc before.

I use 'HKCU\Control Panel\International' 'sShortDate', 'sTimeFormat', 'sDate', 'sTime'. because it always adapts the outputs to the used format so it is always correct regardless of region settings.

jc001
Posts: 5
Joined: 06 Dec 2018 12:09

Re: Creating a folder with today's date

#4 Post by jc001 » 06 Dec 2018 13:04

Squashman - Many thanks for the quick reply. However, I am not familar with these. This code was written by someone else and left with comments in but I have substituted the following lines of code is this correct? Also what do I need to change to make a folder with the current date?

Code: Select all

SETLOCAL
FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :fix_date %%G %%H %%I %%J)
Changed to this:

Code: Select all

REM GET DATE and TIME
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
SET "YYYY=%dt:~0,4%"
SET "YY=%dt:~2,2%"
SET "MM=%dt:~4,2%"
SET "DD=%dt:~6,2%"
SET "HH=%dt:~8,2%"
SET "min=%dt:~10,2%"
SET "sec=%dt:~12,2%"
Is this correct?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Creating a folder with today's date

#5 Post by Squashman » 06 Dec 2018 13:42

My teaching style is to teach a man to fish. It really helps to know all the commands you can use within a batch file.

You can type help at a command prompt to get most of them.

Code: Select all

H:\>help
For more information on a specific command, type HELP command-name
ASSOC          Displays or modifies file extension associations.
ATTRIB         Displays or changes file attributes.
BREAK          Sets or clears extended CTRL+C checking.
BCDEDIT        Sets properties in boot database to control boot loading.
CACLS          Displays or modifies access control lists (ACLs) of files.
CALL           Calls one batch program from another.
CD             Displays the name of or changes the current directory.
CHCP           Displays or sets the active code page number.
CHDIR          Displays the name of or changes the current directory.
CHKDSK         Checks a disk and displays a status report.
CHKNTFS        Displays or modifies the checking of disk at boot time.
CLS            Clears the screen.
CMD            Starts a new instance of the Windows command interpreter.
COLOR          Sets the default console foreground and background colors.
COMP           Compares the contents of two files or sets of files.
COMPACT        Displays or alters the compression of files on NTFS partitions.
CONVERT        Converts FAT volumes to NTFS.  You cannot convert the
               current drive.
COPY           Copies one or more files to another location.
DATE           Displays or sets the date.
DEL            Deletes one or more files.
DIR            Displays a list of files and subdirectories in a directory.
DISKCOMP       Compares the contents of two floppy disks.
DISKCOPY       Copies the contents of one floppy disk to another.
DISKPART       Displays or configures Disk Partition properties.
DOSKEY         Edits command lines, recalls Windows commands, and
               creates macros.
DRIVERQUERY    Displays current device driver status and properties.
ECHO           Displays messages, or turns command echoing on or off.
ENDLOCAL       Ends localization of environment changes in a batch file.
ERASE          Deletes one or more files.
EXIT           Quits the CMD.EXE program (command interpreter).
FC             Compares two files or sets of files, and displays the
               differences between them.
FIND           Searches for a text string in a file or files.
FINDSTR        Searches for strings in files.
FOR            Runs a specified command for each file in a set of files.
FORMAT         Formats a disk for use with Windows.
FSUTIL         Displays or configures the file system properties.
FTYPE          Displays or modifies file types used in file extension
               associations.
GOTO           Directs the Windows command interpreter to a labeled line in
               a batch program.
GPRESULT       Displays Group Policy information for machine or user.
GRAFTABL       Enables Windows to display an extended character set in
               graphics mode.
HELP           Provides Help information for Windows commands.
ICACLS         Display, modify, backup, or restore ACLs for files and
               directories.
IF             Performs conditional processing in batch programs.
LABEL          Creates, changes, or deletes the volume label of a disk.
MD             Creates a directory.
MKDIR          Creates a directory.
MKLINK         Creates Symbolic Links and Hard Links
MODE           Configures a system device.
MORE           Displays output one screen at a time.
MOVE           Moves one or more files from one directory to another
               directory.
OPENFILES      Displays files opened by remote users for a file share.
PATH           Displays or sets a search path for executable files.
PAUSE          Suspends processing of a batch file and displays a message.
POPD           Restores the previous value of the current directory saved by
               PUSHD.
PRINT          Prints a text file.
PROMPT         Changes the Windows command prompt.
PUSHD          Saves the current directory then changes it.
RD             Removes a directory.
RECOVER        Recovers readable information from a bad or defective disk.
REM            Records comments (remarks) in batch files or CONFIG.SYS.
REN            Renames a file or files.
RENAME         Renames a file or files.
REPLACE        Replaces files.
RMDIR          Removes a directory.
ROBOCOPY       Advanced utility to copy files and directory trees
SET            Displays, sets, or removes Windows environment variables.
SETLOCAL       Begins localization of environment changes in a batch file.
SC             Displays or configures services (background processes).
SCHTASKS       Schedules commands and programs to run on a computer.
SHIFT          Shifts the position of replaceable parameters in batch files.
SHUTDOWN       Allows proper local or remote shutdown of machine.
SORT           Sorts input.
START          Starts a separate window to run a specified program or command.
SUBST          Associates a path with a drive letter.
SYSTEMINFO     Displays machine specific properties and configuration.
TASKLIST       Displays all currently running tasks including services.
TASKKILL       Kill or stop a running process or application.
TIME           Displays or sets the system time.
TITLE          Sets the window title for a CMD.EXE session.
TREE           Graphically displays the directory structure of a drive or
               path.
TYPE           Displays the contents of a text file.
VER            Displays the Windows version.
VERIFY         Tells Windows whether to verify that your files are written
               correctly to a disk.
VOL            Displays a disk volume label and serial number.
XCOPY          Copies files and directory trees.
WMIC           Displays WMI information inside interactive command shell.
You can get further help on a command by typing the command name followed by a forward slash and question mark.

Code: Select all

H:\>md /?
Creates a directory.

MKDIR [drive:]path
MD [drive:]path

If Command Extensions are enabled MKDIR changes as follows:

MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:

    mkdir \a\b\c\d

is the same as:

    mkdir \a
    chdir \a
    mkdir b
    chdir b
    mkdir c
    chdir c
    mkdir d

which is what you would have to type if extensions were disabled.
I normally just use the command line reference at SS64.

jc001
Posts: 5
Joined: 06 Dec 2018 12:09

Re: Creating a folder with today's date

#6 Post by jc001 » 16 Dec 2018 14:21

Many thanks for all your help on this. I have managed to find another way of doing this. I will keep your suggestions for future tasks though.

Post Reply