Вопрос

I run a small batch-file to rename some txt-file:
"C:\backup\t1-dd-dd t2-dd-dd.txt"

in the filename d - is a digit (from 0 to 9);
note: there is space in the filename between t1 and t2 marks (I need it for some reason).

now, I need to replace digits only in the 't1-dd-dd' part of the filename.

with powershell Rename-Item it can be done like this (example):

powershell -command "& { Get-ChildItem с:\backup -filter 't1-* t2-*.txt' | Rename-Item -NewName { $_.Name -replace 't1-\d\d-\d\d','t1-00-99' } }"

result would be like this:
C:\backup\t1-14-26 t2-56-48.txt (old filename)
C:\backup\t1-00-99 t2-56-48.txt (new filename)

is it possible to do same thing without powershell, just using cmd RENAME command?

Это было полезно?

Решение

You can't do it with regex, but you can do it with RENAME wildcards.

ren "c:\backup\t1-??-?? t2-??-??.txt" "???00-99*"

Within the target mask, each ? preserves one character, and the literals do a one for one character replacement, and the * preserves the remainder.

See How does the Windows RENAME command interpret wildcards? for more info.

If you are worried that the source mask is not specific enough, then you could use the following batch script to guarantee only properly named files are renamed.

pushd "c:\backup"
for /f "delims=" %%F in (
  'dir /b /a-d "t1-??-?? t2-??-??.txt" ^| findstr /xirc:"t1-[0-9][0-9]-[0-9][0-9] t2-[0-9][0-9]-[0-9][0-9].txt"'
) do ren "%%F" "???00-99*"
popd

But your powershell script is probably easier :-)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top