K
K
komino2021-10-21 15:29:39
cmd/bat
komino, 2021-10-21 15:29:39

How to find and rename a file?

Probably the simplest problem, with the simplest solution, but this is my first time dealing with CMD/BAT.

There is a Conditional folder C: \ TEMP, in which files periodically appear (once a month, excel files are uploaded using a bat script). They all appear with the same names, but with random numbers. For example:
"pattern_52773625_1.xls",
next. month is the same pattern, but with different numbers.
--
"shablon_62483624_1.xls",
same as pattern.
--
"reports_71439264_1.xls",
same as with pattern.
--

Task: Rename pattern_52773625_1 to "pattern 02-10-2021.xls" (02-10-2021 - The date at the time the script was executed. Other files are renamed in the same way.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Yurchenkov, 2021-10-21
@EPIDEMIASH

Why don't you want to write something similar on ps for example?

Get-ChildItem -Path $env:C:\TEMP\ -Filter "*.xls*" -Recurse | Where-Object {$_.LastWriteTime -ge $date} | Foreach {"$($_.BaseName) $(get-date -f yyyy-MM-dd)$($_.extension)"}

Upd: rewritten a bit, searches for files for today's date and adds the current date to the name.
If for example we want for the last week that have been changed, we change the condition
where-object {$_.LastWriteTime -gt ((Get-Date).adddays(-7))}

K
komino, 2021-10-22
@komino

for %%i in ('dir /bc:\temp *pattern*') do (for %%j in ('date /t') do ('move /yc:\temp\%%i new_folder\pattern-% %j))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question