0

Hi I am trying to add a suffix before the extension for all files in a folder. Is there anyway I can get a script to do this with notepad please. I cannot use any other means :-( Example files is my,file.pdf would like it to be renamed my, file-2020.pdf.

  • Please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [ask]. – DavidPostill Oct 02 '20 at 17:54

1 Answers1

2

The for command is your friend. In the example below, you will see that I used ~nf and ~x.

I would explain these too you.. but you will see a better explination by using for /? at the command prompt. Go to the end of the help text and it will tell you what each one means. You will also see that for is pretty badass at parsing things for a stupid batch file command.

Try (from the command line):

for %f in (*.*) do echo rename "%~nxf" "%~nf-2020%~xf"

From a batch file you need to double the % chars:

for %%f in (*.*) do echo rename "%%~nxf" "%%~nf-2020%%~xf"

You can see that I stuck the -2020 where I did. Put anything you want there.

Also.. OF COURSE.. REMOVE THE ECHO command when it does what you want.. it is there so you can play with it. Note that using ECHO until you get the command correct will prevent you from hurting yourself.

  • Thank you so much for replying. I should jave mentioned I have no knowledge in scripting except able to put scrip in a notepad and run it. Can you please give me exact lines that I would need to put in the notepad to do this. I will save the bat file in the same folder as the files that need the suffix changed. – Chandu Malapaka Oct 02 '20 at 16:05
  • I GAVE YOU the exact lines (actually, you only need one line). Remove the word "echo". The first example is for direct from the command line. The second example is for running from a batch script. The notepad will not run batch scripts.. only provide and editor to write them. Please ask more questions if I am still confusing you. :-) I am happy to help. – Señor CMasMas Oct 02 '20 at 16:23
  • @chanduMalapka then we have no option, and here we can't teach you any tutorial step by step. you have to do some learning on batch at your own. first of all, these commands in this helpful answer is quite complicated, but windows has a built-in help system luckily. so first open a command prompt (hit win+r and type cmd) and a command prompt window will open. then you can easily get help on any command by adding /? after the command. get through the commands thoroughly, first start from some basic commands like echo,cls,title,pause,color and then go through [Continued below] – Wasif Oct 02 '20 at 16:28
  • The commands set,goto,if,for,call. then learn about the important batch file scripting techniques and then learn about the native command line windows Utilites. you should visit ss64.com/nt and robvanderwoude.com, they are great places for learning batch and native windows scripting (powershell, VBScript, jscript, hta, .net based (c#,VB.net)). now let's come into the main topic. first these commands will only execute, if you are in the directory where the files are, use cd /d and pushd to change directory. then you can apply the command. or if it is in your PATH environment [cont'd] – Wasif Oct 02 '20 at 16:33
  • variable, to add entries to path environment variable use setx. and I have seen your intention of copy-paste into notepad. if that's the case add cd or pushd lines first (@echo off at begginning preferably, to stop show of unnecessary clutter output) and then insert the command. save it as a .bat file and then run it. there is no need to run as administrator if you have permission of write to the file. good luck – Wasif Oct 02 '20 at 16:36
  • Thanks for filling in the blanks @Wasif_Hasan ! – Señor CMasMas Oct 02 '20 at 17:16