I wrote a simple .bat file that appends the string "somethingnew" infront of .txt files:

for %%x in (*.txt) do ren "%%x" "somethingnew%%~nx"

But when I run it from the command prompt (or when I double click it) it does nothing.

I found this post but there is no item called "UserChoice" in the registry and I checked that the .bat association is correct.


EDIT: Also, I've noticed that when I double click the file, the command prompt opens for a split second and then closes, and when I try to run the file through the command prompt, it runs but does nothing.


I just tried your batch script. It works, but it doesn't output anything showing it's working. If you want confirmation it did something, add "pause" on another line. Also, it renamed my txt files without adding .txt to the end of the filename. Here's your updated code that should address both issues:

for %%x in (*.txt) do ren "%%x" "somethingnew%%~nx.txt"
  • Thanks for the pause and for the .txt at the end. That was what messed up the previous code as I didn't notice that the files had changed file format when I had run a similar command. However, your code also renames the first file twice. Feb 5 at 17:40
  • I created three new txt files, and they all get renamed correctly, but the first file gets renamed twice before the pause Feb 5 at 17:41
  • Try renaming all of your .txt files to something other than "somethingnew.txt" and running the code again. I just tried it and it worked fine.
    – Salocor
    Feb 5 at 17:42
  • Here is an image for clarification: ibb.co/mtj5cPZ
    – Salocor
    Feb 5 at 17:45
  • 1.txt becomes somethingnew1.txt, then 2.txt becomes somethingnew2.txt, then somethingnew1.txt becomes somethingnewsomethingnew1.txt Feb 5 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.