1

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.

Thanks.

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.

1 Answers1

1

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"
pause
Salocor
  • 705
  • 2
  • 15
  • 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. – Zero Pancakes Feb 05 '21 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` – Zero Pancakes Feb 05 '21 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 05 '21 at 17:42
  • Here is an image for clarification: https://ibb.co/mtj5cPZ – Salocor Feb 05 '21 at 17:45
  • `1.txt` becomes `somethingnew1.txt`, then `2.txt` becomes `somethingnew2.txt`, then `somethingnew1.txt` becomes `somethingnewsomethingnew1.txt` – Zero Pancakes Feb 05 '21 at 17:46
  • That's strange... Did you by chance forget to remove the old code before pasting the new code? Try making a new batch script and paste the code I provided. – Salocor Feb 05 '21 at 17:47
  • It's a new script. I don't know why it works on your pc but not on mine. – Zero Pancakes Feb 05 '21 at 17:49
  • Try removing the pause from the script. – Salocor Feb 05 '21 at 17:50
  • I removed it and it does the same thing. – Zero Pancakes Feb 05 '21 at 17:53
  • Let's move this discussion to chat so we don't pollute the comments. – Salocor Feb 05 '21 at 17:53
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/119383/discussion-between-salocor-and-zero-pancakes). – Salocor Feb 05 '21 at 17:53