Batch script to replace lines in a different batch file - batch-file

I have a batch script that has some software versions that looks like this (the numbers can change):
set NewVer=123
set NewVerServ=456
set AgeNewVer=789
I need to create a different script that, when ran, looks for those specific strings, and replace the numbers with new numbers that I will input in the new script.
I couldn't really get it to work with findstr as it returns mixed results.
I'm sure I'm missing something simple here, can anyone offer any advice?

You could store the values in a separate file, then you can rewrite the complete file each time.
(
echo set NewVer=123
echo set NewVerServ=456
echo set AgeNewVer=789
) > versions.bat
and if you want to use them from another batch file, use:
call versions.bat

Still no wiser as to which specific strings will be involved, but I'd deduce that this new script is intended to replace the variables set in the existing script with new values.
IMHO, the easiest way is to remove those three lines from the existing script (and any other such set variable lines as you may consider need to be altered) and use, as your new script,
#echo off
setlocal
set NewVer=124
set NewVerServ=457
set AgeNewVer=790
call "oldscript.bat"
as the environment inherited by oldscript.bat will now contain the required variables set to their new values.

Related

Batch file how to delete end text in a variable string in a for loop

I am writing some batch code to simplify a process I have of downloading some files, renaming them, and then copying them to replace the old ones. I'm running into an issue where I have a FOR loop read in a list of files from a directory, then try to modify the filenames.
The filenames all have FLY in the name, and I want to remove all text after FLY. I can't use tokens because the filenames are inconsistent in length, have multiple spaces, and wouldn't have a set number of tokens. I can't use substring because there is not a set number of characters after FLY.
I've tried using the examples at SS64 and also read numerous threads on here but nothing really seems to match my situation.
Here's the code snippet, appreciate if someone can tell me where I'm going wrong:
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=*" %%A IN ('DIR /B ^"%~DP0VFR^"') DO (
SET FILENAME=%%A
SET REMOVETEXT=!FILENAME:*FLY=!
SET NEWFILENAME=!FILENAME:!REMOVETEXT!=!
ECHO !FILENAME! will be renamed !NEWFILENAME!
)
When I insert echos to see what's going on everything works as expected up until the last SET, where somehow the ending result is !NEWFILENAME! is blank.
Hmm. My results were different from yours.
The " in your dir do not need to be escaped.
The problem with your set statement is that it's interpreted as
SET NEWFILENAME=!FILENAME:! + REMOVETEXT + !=!
and since FILENAME: and = are not existing variables, each will be replaced by nothing yielding "REMOVETEXT", not blank as you claim.
The solution is to use a two-stage evaluation of newname
call SET NEWFILENAME=%%FILENAME:!REMOVETEXT!=%%
which is resolved as
SET NEWFILENAME=%FILENAME:current_value_of_REMOVETEXT=%
in a sub-shell.
After cold brew it occurred to me that I might be going about this all wrong and making it more complicated than it needs to be... I decided to try directly renaming the files with wildcards and that actually worked. Didn't even need the FOR loop.
REN "%~DP0VFR\*FLY*" *FLY
No idea why the first (and overly convoluted) solution I tried didn't work, but this does with a lot less code!

Pick a specific folder in a Path and the name of a file to set 2 differents variables

Sorry to ask a question which will be an easy one for many of you I guess...
I'm a student in a CG School, but for the need of my project I need to code a simple .bat file to run on many computers. Everything works pretty fine, but now I need to simplify it to gain extra time setting up the .bat.
We have ALWAYS a Path like that :
D:\Project\Production\Shot\Render\Room\maya\scenes\Room_01.mb
I need two variables which store :
project : D:\Project\Production\Shot\Render\Room\maya
scene : Room_01
I think I need a loop which analyzes the path backward and sets the path after seeing 2 \ in order to avoid the file name, and the scenes folder. And I need an other thing to store all the file name ######.mb removing the .mb
I tried many things with substring forum, but it's kinda black magic for me...
So if anyone could help, I would be so grateful to you ! :)
Thank you in advance.
An easy solution is to use a for command. It will allow you to retrieve different properties of a file system reference.
set "fileReference=D:\Project\Production\Shot\Render\Room\maya\scenes\Room_01.mb"
for %%a in ("%fileReference%") do set "scene=%%~na"
Where %%~na is the name of the element being referenced by the for replaceable parameter %%a
for %%a in ("%fileReference%\..\..") do set "project=%%~fa"
Where %%~fa is the full path of the element being referenced by the for replaceable parameter %%a
A for loop is typically needed here. The first for loop grabs the last element of the string when split by a backslash. The path is then modified to have the file name removed from it, and then another for loop grabs the end off that string in the same manner.
#echo off
set my_path=D:\Project\Production\Shot\Render\Room\maya\scenes\Room_01.mb
for %%F in ("%my_path%") do set file=%%~nxF
echo.%file%
call set modded_path=%%my_path:\%file%=%%
for %%F in ("%modded_path%") do set folder=%%~nxF
echo.%folder%

Launching Pentaho Job using a batch file and setting a parameter value

I'm scheduling a Job using a batch file (bat) but I don't know how to set a parameter that the job needs.
hope someone can help me on this!
My Batch file is:
#echo off
set Pentaho_Dir="C:\Transformations&Jobs"
set Pentaho_Job=Interfases_MI.kjb
set path="C:\Program Files\Pentaho\data-integration";%path%
Kitchen.bat /file:%PENTAHO_DIR%\%Pentaho_Job% /log:%PENTAHO_DIR%\Logs\Interfases_MI.log /level:basic
Since you keep which parameter value is failing to yourself, we resort to guesswork.
I'd try
set "Pentaho_Dir=C:\Transformations&Jobs"
set "Pentaho_Job=Interfases_MI.kjb"
set "path=C:\Program Files\Pentaho\data-integration;%path%"
Kitchen.bat /file:"%PENTAHO_DIR%\%Pentaho_Job%" /log:"%PENTAHO_DIR%\Logs\Interfases_MI.log" /level:basic
The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var. This may not be a problem in the current case, but the resolution of your code would be
Kitchen.bat /file:"C:\Transformations&Jobs"\Interfases_MI.kjb /log:"C:\Transformations&Jobs"\Logs\Interfases_MI.log /level:basic
Whereas cmd would be reasonably happy with the quotes in odd places, kitchen.bat may not be.
In windows you can pass the parameters as
/param="_destination_email=test#test.com"

Batch incrementation from external file

I have a script who creates new tags in a SVN, and add some files. I want to automate this task so I would like to find some way to do automatically the incrementation for the tags name, from 1.0 to X.0.
I thought about a conf file who would contains "1.0" as a first version number and who would be overwrite at each call to the script. But not sure I can get the "1.0" value from the file and then do an incrementation on it in my script.
Any help would be really appreciate.
Thanks in advance
Don't create a seed configuration file. Instead, let the batch script default to 1.0 if file does not exist.
#echo off
setlocal
set "conf=version.conf"
if not exist "%conf%" (set version=1.0) else (
for /f "usebackq delims=." %%N in ("%conf%") do set /a version=%%N+1
)
set "version=%version%.0"
(echo %version%)>"%conf%"
I'm assuming you will never run this process multiple times in parallel - it can fail if you do run in parallel. Modifications can be made to lock the conf file so you can run in parallel if need be. See the accepted answer to how to check in command line if given file or directory is locked, that it is used by a process? for more info.
Take a look at keywords in Subversion using autoprops.
First, setup subversion to honor keyword expansion
enable-auto-props = yes
[auto-props]
version.txt = svn:keywords=Revision
Then, setup a simple file, let's call it version.txt with the $revision$ keyword and some random content.
$revision$
Random content
Then, in your batch file, recreate the version.txt file with new random content
echo $revision$ >version.txt
echo %random% %date% %time% >>version.txt
and check in this new file every time your batch file is run, so it will become
$revision 32 $
4214 Mon 21/01/2013 15:53:27,62
This way, subversion will keep an accurate version number of all the runs of the batch file, even in multiple clients and simultaneosly.
You might then extract and use the revision number from version.txt with code similar to
for /f "tokens=1,2" %%a in (version.txt) do (
if %%a==$revision (
echo Revision number is %%b
echo do something with %%b, create %%b tag or whatever
)
)
Since you don't say what language you want to use only general remarks can be given:
It certainly is possible to maintain a small 'version' file holding the 'dottet version number', something like 0.2.6 maybe. That files content can be read by any process. You should implement a little collection of methods to split that content into its numerical tokens (major and minor version and the like). Those numerical values can be processed by any mathematical function you like to use. For example you can increment them. Another method would be some 'implode' function that takes the numerical tokens and creates again a 'dottet version number' (now maybe 0.2.7...) and finally you can write that information back into the file. It certainly makes sense to allow an argument that controls which part of the version should be incremented.
Such scheme is not really efficient, but often sufficient.
Note, that such approach will only work if you can guarantee that it is always only a single process to access that version file. Otherwise multiple processes might overwrite each others results which certainly is a cause of problems.
As an alternative, maybe a more elegant alternative, you might consider treating the subversion repository itself as seed storage for your version number: instead of reading a special files content (what if that file is deleted or something else happens?) make a request to the tags folder inside subversion. It should contain all previously tagged versions. So that is precisely the information you want. Take all version numbers, sort them, take the highest one and process it as above.

Removing date and time stamp from a string with spaces in batch

I am making a program that automatically backs up files, stores up to a maximum of five of them, and has an option to restore any of the five files. When the files are backed up, it adds a date and time stamp in the format of YYYYMMDD_HHMMSS_filename.ext. In order to restore the file, it is necessary to first cut out the date stamp.
The method I am currently using to cut the date stamp off of the beginning of the file is as follows.
set VAR=%VAR:~16%
echo %VAR%
The problem being, if the backed up file is called "20120825_140343_file name.txt", the above method will only return "file," omitting anything after the space. The spaces in the file names need to be preserved for them to be recognized by the program using them.
tl;dr I need to cut the string 20120825_140343_file name.txt into just "file name.txt", but my method just returns "file."
If delimiters or something would help I could separate the date stamp and file name with a different character, I.E. 20120825_140343-file-name.txt
Your method, though inelegant and inflexible, should work. This tells me that you are not storing the entire filename in VAR. That is why %var:~16% only results in file, and not file name.txt. I assume that you assign VAR like this somewhere:
SET VAR=%1
You'll need to either do this:
SET VAR=%1 %2
Or insert double-quotes around the file name when you call your batch file, and then set var like this to remove the quotes:
SET VAR=%~1
That should be enough to get your batch to work.
====================================================
But, to answer the question you actually asked, I'll show you a method of extracting "file name.txt" from var that will work even if there are more or even less than 16 prefix characters.
Use the for /f statement, specify the 3rd token, with underscores as a delimiter. Here is a self-contained example. (To run from the command-line, change %%x to %x.
SET VAR=20120825_140343_file name.txt
for /f "tokens=3 delims=_" %%x in ("%VAR%") do set VAR=%%x
ECHO %VAR%
Just remember, this solution will NOT fix your problem if you do not fix your code to assure your VAR variable has the entire filename in it.
Have you tried this?
set VAR="%VAR:~16%"

Resources