Merge files with the same prefix - concatenation

I have 324 files. The format of the file names is: 1.fa.abc.txt, 1.fa.de.txt, 1.fa.zen.txt and 2.fa.abc.txt, 2.fa.de.txt, 2.fa.zen.txt and 105.fa.abc.txt, 105.fa.de.txt,105.fa.zen.txt. All of these files are in the same folder. I want to merge all the files with the same prefix (the numbers in the prefix, in this case). Is there a quick way to do it?

This code in a bat file will merge all the files with the same prefix from 1 to 324, storing every merged file (from the prefix) in a different result.txt:
FOR /L %%G IN (1,1,324) DO copy %%G*.* %%Gresult.txt

Related

Batch file to copy multiple files by using partial filename

I want to copy multiple files from source to destination and the first 8 letters of the files are same so by using this partial file name i would like to copy and content of the files also should be replaced with string while copying.
for example:
I have three file in source path and those are like below
samefile123.txt
samefile456.txt
myfile.txt
ratefile.txt
I want to pick up the first two files because the first 8 letters of the files are same and if the files have content like apple or banana or grape words those words should be replace with papaya.
To copy multiple text files with the same name using batch you need to use a wildcard. Here is what I have tested on my computer and it works.
xcopy "C:\%username%\desktop\test\samefi*.txt" "C:\users\%username%\desktop"
For more information on the uses of xcopy use:
xcopy /?
You can read more on wildcards here.
If I could get more information on the second part about changing the name from grape to papaya I may assist with that.

Putting pairs of files into separate zip archives

I have in a single folder pairs of files with the same filename and different extension.
ex.:
filename1.txt
filename1.docx
filename2.txt
filename2.docx
filename3.txt
filename3.docx
filename4.txt
filename4.docx
etc...
I would like to select all those files and put the pairs in separate zip files.
ex.:
filename1.zip (containing "filename1.txt" and "filename1.docx")
filename2.zip (containing "filename2.txt" and "filename2.docx")
filename3.zip (containing "filename3.txt" and "filename3.docx")
filename4.zip (containing "filename4.txt" and "filename4.docx")
filename5.zip (containing "filename5.txt" and "filename5.docx")
etc...
Is that even possible using 7-zip command line and a prepared batch file?
Double the % to %% for use in a batch file.
FOR %f in (*.txt) DO 7za a "%~nf.zip" "%~nf.txt" "%~nf.docx"

Batch - Extract many zip, rename files extracted in a same directory

Hi, I have many zip files located at g:\toto. These zips contain some files. I would like to extract all zip in a same directory (g:\toto\extracted) then rename various files of the zip.
Example 1 :
www_12567.vp.zip : 3 files : alpha.doc, beta.xls, teta.doc
I would like after extraction, files are renamed with the name of the zip
www_12567.vp.alpha.doc, www_12567.vp.beta.xls, www_12567.vp.teta.doc
Example 2 :
www_12.vp.zip : 3 files : al.doc, bea.xls, tta.doc
www_12.vp.al.doc, www_12.vp.bea.xls, www_12.vp.tta.doc
I found this question, but it talks about .txt and the zip contain one file, so, it doesn't work.
Without knowing the contents of the archive you can't know which files to rename, because you are putting them into a directory that may already contain other files.
This, however, would be much easier if there was a dedicated directory to put the files temporarily. Here's how you could use it:
#ECHO OFF
SET "srcdir=G:\toto"
SET "tgtdir=G:\toto\extracted"
SET "tmpdir=G:\toto\extracted-tmp"
FOR %%Z IN ("%srcdir%\*.zip") DO (
unpack "%%Z" with your favourite tool into "%tmpdir%"
FOR %%I IN ("%tmpdir%\*") DO MOVE "%%I" "%tgtdir%\%%~nZ.%%~nxI"
)
Of course, the temporary directory would need to be empty before running the batch file. You could add DEL "%tmpdir%\*" somewhere before the loop to make sure it is.
One other note is, the above assumes that the archives do not contain subdirectories or, at least, that the files are extracted without subdirectories.
UPDATE
If you are using the 7-Zip archiver to work with .zip files, then this is how your extract command might look:
7z e "%%Z" -o"%tmpdir%"
Disclaimer: I'm not an active user of 7-Zip. This is what I used as a reference to come up with the above command:
7-Zip Command-Line Examples

How to exclude a specific file from being copied using xcopy

I'm new to Windows batch files, but I'm writing a .bat file that just copies a bunch of files from one place to another maintaining the file directory structure. Using xcopy this is simple but I need to exclude some files from being copied. You can use /exclude and create a text file full of strings that you want to be excluded, but this doesn't just exclude files with the exact names in the text file, it excludes all files whose filenames contain any of the strings in the text file.
What this means is, if I want to exclude any files named 123.txt and put this string in my exclusions text file, if there was a file called 1123.txt anywhere in the source folder or any of its subfolders that would also be excluded.
How can I exclude only files with a specific filename from being copied?
Evening Bill.
Can you add a slash before each file name? That should work
EG
instead of
123.txt
blah.txt
use
\123.txt
\blah.txt
Try creating a temporary folder, xcopying all of the files into that folder, deleting the ones you don't want, then xcopying those to the final destination. Finally, delete the temporary folder and its contents with rd xyzzy /q/s

How to make a batch work with long path names and spaces for a For Loop with subdirectories?

How to make a batch work with long path names and spaces for a For Loop with sub-directories ?
paths to files will be over 200 or 300 letters.
What i m trying to do is to bulk convert multiple files with a program that let me insert the input file directory path+name and the output file directory path+name in a .INI that comes with the program.
i will need to use a For Loop to get the directory path and file name and then insert it in the ini.
Problems : files are deeply nestled and file path contains spaces.
I don't particularly see your problem, honestly. Spaces are hardly special in file or folder names, so in any case you need to quote your paths. So
for /r %%x in (...) do (
dosomething "%%x"
)
instead of just dosomething %%x should do it, as usual.

Resources