0

I have very long list of commands in my batch, where a lot of strings depend on file name of the file I work with. So I would like to somehow rewrite those parts of code with diferent variation so I could avoid manualy search for these bits and rewrite them one by one in my long batch whenever I would work with diferent file name of alt file. just an example:

Batch-1-(endlesly-long-bat).bat:

IF EXIST "*.webm" (
REN "*.webm" "COOL-PROJECT.webm"
)
FOR %%a in ("COOL-PROJECT.webm") do ffmpeg.exe -i "%%a" -vcodec copy -acodec copy "%%~na.mp4"
IF EXIST "COOL-PROJECT.mp4" (
MD "COOL-PROJECT"
)
REM so on and so on. order, type or commands itselfs are irelevant

Batch-2-(overwriting-bat).bat:

this bat needs to scan content of "Batch-1-(endlesly-long-bat).bat" and look for specific string of characters "COOL-PROJECT" and when it founds all of them, then it will replace those characters with another string of characters "WORST-NIGHTMARE". so based on input mask phrase (COOL-PROJECT) this batch needs to rewrite specific informations in first batch. (I guess trought opening it within notepad? or there is a cmd way?)

player0
  • 124,011
  • 12
  • 67
  • 124
  • Maybe a good solution: open in Notepad or whatever editor and replace it with `%~1`. Then you can start the batch file with a startup parameter, for instance `Batch1.bat "COOL-PROJECT"`. No need to do any replaces after that. – GolezTrol Dec 30 '15 at 23:15
  • 4
    And if you want to do the replace from a script, you can pick FART, or any of the other answers to [this almost duplicate question](http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir). – GolezTrol Dec 30 '15 at 23:19
  • Some examples would have clarified matters. – Magoo Dec 31 '15 at 05:34
  • 1
    Bad idea - there is no need to write a script that requires editing between runs. You should follow Golez Trol's and Stephan's advice. It is extremely rare that you would ever require a batch script edit another batch script as part of normal operation. – dbenham Dec 31 '15 at 13:39
  • @GolezTrol - nice handy idea, I will use it, but not as solution of my question. that FART and other suggestions are also interesting. thank you – player0 Dec 31 '15 at 21:44

1 Answers1

2

if you use the same string over and over again, it's a good idea to work with variables instead of the string itself.

set "filename=COOL-PROJECT"
IF EXIST "*.webm" (
REN "*.webm" "%filename%.webm"
)
FOR %%a in ("%filename%.webm") do ffmpeg.exe -i "%%a" -vcodec copy -acodec copy "%%~na.mp4"
IF EXIST "%filename%.mp4" (
MD "%filename%"
)
REM so on and so on. order, type or commands itselfs are irelevant

This way, you have to edit only one line instead of many strings distributed in the whole file.

As this is a "one-time-Task" only, you can easily use "find and replace" with your Editor.

Als Golez' idea is a very good one. Just replace set "filename=COOL-PROJECT" with set "filename=%~1" and start your batchfile with the desired Name as Parameter:

Batch1.bat "COOL-PROJECT"

No need to edit anything ever again.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • thx. if I may confirm... is "Name as Parameter" (`Batch1.bat "COOL-PROJECT"`) same as `Title "COOL-PROJECT"` ? – player0 Jan 01 '16 at 17:31
  • uhm - no? First is a Parameter to the batchfile, second is a command to set the Windows title. See `title /?` – Stephan Jan 01 '16 at 18:58