-2

I have a batch of file with the following format

YYYYYYYYYY(X).txt

YYYYYYYYYY(XX).txt

YYYYYYYYYY(XXX).txt

YYYYYYYYYY(XXXX).txt

Y are simplified Chinese character/punctuation with no fixed length, i.e. the no. of Y ranged from a few to more than 10

And X are number (0-9)

I wanted to rename all the file and delete all the Y in front. What command can I use with cmd?

2 Answers2

0

Here's some quick untested example code, which should rename, YYYYYYYYYY(X).txt to (X).txt, YYYYYYYYYY(XX).txt to (XX).txt, YYYYYYYYYY(XXX).txt to (XXX).txt, and YYYYYYYYYY(XXXX).txt to (XXXX).txt, subject to matching names not already existing, and which is exactly what your question asks.

@For %%G In ("C:\Users\Gary\Documents\?*(*).txt") Do @(
    Set "}=%%~nG" & SetLocal EnableDelayedExpansion
    Ren "%%G" "(!}:*(=!%%~xG" & EndLocal)

Here's some additional untested example code, which should rename, YYYYYYYYYY(X).txt to X.txt, YYYYYYYYYY(XX).txt to XX.txt, YYYYYYYYYY(XXX).txt to XXX.txt, and YYYYYYYYYY(XXXX).txt to XXXX.txt, subject to matching names not already existing.

For %%G In ("C:\Users\Gary\Documents\*(*).txt") Do (
    Set "f=%%~nG"
    SetLocal EnableDelayedExpansion
    Set "n=!f:*(=!"
    Ren "%%G" "!n:~,-1!%%~xG"
    EndLocal
)
Compo
  • 36,585
  • 5
  • 27
  • 39
0

[untested]

for /f "delims=" %%b in ('dir /b /a-d "d:\the path\*(*).txt" ') do (
 for /f "tokens=2delims=()" %%c in ("%%b") do ren "d:\the path\%%b" "%%c%%~xb")
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). – costaparas Jan 23 '21 at 03:56
  • @costaparas: When I post a full explanation as I have in the past, (see many of my 3,700-plus responses) I include what I regard as relevant comments and tips in the answer to pre-empt the inevitable follow-up questions. Unfortunately, I have had the experience where a recent response, padded by what I regard as useful, but probably not *essential* information vandalised by two never-seen-before-in-this-tag members enforcing "the rules". I asked for a moderator rollback, which was refused, describing what had been deleted as "off-topic meta-commentary". – Magoo Jan 23 '21 at 04:17
  • This came up while reviewing [low quality posts](https://stackoverflow.com/review/low-quality-posts/28129727). I marked as Looks OK, and left a comment to indicate it would benefit from some commentary, as is usual on SO. I can't speak for the actions taken by others, but you should be able to edit your own posts without moderator assistance (unless it was locked or something?). – costaparas Jan 23 '21 at 04:22
  • @costaparas : On the other hand, if I don't add an explanation, I get my post proposed for deletion and downvoted - not because it's technically wrong, but because I've broken some other "rule" that has nothing to do with solving the problem. Damned if you do, damned if you don't. – Magoo Jan 23 '21 at 04:26
  • @costaparas: had to go to the mods as the vandals simply entered a rollback war – Magoo Jan 23 '21 at 04:27
  • I get where you're coming from, there are a lot of *trigger happy downvoters* around. I usually provide some code + some related discussion, and try not to go too off-topic. I'm also careful with the types of questions I answer, since sometimes if the question isn't "to standard", the answers get mis-treated as well. I agree 100% that there is a lot of inconsistency with the way people enforce the "rules" especially when it comes to voting (both close votes and downvotes). All we can do is aim to do our best, and rely on the good nature of most in the community. – costaparas Jan 23 '21 at 04:32