Can't figure out how to read file from nmake - nmake

I am doing something like this:
all:
#SET /p filecontent= < somefile.txt
#echo %filecontent%
However the filecontent variable does not seem to hold the contents of the file somefile.txt.

It is possible to read a file that is not a valid nmake file using !INCLUDE. For examle if we have a version file version that contains a single line of text we can do that:
//version file
1.2.4
//makefile
VERSION= \
!INCLUDE <version>
It is not working if the file contains more than one line.

Simply ensure somefile.txt is in acceptable nmake syntax, and then !include it. Thus:
c:>type somefile.txt
PASSWORD=secret
c:>type makefile
!INCLUDE somefile.txt
!MESSAGE Password is [$(PASSWORD)]
c:>nmake -nologo
Password is [secret]

You could try something like this:
# ---- vitaly.mak ----
target1:
# create and invoke a temporary cmd file
#<<mygetpassword.cmd
#echo off
setlocal
#SET /p filecontent= < secret.txt
#echo %filecontent%
endlocal
<<
#--- END ---
I think a cmd/bat file run within nmake.exe cannot affect the environment of nmake. So you must use the password that you grabbed from the secret.txt within the temporary cmd file.

Related

How to modify lines that hold a given string with new information and save it as a text file

I am working on modifying our batch files where we call #make functions inside. We want to add a script inside the batch file that checks an external header file, finds the line with date information(APP_VERSION_DATE) and updates the information there with new date information(I figured out how to fetch windows date information with batch, this is not an issue)
I know what steps to follow but batch syntax feels completely counter intuitive to me and I am stuck.
These are the steps I would like to follow:
1- Go through the app_version.h file line by line(for /f)
2- Find the lines with string APP_VERSION_DATE(if findstr...)
3- delete everything except APP_VERSION_DATE
4- CONCAT date information to APP_VERSION_DATE like APP_VERSION_DATE "23-05-2022"
5- Keep echoing every other line
6- Pipeline the information a new header file.
7- Delete header file
8- Rename the new header line as the old one.
set strToFind="app_version_date"
set result="Not Found"
for /f "tokens=2 delims=[]" %%A in ('findstr %strToFind% %filename%') do (
set result=%%A
if defined result (
if %result%==this is something
echo hurra this is it
) ELSE echo
)
this is where I am at right now and I am obviously still too far off to do something I want to do.
I am able to make a program that can find a given string in a file and change it but in this case I want to find the line that has the string I am searching for, delete the rest and modify it. I want to find the line where the string is and modify it, not the string itself. This is simply because date information as shown below;
#define APP_VERSION_DATE [2022-05-16 12:13]
won't be static and ever changing with each compile attempt.
I have something like this but this is too far from what I want to do.
Any help would be great! Thanks in advance
Replace date/time in header file app_version.h
There could be used the following commented batch file for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "HeaderFile=app_version.h"
if not exist "%HeaderFile%" exit /B 20
rem Get current local date/time in format [yyyy-MM-dd HH:mm].
for /F "tokens=1-5 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "AppVersionDate=[%%G-%%H-%%I %%J:%%K]" & goto UpdateHeaderFile
rem Let FINDSTR output all lines of the header file with a line number and
rem a colon at the beginning for processing really all lines including the
rem empty lines in the header file and output all lines without the line
rem number and the colon with exception of the line containing the string
rem #define APP_VERSION_DATE which is ignored and instead is output a line
rem defined here with the local date/time determined before. All lines output
rem by the loop are written into a newly created temporary header file.
:UpdateHeaderFile
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%HeaderFile%" 2^>nul') do (
set "Line=%%I"
setlocal EnableDelayedExpansion
if "!Line:#define APP_VERSION_DATE=!" == "!Line!" (
echo(!Line:*:=!
) else (
echo #define APP_VERSION_DATE %AppVersionDate%
)
endlocal
))>"%HeaderFile%.tmp"
rem Replace the original header file with the temporary header file.
if exist "%HeaderFile%.tmp" move /Y "%HeaderFile%.tmp" "%HeaderFile%" >nul
rem Delete the temporary header file if the command line above failed
rem because of the original header file is read-only or write-protected
rem or currently opened by an application with shared access denied.
if exist "%HeaderFile%.tmp" del "%HeaderFile%.tmp"
endlocal
The environment variable HeaderFile can be defined with an absolute path or a relative path.
Please read the chapter Usage of ROBOCOPY to get current date/time in my answer on Time is set incorrectly after midnight for an explanation of the first for /F command line.
Please read next my answer on How to read and print contents of text file line by line? It describes in full details the second for /F loop with the small modification of an additional IF condition to replace the line containing the string #define APP_VERSION_DATE with an current date/time.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
endlocal /?
exit /?
findstr /?
goto /?
if /?
move /?
rem /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of the unconditional command operator &.
Create header file current_date_time.h with date/time
The task could be done much easier if the file app_version.h contains anywhere the line:
#include "current_date_time.h"
The batch file could be in this case just:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1-5 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do echo #define APP_VERSION_DATE [%%G-%%H-%%I %%J:%%K]>"current_date_time.h"& goto EndBatch
:EndBatch
endlocal
This batch file always creates new the file current_date_time.h with just the single line:
#define APP_VERSION_DATE [2022-05-23 18:48]
And this single preprocessor macro definition line is included on compilation into app_version.h.
Define preprocessor macro APP_VERSION_DATE with current date/time
Every C/C++ compiler has an option to define a preprocessor macro on the command line and the option can be used multiple times on the command line to define multiple preprocessor macros.
For example see:
GNU gcc/g++: Options Controlling the Preprocessor explaining case-sensitive option -D
Microsoft C/C++: /D (Preprocessor Definitions)
So it is possible to define an environment variable with the current date/time in the wanted format with the single command line below and reference this environment variable value on running the C/C++ compiler with the appropriate option.
#set "AppVersionDate=" & for /F "tokens=1-5 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do #if not defined AppVersionDate set "AppVersionDate=[%%G-%%H-%%I %%J:%%K]"
GNU gcc/g++ would be run later with -D "APP_VERSION_DATE=%AppVersionDate%" and Microsoft C/C++ compiler with /D "APP_VERSION_DATE=%AppVersionDate%" as one of the options on compilation of the C/C++ source code files.
There are also the predefined macros __DATE__ and __TIME__:
GNU gcc/g++: Predefined Macros
Microsoft C/C++: Predefined macros
Search also for information about the environment variable SOURCE_DATE_EPOCH which gives control over the timestamp added by the C/C++ compiler itself to the produced binaries.

Possible to make a backup of original file in vim?

Is it is possible to make a copy of original file before writing the new buffer to the file without having to leave vim and copy it manually?
How about this? After editing the file, before :wq, you can do:
:!cat myfile.txt > backup.txt
and then save using :wq. The previous content would be stored in backup.txt
UPDATE
I realized that my solution might be a little complicated for beginners and unnecessary for single files backups, so if you want a simple solution just use:
:!cp % ~/
The % register keeps the name of the file and with this extern command you can copy the current file to your home folder or you can change it to any folder you want.
In Windows you can use this to send to a backup folder on C::
:!copy % \backups\
You can turn in a shortcut on your .vimrc with something like:
nnoremap .b :!cp % ~/
Old Answer:
I had the same need to backup before save the modifications, so I created a Bash and a Batch(for Windows) file that backups all the files that I want and used this conditional statement on .vimrc to choose automatically between the two systems:
if has("win32")
nnoremap <leader>bc :! C:\C\SCRIPTS\backupWIN.bat<cr>
else
nnoremap <leader>bc :!bash /home/vini/C/SCRIPTS/backup.sh<cr>
endif
Here the code for the Bash version:
#!/bin/bash
#adds the date to folder(you can change the date format)
now=$(date +"%d_%m_%Y_%H;%M;%S")
mkdir /home/vini/backups/C_BKP/pre_alpha/$now
cp -r /home/vini/C /home/vini/backups/C_BKP/pre_alpha/$now
echo "saved in: /home/vini/backups/C_BKP/pre_alpha/"$now
Here the code for the Batch file:
set start=%time%
::I didn't managed to make it print the seconds, so I choose to
::override the same files if I save twice in the same minute
#echo off
For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set mydate=%%a-%%b-%%c)
For /f "tokens=1-4 delims=/:" %%a in ('time /t') do (set mytime=%%ah%%bm%%c)
mkdir C:\Users\vini\Desktop\C\C-%mydate%-%mytime%
::careful with xcopy , read the documentation before modify it
xcopy /E /Y C:\C C:\Users\vini\Desktop\C\C-%mydate%-%mytime%\
You just need to change the name of the directories for match your folders and you are good to go.
As #Sato Katsura pointed out, it is patchmode:
:set patchmode=.orig
I just simply use:
:!cp % %.backup
Which will create a new file in the same location with .backup appended to it.

check for file existence using nmake and delete it

I am using Windows version of NMAKE. I would like to check for file existence in a make file. If it exists I need to delete it. Here is my code:
!IF EXIST ("C:\ABC.XML")
#del ABC.XML
!ELSE
#echo "FILE DOESN'T EXIST
!ENDIF
The above code is not working. I could not figure it out the problem. Please help.
Your code doesn't work because !IF, !ELSE and !ENDIF are a preprocessing directives and the result of preprocessing must produce a valid makefile. Commands are only allowed as part of what Microsoft calls a description block which are required to start with a dependency line with one or more targets and zero or more dependents.
You can get around this by executing your commands during the preprocessing stage by including them in a preprocessing directive surrounded by brackets ([]). Something like this:
!IF EXIST(C:\ABC.XML)
! IF [del C:\ABC.XML]
! ENDIF
!ELSEIF [echo FILE DOESN'T EXIST]
!ENDIF
The second !IF and the !ELSEIF directives are used to provide a context for the commands so they're executed during the preprocessing phase.
However I think you'd probably be better moving the del command to a description block where it's actually needed. For example if file ABC.XML needs to be deleted before it can be rebuilt, use something like this:
ABC.XML: ABC.CSV
-rem The csv2xml translator requires that the XML file not already exist
-#del ABC.XML 2> NUL
csv2xml ABC.CSV ABC.XML

How to run batch script without using *.bat extension

Is there any method in Windows through which we can execute a batch script without *.bat extension?
This is an interesting topic to me! I want to do some observations about it.
The important point first: A Batch file is a file with .BAT or .CMD extension. Period. Batch files can achieve, besides the execution of usual DOS commands, certain specific Batch-file facilities, in particular:
Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
Execution of GOTO command.
Execution of CALL :NAME command (internal subroutine).
Execution of SETLOCAL/ENDLOCAL commands.
Now the funny part: Any file can be redirected as input for CMD.exe so the DOS commands contained in it are executed in a similar way of a Batch file, with some differences. The most important one is that previous Batch-file facilities will NOT work. Another differences are illustrated in the NOT-Batch file below (I called it BATCH.TXT):
#echo off
rem Echo off just suppress echoing of the prompt and each loop of FOR command
rem but it does NOT suppress the listing of these commands!
rem Pause command does NOT pause, because it takes the character that follows it
pause
X
rem This behavior allows to put data for a SET /P command after it
set /P var=Enter data:
This is the data for previous command!
echo Data read: "%var%"
rem Complex FOR/IF commands may be assembled and they execute in the usual way:
for /L %i in (1,1,5) do (
set /P line=
if "!line:~0,6!" equ "SHOW: " echo Line read: !line:~6!
)
NOSHOW: First line read
SHOW: Second line
NOSHOW: This is third line
SHOW: The line number 4
NOSHOW: Final line, number five
rem You may suppress the tracing of the execution redirecting CMD output to NUL
rem In this case, redirect output to STDERR to display messages in the screen
echo This is a message redirected to STDERR >&2
rem GOTO command doesn't work:
goto label
goto :EOF
rem but both EXIT and EXIT /B commands works:
exit /B
:label
echo Never reach this point...
To execute previous file, type: CMD /V:ON < BATCH.TXT
The /V switch is needed to enable delayed expansion.
More specialized differences are related to the fact that commands in the NOT-Batch file are executed in the command-line context, NOT the Batch-file context. Perhaps Dave or jeb could elaborate on this point.
EDIT: Additional observations (batch2.txt):
#echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.
rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2
echo Standard input/output operations> CON
echo/> CON
< CON set /P var=Enter value: > CON
echo/> CON
echo The value read is: "%var%"> CON
Execute previous file this way: CMD < BATCH2.TXT > NUL
EDIT: More additional observations (batch3.txt)
#echo off
rem Dynamic access to variables that usually requires DelayedExpansion via "call" trick
rem Read the next four lines; "next" means placed after the FOR command
rem (this may be used to simulate a Unix "here doc")
for /L %i in (1,1,4) do (
set /P line[%i]=
)
Line one of immediate data
This is second line
The third one
And the fourth and last one...
(
echo Show the elements of the array read:
echo/
for /L %i in (1,1,4) do call echo Line %i- %line[%i]%
) > CON
Execute this file in the usual way: CMD < BATCH3.TXT > NUL
Interesting! Isn't it?
EDIT: Now, GOTO and CALL commands may be simulated in the NotBatch.txt file!!! See this post.
Antonio
Just use:
type mybat.txt | cmd
Breaking it down...
type mybat.txt reads mybat.txt as a text file and prints the contents. The | says capture anything getting printed by the command on its left and pass it as an input to the command on its right. Then cmd (as you can probably guess) interprets any input it receives as commands and executes them.
In case you were wondering... you can replace cmd with bash to run on Linux.
in my case, to make windows run files without extension (only for *.cmd, *.exe) observed, i have missed pathext variable (in system varailbles) to include .cmd. Once added i have no more to run file.cmd than simply file.
environment variables --> add/edit system variable to include .cmd;.exe (ofcourse your file should be in path)
It could be possible yes, but probably nor in an easy way =) cause first of all.. security.
I try to do the same thing some year ago, and some month ago, but i found no solution about it.. you could try to do
execu.cmd
type toLaunch.txt >> bin.cmd
call bin.cmd
pause > nul
exit
then in toLaunch.txt put
#echo off
echo Hello!
pause > nul
exit
just as example, it will "compile" the code, then it will execute the "output" file, that is just "parse"
instead of parsed you could also just rename use and maybe put an auto rename inside the script using inside toLaunch.txt
ren %0 %0.txt
hope it helped!
It is possible at some degree. You'll need an admin permissions to run assoc and ftype commands. Also a 'caller' script that will use your code:
Lets say the extension you want is called .scr.
Then execute this script as admin:
#echo off
:: requires Admin permissions
:: allows a files with .scr (in this case ) extension to act like .bat/.cmd files.
:: Will create a 'caller.bat' associated with the extension
:: which will create a temp .bat file on each call (you can consider this as cheating)
:: and will call it.
:: Have on mind that the %0 argument will be lost.
rem :: "installing" a caller.
if not exist "c:\scrCaller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\scrCaller.bat
rem :: associating file extension
assoc .scr=scrfile
ftype scrfile=c:\scrCaller "%%1" %%*
You even will be able to use GOTO and CALL and the other tricks you know. The only limitation is that the the %0 argument will be lost ,tough it can be hardcoded while creating the temp file.
As a lot of languages compile an .exe file for example I think this a legit approach.
If you want variables to be exported to the calling batch file, you could use
for /F "tokens=*" %%g in (file.txt) do (%%g)
This metod has several limitations (don't use :: for comments), but its perfect for configuration files.
Example:
rem Filename: "foo.conf"
rem
set option1=true
set option2=false
set option3=true
#echo off
for /F "tokens=*" %%g in (foo.conf) do (%%g)
echo %option1%
echo %option2%
echo %option3%
pause

nmake: can a batch file run as part of a make command block, affect the environment of the nmake.exe process?

I think in nmake if I do this:
example :
set value=77
echo %%value%%
The result will display 77 on the console.
Is there a way for me to invoke a .cmd or .bat file that will affect the environment of the nmake.exe process? Suppose I put the statement set value=77 in a file called "setvalue.cmd". Then change the makefile to this:
example :
setvalue
echo %%value%%
I get:
%value%
Alternatively, if there's a way to set a macro within a command block, that would also work. Or, a way to set the value of a macro from a batch file, even outside a command block.
You can create an nmake snippet during makefile pre-processing, and read that in. Assuming batch.cmd outputs valid nmake syntax, then
!if [batch.cmd >makefile.auto]
!error *** Could not create makefile.auto
!endif
!include makefile.auto
You should ensure batch.cmd sets %errorlevel% appropriately (e.g., exit /b 22).
makefile.auto can contain anything, but you would probably want stuff like value=77. A couple of points:
Dereference value using nmake syntax ($(value))
You can pass parameters to batch.cmd if necessary ([batch.cmd $(OBJECTS) >makefile.auto])
No, I don't think so.

Resources