A
A
Alex Pebody2022-04-06 09:27:17
cmd/bat
Alex Pebody, 2022-04-06 09:27:17

Storage of files for 2 months + 1 file on the first day of the month?

Good day everyone! Friends, if it's not difficult, who knows... Task: there are a lot of backup files, you need to save 1 file for the 1st day of each month, as well as backups for the last 2 months for each day.

In fact, making storage for 2 months is as easy as shelling pears:

forfiles -p "C:\some_folder" -s -m *.* -d -60 -c "cmd /c del /q @path"

REM -- moving 1st of month folders to temp\
forfiles  /M *_FULL_20*01_*.csv  /C "cmd /c move @path temp\ "

REM -- deleting files older than 30 days
forfiles  /M *_FULL_20*.csv  /D -30   /C "cmd /c del /s /q @path "

REM -- bringing back temp\ to this folder
forfiles  /P temp\  /C "cmd /c move @path ..\ "


To be blunt, you can do this:
forfiles -p "c:\folder" -s -m *.* -d -1 -c "cmd /c copy @path c:\folder\1st_dates"
forfiles -p "c:\folder" -s -m *.* -d -60 -c "cmd /c del /q @path"


Is there a more practical structure?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2022-04-06
@res2001

You can call a pipeline in cmd, in which you can try to filter files for the first number.
Roughly something like this:

forfiles -p "c:\folder" -s -m *.* -d -1 -c "cmd /c (dir @path | findstr /v /r /c:"тут регулярное выражение для поиска 1 дня в дате") && del /q @path"

This is just a diagram of how you can approach the solution. Didn't debug anything.
The pipeline can be debugged separately by stuffing it into a batch file or directly from the command line.
Forming a regular expression, I think, will not be difficult. I haven’t used them for a long time, so it disappeared from my head, and I’m too lazy to remember.
I see some difficulties with nested quotes, perhaps they need to be escaped somehow. Maybe there is something about this in the forfiles help.
By the way, the format for displaying the date in dir depends on the system settings for the date format, so it can be different on different computers. Keep this in mind. cmd does not know how to work with dates, so you have to use only the capabilities of working with strings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question