Save current date -X days in a variable

Asked

Viewed 302 times

1

I have a folder that every day thousands of log files are saved and when you need to search a file by date and time is a sacrifice.

I have to do a routine to save the current files in a folder named with the date, example (20180425), I need to do the same for the old files, only to do this I would like to create a routine that went searching for files with the current date -365 days and was saving the files with current date -365 days that would be a folder with the name (20170425).

Is there any way to save the current date -X days in a variable so I can achieve this goal?

I need to do this in a batch, it can even be an executable that can be called by placing the parameter of the number of days ago so that it returns what was the date.

To save the current date in YYYYMMDD format I use this command:

SET date=
for /F "tokens=1-3 delims=/ " %%a in ('date /T') do set date=%%c%%b%%a

To move the files I want to this folder I use this one:

forfiles -s -d -0 -m *.log -c "cmd /c move "@path" "%date%" "
  • Let’s see if I got it, do you want to take the file creation date and separate by folders? Example: 26-04-2018 18:06 will create a folder 20180426

  • Exactly that.

  • Let me see if I understand, you want for example a file with creation date 13/12/2016 to be played inside a folder called 20161213 ?

  • Recovers file date as ? in name saved by file creation date ?

  • @Cristianmota, that’s right.

  • @Sachadee, I edited the question by placing the commands I use to get the current date and move the current files to the folder.

  • Okay, hold on a second, I’ll set up the script for you.. I’m at work, before 10:00 I do it.

Show 2 more comments

1 answer

2


Assuming the files are in format .log run the script in the same file directory, folders will be created with all dates necessary to make the organization, once created, will be made a copy of the files to the folder referring to the file date, preserving the files originals.

    @echo off

    if not exist %tmp%\SDL (md %tmp%\SDL)

    if exist %tmp%\SDL\logs.txt (del /f /q %tmp%\SDL\logs.txt)
    if exist %tmp%\SDL\datas.txt (del /f /q %tmp%\SDL\datas.txt)
    if exist %tmp%\SDL\x.txt (del /f /q %tmp%\SDL\x.txt)

    dir /b *.log>%tmp%\SDL\logs.txt

    setlocal enabledelayedexpansion
    for /f %%L in (%tmp%\SDL\logs.txt) do (
    set log=%%L

    for %%E in (!log!) do (
    set data=%%~tE
    set data=!data:~,-6!
    set data=!data: =!

    echo.!data!>%tmp%\SDL\tmp
    for /f "tokens=1-3 delims=/" %%G in (%tmp%\SDL\tmp) do (set data=%%I%%H%%G)
    if not exist !data! (MD !data!)
    echo.!data!>>%tmp%\SDL\datas.txt

    echo.!log! !data!>>%tmp%\SDL\x.txt
    )
    )
    )

    for /f "tokens=1-2 delims= " %%K in (%tmp%\SDL\x.txt) do (set f=%%K
    set d=%%L

    copy /a /v "!f!" "!d!"
    )
    pause >nul

Browser other questions tagged

You are not signed in. Login or sign up in order to post.