copy +,, not working as expected: file is touched but also copied - visual-studio

In the pre-build event, VS2017, I added the following lines to "touch" my program so it would compile and get the date/time of compilation for my "build info" that is shown in my about infobox.
copy /b "$(ProjectDir)\MyAbout.cs"+,,
echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"
This has been working perfectly but I noticed I have a copy of "MyAbout.cs" in the binary directory where the executable resides. I was under the impression that the copy would not take place if no arguments were supplied.
Windows 10

The file is copied instead of touched if the destination directory does not match the source directory. The default destination is the current directory.
The solution is simple:
copy /b "$(ProjectDir)\MyAbout.cs"+,, "$(ProjectDir)"
or
pushd "$(ProjectDir)"
copy /b MyAbout.cs+,,
popd

Related

Batch file for loop directory search result changes when clicking on the batch file versus dragging a folder onto the batch file

Problem statement: Dragging a folder onto a batch file changes the result of a for loop while simply clicking on the batch file results in different but desired output from the mentioned for loop.
Background:
I would like to retrieve the latest created .jar file's name in a folder. A .bat file will be located in this folder where the .jars are. With the example .bat file code provided below, I double click the .bat file and get the result that I am looking for e.g. : {jar_root_dir_path}+"logger-20191030.jar", see the "Desired output:" section below for clarification.
Thing is that this .bat file will only be used in the case where someone would drag a folder onto it and then pass this information to the .jar file. This is where my problem comes in. When I drag a folder onto the batch file, I do not get any file results for the latest .jar file. Refer to the "Current output:" section below to see the output of the problem.
Code:
#Example code
#echo off
for /f "delims=" %%x in ('dir /od /b logger*.jar') do set latestjar=%%x
echo "%~dp0%latestjar%" + "%~dp0output.txt"
pause
Output:
Note: {example_path_here} in this question only acts as a placeholder for actual directory / file paths e.g. {example_dir} = "D:\Installation\Logger\"
Current output:
File Not Found
{root_directory_of_.bat} + {root_directory_of_.bat_with_output_file_name_appended}
Press any key to continue . . .
Desired output:
{root_directory_of_.bat_with_latest_.jar_file_appended} + {root_directory_of_.bat_with_output_file_name_appended}
Press any key to continue . . .
When you "drag something" to the batch file (I assume you mean drag onto the icon of the batch file or the batch file's name in Explorer), it is executed in another working folder (There probably are no .jar files in %windir%\system32).
Solution:
make sure, the working folder is what it should be. If it should be the same folder where your batch file resides, change the working folder with:
cd /d "%~dp0"
After analysis the following was determined:
A double click event will let the execution take place in the current folder where the .bat is located.
A folder drag and drop event caused execution to take place where the drag and dropped folder's parent directory is situated.
Point 2 is where the problem came in. To rectify this we "cd" back to the directory of the .bat file in order complete our original code.
Here is an example of the rectified example code of the question:
#echo off
cd /d %~dp0
for /f "delims=" %%x in ('dir /od /b logger*.jar') do set latestjar=%%x
echo "%~dp0%latestjar%" + "%~dp0output.txt"
pause
Here is the end-result of what I tried to achieve:
#echo off
cd /d %~dp0
for /f "delims=" %%x in ('dir /od /b logger*.jar') do set latestjar=%%x
java -jar "%~dp0%latestjar%" %1 > "%~dp0output.txt"
Explanation of last code segment / end-result:
- A folder is dropped onto a batch file in the file explorer -
1. The code resets back to the .bat file directory
2. We iterate through similarly named .jar files in the directory and find the most recently created one.
3. We run the latest .jar file, with an argument which is the path of the drag and dropped folder and then we output the .jar's output to an output.txt file located in the same folder as the batch file.

Use embedded file from .exe

I made an exe that has embedded files in it like a portable 7zip (7za.exe) and I want to call to it in the batch script that I am compiling into an exe but when I do it just gives me "7za.exe" is not recognized as an internal or external command. If I left anything out just ask.
(Sorry if this is an easy fix I am just messing around with some basic code)
This is the code I am working with and exe is in releases tab.
https://github.com/iamtis/mass-extract
Let us look on batch file with some additional lines at top:
#echo off
echo Current working directory is: %CD%
echo Directory of batch file is: %~dp0
pause
echo Files in current working directory:
dir /A-D /B
pause
echo Files in directory of batch file:
dir /A-D /B "%~dp0"
pause
I suppose that the current working directory is not equal the directory of the batch file and the tools are in the directory of the batch file. I suppose the batch file directory is a subdirectory with random name in %TEMP%.
So what you most likely need is:
#echo off
set "ToolPath=%~dp0"
if not exist "%CD%\archive\*" md "%CD%\archive"
"%ToolPath%7za.exe" x "%CD%\*.zip" "%CD%\archive\"
"%ToolPath%7za.exe" x "%CD%\*.7z" "%CD%\archive\"
"%ToolPath%unrar.exe" x "%CD%\*.rar" "%CD%\archive\"
"%ToolPath%7za.exe" a -mx9 archive.7z "%CD%\archive\"
rd /S /Q "%CD%\archive"
set "ToolPath="

how to copy directory of specific files in batch file

c:/--> folder1-->
folder2->
img001.png
img002.jpg
img003.png
I having kind of folder structure.
I need to copy a single file from this folder to Destination folder.
source : "c:\folder1\folder2\imgoo1.png"
Destination:"D:\folder1\folder2\imgoo1.png"
need output:
D:/--> folder1-->
folder2->
img001.png
Note:I need batch file format
robocopy "c:\folder1\folder2" "d:\folder1\folder2" "img0001.jpg"
Since robocopy is not included in windows XP, this can be done with plain xcopy
xcopy "c:\folder1\folder2\img0001.jpg" "d:\folder1\folder2\"
for %%f in (img001.png img002.jpg img003.png) do copy /b "c:\folder1\folder2\%%f" "d:\folder1\folder2\"
Note that thw directory separator in windows is \, not /. / is used as a command-switch - /b in the above case means "copy in binary mode'.
Note that you do not say whether the batch should check whether the destination directory exists or whether the destination filename already exists.
md "d:\folder1\folder2" 2>nul
will force the destination filename to exist (2>nul suppresses the 'already exists ' message)
You can add an extra switch /y to the copy command to force overwrite in the case that the destination file already exists.
You can add >nul to the copy command to suppress the 1 file copied message.
This will copy that one file. The target filename is not required but can be left in.
copy "c:\folder1\folder2\imgoo1.png" "D:\folder1\folder2\imgoo1.png"
This assumes that the folders already exist.

batch file to execute a command on all files in multiple directories

I would like to make a batch file that runs this command:
C:\Program Files (x86)\IrfanView\i_view32.exe" "C:\Users\digi_admin\TIFFs\
OLD DIRECTORY\*.tif" /ini="C:\Users\digi_admin\Documents\" /advancedbatch /tifc=4
/convert="C:\Users\digi_admin\CompTIFs\Some Folder\NEW DIRECTORY\*.tif"
On all the files in several folders. All the files will be located in \TIFFs\ but will reside in several different sub-folders. (OLD DIRECTORY) Also I need to create the folder \NEW DIRECTORY\ (which will have the same name as OLD DIRECTORY) before the command is ran (is run?). Here is what I have so far:
FOR /D %d IN ("C:\Users\digi_admin\TIFFs\*.*") DO "C:\Program Files
(x86)\IrfanView\i_view32.exe" %d\*.tif /ini="C:\Users\digi_admin\Documents\"
/advancedbatch /tifc=4 /convert="C:\Users\digi_admin\CompTIFs\Some Folder\%d\*.tif"
I have been trying it in command line so %d should be %%d. I am unfamiliar with DOS so I'm sure it is relatively simple. Any help would be greatly appreciated.
#ECHO OFF
SETLOCAL
SET destroot=C:\Users\digi_admin\CompTIFs
FOR /D %%d IN ("%destroot%\*") DO (
MD "%destroot%\%%~nxd" 2>nul
"C:\Program Files (x86)\IrfanView\i_view32.exe" "%%d\*.tif" /ini="C:\Users\digi_admin\Documents\" /advancedbatch /tifc=4 /convert="%destroot%\%%~nxd\*.tif"
)
Now - that's assuming the '/convert' is a destination filespec.
Notes:
I've set the destination root into a variable - makes the typing
easier
the 2>nul suppresses the error message generated should the
destination directory already exist
%%~nxd means the Name and eXtension from %%d. In all probability, the
extension won't exist - this is harmless

Batch script to copy file with a changing name

Our build produces an archive with the name app-component-x.x.x.x-SNAPSHOT.zip where x.x.x.x is a version number (ie: 1.6.2.8). Directory is c:\buildresults\app
We want to write a batch script that a) copies the file to another directory with a fixed name such as build-results.zip and then b) extracts the file.
I am not sure how to do part A. This doesn't seem to work: copy c:\buildresults\app\*.zip c:\xxx\build-results.zip
Any ideas?
Update:
File is being copied BUT the size is dramatically less. Ie: the file seems to be getting corrupted.
This seems to work but not ideal:
cd buildresults\app
for %%f in (*component*) do (
echo %%~nf
7za.exe -oC:\buildresults\app x "%%~nf.zip"
)
You could try:
xcopy /Y /Q /C /H /R c:\buildresults\app\*.zip c:\xxx\build-results.zip
Cannot reproduce:
D:\>mkdir xxx
D:\>echo test > test-1.2.3.zip
D:\>copy test-*.zip xxx\test-current.zip
test-1.2.3.zip
1 Datei(en) kopiert.
D:\>type xxx\test-current.zip
test
D:\>
Are you sure the target doesn't exist? Btw, are you sure your * matches exactly one file in every case? Because copying multiple files to one destination is a valid operation and will end up with an invalid zip file.

Resources