How to use MAKECAB.EXE - windows

The syntax is:
MAKECAB [/Vn] [/D variable=value ...] [/L directory] source [destination]
MAKECAB [/Vn] [/D variable=value ] /F directives_file [...]
If no directives_file is used and the last parameter destination is specified, e.g. as "C:\Test", the cab will be generated in C:\Test.
But when a directives_file is used, the cab is generated in a subfolder \disk1.
What should be specified in the directives_file to have the same result as in the first case, i.e. the cab is generated in a specified folder?

I figured it out:
To get the cab with the name MyCabinet.cab in the folder C:\MyCabFiles, the following two lines must be included in the directives_file:
.Set CabinetName1="MyCabinet.cab"
.Set DiskDirectoryTemplate="C:\MyCabFiles"

Related

Rename multiple files with batch windows

I need to rename a group of .pdf files
In the \Source folder I have the files:
bbbbbbbbb-56.pdf
vduysdvss-60.pdf
sdvbdsbvv-80.pdf
I have to rename them in the \Destination folder like this:
11111111-bbbbbbbbb-ggg-hhh-56-dddd.pdf
11111111-vduysdvss-ggg-hhh-60-dddd.pdf
11111111-sdvbdsbvv-ggg-hhh-80-dddd.pdf
so I need to insert some fixed parts:
before the name
in the middle of the name
at the end of the name.
Using the command:
cd \Source
copy "\Source" "\Destination"
cd \Destination
ren *.pdf 11111111-?????????-ggg-hhh???-dddd.*
the result is:
11111111--56-ggg-hhh-dddd.pdf
the bbbbbbbbb string disappears
can you help me?
Thanks
By using the following command Copy the files from Source to Destination :
copy "/Source_folder" "/Destination_folder"
Go in /Destination folder
cd "/Destination_folder"
And then ren the file names by the following command :
ren *.pdf ???-new_filename_part.*
(The question mark (?) is also a wildcard, but it represents a character of the original file name. So, in the syntax, we're using three question marks, which means that the output file name will include the first three characters of the original file (which works as a unique identifier to avoid duplication)
(According to your logic you can change the new filenames by using some RegExpressions or some variables)

Winrar command line not deleting recursive when not using Wildcards

I'm trying to delete some files in a WinRar Archive using the Command Line.
The Rar-File:
/testing.rar
/testing.rar/some-data.txt
/testing.rar/testing/some-data.txt
Here's my Code:
cd "C:\Program Files\WinRAR\" && rar d -cl -r "c:\full\path\testing.rar" some-data.txt
It only deletes the some-data.txt file in the root, not inside /testing/
When using
cd "C:\Program Files\WinRAR\" && rar d -cl -r "c:\full\path\testing.rar" some-data.*
(changed the extension to .*) it does delete both files.
Am I doing something wrong?
I looked on first version of great solution of MC ND and thought by myself what happens if there is by chance although very unlikely a directory with name some-data.txt inside the archive?
The answer is that this directory with all files and subdirectories is also deleted because of also listed by Rar.exe and findstr does not filter out the list entry of the directory as it looks like exactly like a file name entry in bare list format.
The solution for code written by MC ND is to use the Rar command lb with switch -ed to filter out directory entries on list output. This small modification was applied to the command line by MC ND in his answer.
Then I thought about using the switches -ed and -x"..." to filter out directories and file names which should not be deleted on running Rar.exe with command d and a some-data.txt wildcard pattern.
And here is the single line solution:
"C:\Program Files\WinRAR\Rar.exe" d -ed -x"some-data.txt?*" -- "C:\full\path\testing.rar" "some-data.txt*"
The asterisk at end of file name is necessary to delete all files with name some-data.txt inside the archive including the files in subdirectories.
The switch -ed excludes all directories in archive including those which by chance are named some-data.txt. Directories in archive starting with some-data.txt and with more characters appended like a directory with name some-data.txt_dirare automatically ignored by Rar.exe with used file name wildcard pattern.
The switch -x"some-data.txt?*" results in excluding files starting with some-data.txt and having more characters appended like some-data.txt1 and some-data.txt10. File names like first_some-data.txt are automatically ignored by Rar.exe with used file name wildcard pattern.
The switch -cl to convert file names to lower case is ignored by Rar on using command d and therefore not used in the command line above. Rar interprets the file names case-insensitive like Windows.
Update:
Eugene Roshal, owner of win.rar GmbH, told me by email an easier method to delete a file in root archive folder and in all subfolders:
"C:\Program Files\WinRAR\Rar.exe" d "C:\full\path\testing.rar" "some-data.txt" "*\some-data.txt"
This command line deletes only some-data.txt in root archive folder (last but one argument) and in all its subfolders (last argument). Files in a folder with name some-data.txt are not deleted by this command line, but an empty folder with name some-data.txt would be also removed from archive. The switch -ed can be used additionally to prevent deletion of an empty folder some-data.txt.
While the d command can not handle it, a simple pipe can deal with it
#echo off
setlocal enableextensions disabledelayedexpansion
set "rar=C:\Program Files\WinRar\rar.exe"
set "archive=c:\full\path\testing.rar"
(
%= List archive contents =%
"%rar%" lb -ed "%archive%"
)|(
%= filter the list for the file in any subfolder =%
findstr /i /e /l /c:"\somedata.txt"
%= and include the root file =%
echo somedata.txt
)|(
%= Delete from archive the list of files read from stdin =%
"%rar%" d -cl -n# "%archive%"
)
The second step (filter the list of files in archive) is splited in the findstr and the echo just to prevent the case when the file to be deleted is not present in the output. Without a list of files the -n# modifier (read files to delete from stdin) will not read anything and all the archive contents will be removed.

Batch command XCOPY prompts me: Is the destination file or directory

When I run this file:
xcopy .\*.odt .\source.zip
I am prompted to specify what source.zip is:
xcopy .\*.odt .\source.zip
Does .\source.zip specify a file name
or directory name on the target
(F = file, D = directory)?
In my case when it find the .odt file to copy the file and place in the same directory but with new name source.zip. Is there approach to avoid the prompting since I always want destination to be a file not directory.
Any .odt file (being in .zip format in fact) is a binary file, see OpenDocument Document Representation
As a collection of several sub-documents within a package, each of which stores part of the complete document. This is the common
representation of OpenDocument documents. It uses filename extensions
such as .odt, .ott, .ods, .odp ... etc. The package is a
standard ZIP file with different filename extensions and with a
defined structure of sub-documents. Each sub-document within a package
has a different document root and stores a particular aspect of the
XML document. All types of documents (e.g. text and spreadsheet
documents) use the same set of document and sub-document definitions.
Therefore, you need to treat it as a binary file (read copy /?):
copy /B .\*.odt .\source.zip
Above command would work smoothly only if there will be only one file with extension .odt. Otherwise, it will prompt you for Overwrite .\source.zip? (Yes/No/All):. To stay on the safe side:
from command line for %G in (.\*.odt) do copy /B "%G" ".\source_%~nG.zip"
from a batch script for %%G in (.\*.odt) do copy /B "%%G" ".\source_%%~nG.zip"
%~nG (or in batch %%~nG) explanation: read Parameter Extensions.

Windows batch - concatenate multiple text files into one

I need to create a script, which concatenates multiple text files into one.
I know it's simple to use
type *.txt > merged.txt
But the requirement is to "concatenate files from same day into file day_YYYY-DD-MM.txt" I am a Linux user and Windows batch is hell for me. It's Windows XP.
Windows type command works similarly to UNIX cat.
Example 1: Merge with file names (This will merge file1.csv & file2.csv to create concat.csv)
type file1.csv file2.csv > concat.csv
Example 2: Merge files with pattern (This will merge all files with csv extension and create concat.csv)
When using asterisk(*) to concatenate all files. Please DON'T use same extension for target file(Eg. .csv). There should be some difference in pattern else target file will also be considered in concatenation
type *.csv > concat_csv.txt
At its most basic, concatenating files from a batch file is done with 'copy'.
copy file1.txt + file2.txt + file3.txt concattedfile.txt
In Win 7, navigate to the directory where your text files are. On the command prompt use:
copy *.txt combined.txt
Where combined.txt is the name of the newly created text file.
Place all files need to copied in a separate folder, for ease place them in c drive.
Open Command Prompt - windows>type cmd>select command prompt.
You can see the default directory pointing - Ex : C:[Folder_Name]>.
Change the directory to point to the folder which you have placed files to be copied, using ' cd [Folder_Name] ' command.
After pointing to directory - type 'dir' which shows all the files present in folder, just to make sure everything at place.
Now type : 'copy *.txt [newfile_name].txt' and press enter.
Done!
All the text in individual files will be copied to [newfile_name].txt
I am reiterating some of the other points already made, but including a 3rd example that helps when you have files across folders that you want to concatenate.
Example 1 (files in the same folder):
copy file1.txt+file2.txt+file3.txt file123.txt
Example 2 (files in same folder):
type *.txt > combined.txt
Example 3 (files exist across multiple folders, assumes newfileoutput.txt doesn't exist):
for /D %f in (folderName) DO type %f/filename.txt >> .\newfileoutput.txt
We can use normal CAT command to merge files..
D:> cat *.csv > outputs.csv
cat "input files" > "output files"
This works in PowerShell, which is the Windows preferred shell in current Windows versions, therefore it works. It is also the only version of the answers above to work with large files, where 'type' or 'copy' fails.
Try this:
#echo off
set yyyy=%date:~6,4%
set mm=%date:~3,2%
set dd=%date:~0,2%
set /p temp= "Enter the name of text file: "
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%temp%.txt
This code ask you to set the name of the file after "day_" where you can input the date.
If you want to name your file like the actual date you can do this:
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%yyyy%-%mm%-%dd%.txt
You can do it using type:
type"C:\<Directory containing files>\*.txt"> merged.txt
all the files in the directory will be appendeded to the file merged.txt.
copy is definitely much faster than type - but it sometimes (with large files?) adds a SUB character at the end of the file. So, strictly speaking, it does not simply concatenate the files in the same way as cat in Unix.
So, the correct answer is to use cat - either in something like Git Bash (where it has the same syntax as in Unix), or PowerShell (where it does not).

Using MakeCab.exe & .ddf file (i.e. using directive file) how to specify destination cab filename?

I have decided to use makecab.exe for my requirement to create cab files in my application.
http://msdn.microsoft.com/en-us/library/bb417343.aspx#microsoftmakecabusersguide
And I need to store files as per given path whether relative or absolute inside cab and extract files with original path preserved. So I will be using directive file for this and give that input to makecab command.
windows command promt zip/compress directory
But the cab output is stored in folder Disk1\1.cab.
Please help me to know how can I specify a desired cab filename as destination (I will take cab file name from user input)?
Also, is it possible to append more files to existing cab file in second run of makecab command?
Finally found the solution.
You can set following two variables in the directive file which will identify destination path and name of cab file:
DiskDirectoryTemplate=template [Output directory name template; * is replaced by disk number]
CabinetNameTemplate=template [Cabinet file name template; * is replaced by Cabinet number]
Description from Microsoft MakeCAB User's Guide
DiskDirectoryTemplate=template
Set the output directory name template. One directory is created for
each disk of the layout.
Default: .Set DiskDirectoryTemplate=DISK* ; Default is DISK1, DISK2,
etc.
As MakeCAB processes a directive file, it will create one or more
disk "images". Rather than using some specific disk format, however,
MakeCAB simply creates one subdirectory for each disk and places the
files for each disk in the appropriate directory. If a * exists in
this variable, then it is replaced with the disk number. If no * is
specified, then all files are placed in the single directory specified
by this variable.
This variable is used only if no variable DiskDirectoryn exists for
disk n.
Examples:
.Set DiskDirectoryTemplate=C:\EXCEL6\DISK* ; Put files in separate dirs
.Set DiskDirectoryTemplate=C:\EXCEL6 ; Put all files in C:\EXCEL6
.Set DiskDirectoryTemplate= ; Put all files in current dir
CabinetNameTemplate=template
Sets the cabinet file name template.
Default: .Set CabinetNameTemplate=*.CAB ; 1.CAB, 2.CAB, ...
This template is used to construct the file name of each cabinet. The *
in this template is replaced by the cabinet number (1, 2, etc.). This
variable is used only if no variable CabinetNamen exists for cabinet
n.
NOTE: Be sure that the expanded cabinet name does not exceed the
limits for your file system! For example, if you used "CABINET*.CAB",
and MakeCAB had to create 10 or more cabinets, then you would have
cabinet names like CABINET10.CAB, which is 9.3, which is an invalid
name in the FAT file system. Unfortunately, MakeCAB would not detect
this until it had already created 9 cabinets!
Examples:
.Set CabinetNameTemplate=EXCEL*.DIA ; EXCEL1.DIA, EXCEL2.DIA, etc.
.Set CabinetNameTemplate=*. ; 1, 2, 3, etc.
In addition, when files are split across cab (in case if cabinet size exceeds its limit size) then you can set subsequent cabinet filenames also using variable CabinetNamen:
CabinetNamen=filename [Cabinet file name for cabinet number n]
CabinetNamen=filename
The cabinet file name for the specified cabinet.
Default: ; By default none of these variables are defined
If this
variable is not defined for a particular disk, then MakeCAB uses the
CabinetNameTemplate to construct the cabinet name.
Example:
.Set CabinetName2=test2.cab
If you know exactly where your source files exist, you can generate a file list from the directory:
dir C:\FolderName /s /b /a-d > c:\temp\files.txt
Then use the files.txt file to create the cab file.
makecab /d CabinetName1=test.cab /D DiskDirectoryTemplate=C:\temp /f c:\temp\files.txt
The above command will generate a test.cab file in your C:\Temp folder using the file list generated earlier.
Additional helpful reference:
Microsoft Cabinet Reference. and
makecab.exe details.

Resources