Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Date when used as folder name spanning multiple directories  (Read 3567 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Date when used as folder name spanning multiple directories
« on: January 14, 2018, 09:46:54 AM »
Trying to get my batch to not span to multiple directories and scratching my head as to why its doing this.

When I look at the %date% I get this format:

C:\Users\user1>echo %date%
Sun 01/14/2018

Which I replace / with - at the start of the batch

C:\Users\user1>echo %date:/=-%
Sun 01-14-2018

Here is what I put together so far. And it passes the data to:
C:\test2\sun 01\14\2018\
in which I would like the data to be in a path such as:
C:\test2\sun 01-14-2018\
Initially I thought it was flipping the / to \ to make for the folder creation depth, but even with / replaced with - in %date% its still doing the same thing. This is probably a pretty easy thing to correct but stuck after 45 minutes on this and google isnt a help at this point.  ;D

Code: [Select]
echo off
cd\.
cd test2
set datefix=%DATE%
set datefix=%DATE:/=-%
cls
@echo. Running Backup
@echo.
@echo.
@echo. Backup ... Creating Folder with todays date
@echo.
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir %datestr%
@echo.
@echo. Backup ... Folder Created with todays date
@echo.
@echo. Backup ... XCopying Important Data to new backup location for %date%
@echo.
xcopy c:\test1\*.* "c:\test2\%datestr%\*.*" /s/d/y/h
@echo.
@echo. Backup ... Important Data has been backed up
@echo.
pause

On my last attempt at trying to get this to work it breaks %date% and dumps the data directly to C:\test2\ with replacing date with datefix. I assumed in the first batch above that it would keep to the formatting of replacement of / with - but it didnt seem to, so I thought maybe I am overlooking the fact that date in the for loop needs to be replaced with datefix which is taking the identity of %date% but with the / to - replacement in formatting with the batch below that copies data to C:\test2\ but doesnt create the folder with todays date.


Code: [Select]
echo off
cd\.
cd test2
set datefix=%DATE%
set datefix=%DATE:/=-%
cls
@echo. Running Backup
@echo.
@echo.
@echo. Backup ... Creating Folder with todays date
@echo.
for /f "tokens=1* delims=" %%a in ('datefix /T') do set datestr=%%a
mkdir %datestr%
@echo.
@echo. Backup ... Folder Created with todays date
@echo.
@echo. Backup ... XCopying Important Data to new backup location for %date%
@echo.
xcopy c:\test1\*.* "c:\test2\%datestr%\*.*" /s/d/y/h
@echo.
@echo. Backup ... Important Data has been backed up
@echo.
pause

Salmon Trout

  • Guest
Re: Date when used as folder name spanning multiple directories
« Reply #1 on: January 14, 2018, 02:15:09 PM »
Please don't think the comments are meant to be criticisms! In that vein, if you do @echo off at the top of the batch script, you don't need all those @ symbols at the start of lines, and you only need a dot after echo if you are making a newline, although I know some people use them as insurance against certain errors stopping a script.

Hope this works. Note: my locale uses dd/mm/yyyy date format

I have put some informative echoes in.

@echo off
REM do it once at the top

REM commented out for my local testing
REM cd\.
REM cd test2

cls

REM This is what DATE /T produces anyhow
set datefix=%DATE%
echo Date var before: %datefix%

REM You swapped the slashes for hyphens
set datefix=%datefix:/=-%
echo Date var after:  %datefix%

echo Running Backup
echo.
echo.
echo Backup ... Creating Folder with todays date
echo.

REM You don't need this
REM You can't magick a variable into a command ;)
REM for /f "tokens=1* delims=" %%a in ('datefix /T') do set datestr=%%a

REM This is today's date with hyphens
echo making folder: %datefix%

REM Remove echo at start when you want to do it for real
echo mkdir %datefix%

echo.
echo Backup ... Folder Created with todays date
echo.
echo Backup ... XCopying Important Data to new backup location for %date%
echo.

REM Remove echo at start when you want to do it for real
echo xcopy c:\test1\*.* "c:\test2\%datefix%\*.*" /s/d/y/h

echo.
echo Backup ... Important Data has been backed up
echo.
pause


a test...

Date var before: 14/01/2018
Date var after:  14-01-2018
Running Backup


Backup ... Creating Folder with todays date

making folder: 14-01-2018
mkdir 14-01-2018

Backup ... Folder Created with todays date

Backup ... XCopying Important Data to new backup location for 14/01/2018

xcopy c:\test1\*.* "c:\test2\14-01-2018\*.*" /s/d/y/h

Backup ... Important Data has been backed up
« Last Edit: January 14, 2018, 02:39:38 PM by Salmon Trout »

Salmon Trout

  • Guest
Re: Date when used as folder name spanning multiple directories
« Reply #2 on: January 15, 2018, 12:44:32 AM »
Something I forgot about is that if a file name, folder name or path has any spaces, it needs to be enclosed in double quotes like this
mkdir "%datefix%". So if your date format is like this "Sun 14/01/2018' this might be necessary.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Date when used as folder name spanning multiple directories
« Reply #3 on: January 15, 2018, 09:46:28 AM »
Thanks Salmon!  8)

So all I really needed to do was

set datefix=%date:/=-%

mkdir "%datefix%"


So cool that its that simple. I had quite the mess going on.  ::)

I got pointed in wrong direction I guess from this example: https://stackoverflow.com/questions/5485853/how-to-create-a-folder-with-name-as-current-date-in-batch-bat-files

And I should have looked at the comments at the bottom of it. I didnt expect a best answer being flawed.  :-[

From the issue I had with it I figured I needed to substitute forward slash for hyphen and then it should work, but that FOR loop was a problem. As well as I got a little creative trying to magic a variable into a command within that to try for force my way to success of trial and error.  Hit it with a bigger hammer, when a hammer wasn't needed, but same result because it was incorrect! ;D

It works awesome...

Now to add on to this.

My next step is going to be to add where I compare between a NAS and Last Backup Location and only new files and changed files saved to New Backup by Date Folder location. I might be back here with more questions on that part of it.  ;)

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Date when used as folder name spanning multiple directories
« Reply #4 on: January 15, 2018, 11:14:36 AM »
The way you are getting the date is region dependent.  So your script may not be portable to other computers.
You could considering getting the date in a format that will always be the same.

Code: [Select]
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"This will always give you the date and time as YYYYMMDDhhmmss.
LocalDateTime=20180115121353.383000-360

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Date when used as folder name spanning multiple directories
« Reply #5 on: January 15, 2018, 05:42:39 PM »
Hey Squashman ... Thank you for your addition on this subject. Never played with WMIC and so now doing some research into its abilities and limitations. Pretty cool you can poll the OS for localdatetime and set formatting in batch.  8)

Microsoft is very vague on WMIC but just started looking into it: https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/wmic.mspx?mfr=true

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Date when used as folder name spanning multiple directories
« Reply #6 on: January 17, 2018, 08:33:22 PM »
You could call out to Powershell as well to get the date and time in the defined format that you want.

Salmon Trout

  • Guest
Re: Date when used as folder name spanning multiple directories
« Reply #7 on: January 18, 2018, 02:37:46 AM »
The way you are getting the date is region dependent.  So your script may not be portable to other computers.
You could considering getting the date in a format that will always be the same.

Code: [Select]
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"This will always give you the date and time as YYYYMMDDhhmmss.
LocalDateTime=20180115121353.383000-360
Can slice that up

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set wmictime=%%a
echo wmictime is %wmictime%
REM Good ol' column ruler for monospace fonts
REM Batch strings start at 0
REM                       1         2
REM             0123456789012345678901234
REM wmictime is 20180118091634.523000+000
set yyyy=%wmictime:~0,4%
set mm=%wmictime:~4,2%
set dd=%wmictime:~6,2%
set hh=%wmictime:~8,2%
set mn=%wmictime:~10,2%
set ss=%wmictime:~12,2%
set mss=%wmictime:~15,3%
set utcoffset=%wmictime:~21,4%
echo yyyy %yyyy%
echo mm   %mm%
echo dd   %dd%
echo hh   %hh%
echo mn   %mn%
echo ss   %ss%
echo mss  %mss%
echo utc  %utcoffset%
echo yyyy-mm-dd-hh-mn-ss.ms_utcoffset: %yyyy%-%mm%-%dd%-%hh%-%mn%-%ss%.%mss%_%utcoffset%


Result...

wmictime is 20180118093042.091000+000
yyyy 2018
mm   01
dd   18
hh   09
mn   30
ss   42
mss  091
utc  +000
yyyy-mm-dd-hh-mn-ss.ms_utcoffset: 2018-01-18-09-30-42.091_+000