Batch script to copy file with a changing name - windows

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.

Related

Generate error at the end of the copy if any files are not copied

I have the one batch that does the copy of some files in the network, this copy updates the previous files by the newest case they already exist in the destination folder, however if any file is in use the system does not present the error message at the end only in the Moment you are trying to update the file.
At the moment I do not want to solve this file problem being in use, I would only like the command to report only at the end of the copy if there was an error in updating some file.
I put in the end the condition if "%errorlevel%" == "0" but this condition does not work if there were errors in the middle of the copy.
My command:
xcopy "C:\origin\." "C:\destination\" /c /d /e /h /i /k /r /y
What I suggest for you is to loop through the folder and check if each file is in use, and then you can write the name of that file to some type of log, maybe a .txt file to read later to show what could not be overwritten.
2>nul (
>>FILE.EXT (call )
) && (echo file is not locked) || (echo file is locked)
This code, which I found at StackOverflow (dbenham deserves all the credit for this) could be used to check if a file is locked. Replace FILE.EXT with your file name, preferably in a loop to easily check every file, and if the file is locked echo the file name to a (temporary) .txt file and once completed, loop through that text file to list the results.
An added benefit to this is, if you check for files in use before trying to overwrite, you could skip files you know you can't write to thus saving a bit of time per file.

XCOPY with pattern matching

I am very new to Windows batch scripting so please ignore if it is stupid question, I have a requirement of copying set of files from source to destination only if source file is modified after certain date_time, I managed to do it using XCOPY command.
XCOPY C:\Src\*.txt C:\Target /D /S /Y
This runs fine for first run, now the twist is once destination folder file is processed it will be renamed to some other name and extension, so when next time my script runs it does not find same file name as source in the destination folder because it is processed and renamed. So is there a way in XCOPY or any other Windows command to do the pattern match for the file name int the destination folder and if match found then proceed with date_time check and copy or else ignore?
Example Source Dir Files:
a.txt
b.txt
c.txt
Destination Dir Files After first script run:
a.txt
b.txt
c.txt
Destination Dir Files after processing and before running the script second time:
a_201603071130.ok
b_201603071130.ok
c_201603071130.ok
For second run of the script with XCOPY it does not find the file a.txt in destination as it is processed but the requirement is to copy only if a.txt file is modified after last run.
I can do this by storing the last run time and checking that for next run etc. but I wanted to find out if there is any other way of doing it.
XCOPY C:\Src\*.txt C:\Target /D /S /Y /M
The /M switch changes the A flag for the file.

Batch file to create a text file with the same name as every .zip file in a directory

I'm not very familiar with windows batch scripts except very simple applications. I'm currently trying to figure out how to run a batch file in a local directory, scan the directory for each *.zip file and then create a corresponding .txt file (in the same local directory). This will run indefinitely at intervals of say... 5 seconds. Any clues to help? Would it be easier to write a Java program or something?
You can use the FOR command to iterate through files in a directory. The TYPE command creates an empty file.
#echo off
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
I got it:
#echo off
:loop
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
timeout /t 300
goto loop

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.

Want to execute command on each file in directory one at a time through batch file windows

I am currently doing this to execute a single command on a particular type of files in directory.
COPY *.prn /B \\\\{$PC}\\{$PRINTER}
The PC And Printer Part is redundant no need to understand that
Instead of executing all files at once I want to be able to do one file at a time through a loop
try this:
for %%i in (*.prn) do COPY "%%~i" /B \\\\{$PC}\\{$PRINTER}
Im not entirely sure what you mean but try this, it will execute the command once for each file in the current directory and (all subdirectories, but this exact snipets not ideal for subdirectories) ending with the extension .prn:
for /r %%a in (*) do (
if %%~xa == .prn (
copy %%~na%%~xa /B \\\\{$PC}\\{$PRINTER}
)
)
Tell me if this doesn't work or you want to do this for subdirectories as well.
Yours, Mona

Resources