Makefile Windows -- remove directory if exists - if-statement

I am trying to port a Makefile to Windows (using GNU Make). I am having trouble with removing directories. I found this question (Delete a directory and its files using command line but don't throw error if it doesn't exist) on how to conditionally remove directories, but I get an error trying to use that solution, translating approximately to The use of ""doc\html"" is syntactically impermissible in this context
The snippet causing it is this:
if exists "doc\html\" rmdir /Q /S doc\html
I also tried
cmd /c if exists "doc\html" cmd /c rmdir /Q /S doc\html
and
cmd /c if exists "doc\html\" cmd /c rmdir /Q /S doc\html.
I also tried rmdir /Q /S doc\html 2>nul. That works kind of. The error gets redirected, but Make still throws an error and stops.
How can I make this work?

the command is exist not exists. There is a typo in the thread I linked...

Related

How can I check if either or file exists in IF Exist?

How can I check if either or file exists in a If Exist statement?
Such as
If exist "C:/Windows/" OR "C:/Windows2" (
Do something
) else (
Something else
)
How would I do this? I just want either or to exist and to do something.
Simple example 1:
#echo off
if not exist "%SystemRoot%\" if not exist "C:\Windows2" goto MissingFolderFile
echo Found either the directory %SystemRoot% or the file/folder C:\Windows2.
rem Insert here more commands to run on either the folder C:\Windows
rem or the file/folder (=any file system entry) C:\Windows2 existing.
goto EndDemo
:MissingFolderFile
echo There is neither the directory %SystemRoot% nor the file/folder C:\Windows2.
rem Insert here more commands to run on neither folder C:\Windows
rem nor file/folder C:\Windows2 existing.
:EndDemo
pause
The Windows command processor is designed for processing one command line after the other which is the meaning of the word batch. The command GOTO is the preferred command to use in a batch file to continue batch processing not on next command line, but another one depending on an IF condition, i.e. change processing from one stack (other word for batch) of command lines to another group of command lines.
Simple example 2:
#echo off
if exist "%SystemRoot%\" goto FolderExists
if exist "C:\Windows2" goto FS_EntryExists
echo There is neither the directory %SystemRoot%\ nor C:\Windows2.
rem Insert here more commands to run on neither folder C:\Windows
rem nor file/folder/reparse point C:\Windows2 existing.
goto EndDemo
:FS_EntryExists
echo The file system entry (file or folder) C:\Windows2 exists.
rem Insert here more commands to run on C:\Windows2 existing.
goto EndDemo
:FolderExists
echo The folder %SystemRoot% exists.
rem Insert here more commands to run on folder C:\Windows existing.
:EndDemo
pause
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.
echo /?
goto /?
if /?
rem /?
NOTE:
The directory separator on Windows is \ and not / as on Linux or Mac. The Windows file management automatically replaces usually all / by \ before passing a file/folder argument string without or with a wildcard pattern to the file system as explained by Microsoft in the documentation about Naming Files, Paths, and Namespaces. But the usage of / instead of \ in file/folder argument strings can result nevertheless in unexpected behavior.
Example for unexpected behavior because of using / on running following command line directly in a command prompt window:
for %I in ("%SystemDrive%/Windows/*.exe") do #if exist "%I" (echo Existing file: "%I") else echo File not found: "%I"
This command line outputs a list of executable file names found by FOR in the Windows directory which do not exist for command IF just because of usage of / resulting in getting assigned to the loop variable the found file names without path. So this command line works only if the current directory on system drive is by chance the Windows directory.
The same command line with usage of \ as directory separator:
for %I in ("%SystemDrive%\Windows\*.exe") do #if exist "%I" (echo Existing file: "%I") else echo File not found: "%I"
This command line outputs each file name of the executables in Windows directory as existing file with full path.
Another example:
There is in root directory of current drive a directory Downloads and the current directory on this drive is Temp, for example D:\Downloads is the wanted current directory and D:\Temp is the current directory.
The used command is:
cd /Downloads
The result is the error message:
The system cannot find the path specified.
The command with correct directory separator usage:
cd \Downloads
This command works on D:\Temp being the current directory and D:\Downloads existing.
CD interprets on incorrect /Downloads the string /D at beginning of the directory path as option /D to change also the drive and searches for that reason for ownloads in current directory instead of Downloads in root directory of current drive. This wrong interpretation by CD is avoided by using the correct directory argument string \Downloads.
SUMMARY: \ is the directory separator and / is for command options.

libjpeg installation- how to go through the instruction

I have a problem with libjpeg installation.I need it so as to use CImg library and have posibility to save .jpg files.
I have download file from:
http://www.ijg.org/ and go step by step with instruction( I use VS2015).
Unfortunately I cannot go through first point:
Open the command prompt, change to the main directory and execute the command line NMAKE /f makefile.vc setup-v10
I got an error in command prompt: error message
fatal error U1025:file 'makefile.vc' not found
Could you please tell me what's going wrong? Or maybe you know some easiest way to install this library?
The fatal error
fatal error U1025:file 'makefile.vc' not found
is caused by nmake not finding the file makefile.vc in the folder that you executed the command
NMAKE /f makefile.vc setup-v10
Looking at the source zip file it appears the source is located in the jpeg-9b folder.
To solve this problem you need to change directory of your command prompt cmd.exe to the jpeg-9b folder before running the nmake command. To do so use the cd command in your cmd.exe session. The syntax is
cd fullpath
where fullpath is the full path where you have the jpeg-9b folder. If your path includes spaces make sure to quote the path with double quotes.

MingGW C++ compiler does not work

I am trying to install C and C++ compiler on Windows 8.
I have already installed MinGW, downloaded and installed mingw32-base and mingw32-gcc-g++ packages and the other required packages, added to the user environmet Path the string "C:\MinGW\bin", and I also tried following this guide
http://www.mingw.org/wiki/Getting_Started
so after installation I created a new file named "fstab" containing "C:\MinGW /mingw" in C:\MinGW\msys\1.0\etc because it was only present the file named "fstab.sample".
After all this I typed on cmd : gcc sample.cpp -o sample.exe
and it doesn't work at all returning me an error:
gcc: fatal error: no input files
compilation terminated
But if I typed gcc --version it gives me gcc 4.9.3 etc...
so the packages are installed but I can't compile any of my programs
Are you in the correct directory in cmd when compiling?
You can navigate to the correct one with cd and check if your .cpp is there with ls
possibly some problem with folders. probably you are not compiling from source directory.
Check out this mingw distro by STL. It contains a few .bat files that open a cmd prompt with compiler in PATH. I use these for all my mingw installations. BAT files are to be placed in mingw folder:
mingwvars.bat
#echo.
#echo Setting up environment for using MinGW with GCC from %~dp0.
#set PATH=%~dp0bin;%PATH%
set_distro_paths.bat
#echo off
if not exist "%~dp0bin\gcc.exe" goto epicfail
if "%X_DISTRO%" == "nuwen" goto :eof
set X_DISTRO=nuwen
if exist "%~dp0git\cmd\git.exe" set PATH=%~dp0git\cmd;%PATH%
set PATH=%~dp0bin;%PATH%
goto :eof
:epicfail
color 4f
echo ERROR: You must run %~nx0 from the root of the distro.
echo Don't copy or move this batch file.
title ERROR
goto :eof
open_distro_window.bat
#echo off
if not exist "%~dp0bin\gcc.exe" goto epicfail
if "%X_DISTRO%" == "nuwen" goto :eof
set X_DISTRO=nuwen
if exist "%~dp0git\cmd\git.exe" set PATH=%~dp0git\cmd;%PATH%
set PATH=%~dp0bin;%PATH%
goto :eof
:epicfail
color 4f
echo ERROR: You must run %~nx0 from the root of the distro.
echo Don't copy or move this batch file.
title ERROR
goto :eof
It's a very common error when you are not in the current directory where your source files are residing.
So just navigate to the current directory and run the command it will compile the source code.

Windows XP Batch IF & XCOPY

I've been struggling with trying to get the below (example) batch file to work on Windows XP SP3. The IF NOT EXIST part seems to work, but I keep receiving the "Does <path\filename.ext> specify a file name or directory name on the target" message despite using the /I flag on XCOPY:
#IF NOT EXIST "\\SERVER\PATH\TO\FILE DIR" (
MKDIR "\\SERVER\PATH\TO\FILE DIR"
XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
) ELSE (
XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
)
My understanding is that with the /I switch, XCOPY should create the directory structure if it doesn't exist - at least it does when I don't specify a file name. Unfortunately for the requirements of this project, I must specify a file name and cannot keep the original as it's a template file that gets manipulated with an automated process every day.
So, I tried to get around the issue with XCOPY and the directory path not existing by checking for the existence of the path, and if it's not there, creating it with the MKDIR command and then copying the file - but XCOPY still prompts as to whether the destination is a file or directory, which doesn't make sense but maybe I'm missing something.
Just to be clear, this is on Windows XP SP3.
Any ideas?
You might find it easier to do something like this:
md "\\SERVER\PATH\TO\FILE DIR" 2>NUL
copy "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"
The initial 'md' will attempt to create the directory. If it already exists, it will output an error message to STDERR. The 2>NUL redirects that to Windows' built-in "null device", which is to say, it just swallows the error message. Assuming you have the appropriate permissions, you can be sure that this directory exists now.
The copy command just copies your file. No need to use xcopy to copy a single file - that's both overkill and fraught with little gotchas like being prompted whether it's a file or directory.
Since the destination file doesn't exist before you copy, xcopy isn't sure if it needs to create a new directory called "FILE FINAL.XLSM", and put the file in there. By the way, since you already create the destination dir, you don't need the /I on your xcopy. Here are a couple ways to do what you want:
echo F | xcopy .... (feed the "F" answer to xcopy)
copy .... (you don't need to use xcopy for a single file)
echo f|XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"
should copy the file AND create the directory. No idea why the option to specify "this is a file" wasn't made available, but RTFM - the /i switch is only effective if you are copying MORE than one file, and specifying \ as the last character of the destination name tells XCOPY that the target is a directoryname in any case, so /i sems redundant.
However, be careful if you follow the copy route. It's better in general to use copy /b because plain copy may fail to properly copy some filetypes (like .MPGs) - it may stop on the first ^Z. copy /b appears safe however.

Post-Build script throws error in VIsual Studio 2010

I've seen several threads with the same issue, but none of the solutions seems to work for me so I'm trying it here.
I need a post-build script in VS2010 that moves a .lib file to a directroy (which possibly dosn't exist yet; if so create it).
I am using this, which returns error code 2:
xcopy /y "$(TargetDir)$(ProjectName).lib" "$(SolutionDir)lib\$(ProjectName).lib"
Also tried, which returns error code 1: (what is the difference?)
copy /y "$(TargetDir)$(ProjectName).lib" "$(SolutionDir)lib\$(ProjectName).lib"
The most common issues people seem to hav is the lack of quotes on paths, but I have that.
Why dosn't it work?
This ought to be closer:
if not exist "$(SolutionDir)lib" md "$(SolutionDir)lib"
xcopy /y /d "$(TargetPath)" "$(SolutionDir)lib"
After a quick test on the command line what's happening with copy is it is failing because the directory does not exist. What is happening with xcopy is it is failing when it prompts for whether the target is a file or directory when it finds the directory doesn't exist. /-Y may be set in your COPYCMD environment variable or your target path may be misleading causing a prompt for whether the target is a directory or file which is not supressed by the /Y flag for overwrite.
Example: xcopy /Y "C:\test.txt" "missingdirectory\test5.txt"
Obviously the easiest solution is to check if the directory exists and create it if it's missing before doing the copy in your post-build script.