Save current date -X days in a variable

0

I have a folder that saves thousands of log files daily and when it is necessary to search for a file by date and time it is a sacrifice.

I have a folder that I want to save to the folder where I want to save the current files. 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, the current date -365 days and was going to save 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 into 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 putting the parameter of the number of days ago so that it returns what the date was.

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:

forfiles -s -d -0 -m *.log -c "cmd /c move "@path" "%date%" "
    
asked by anonymous 26.04.2018 / 23:01

1 answer

1
  

Assuming the files are in the .log format, run the script   folders in the same directory as the files, folders with all the   necessary dates to make the organization, once created, will be   made a copy of the files to the folder referring to the date of the file, 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
    
28.04.2018 / 02:57