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.
Related
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)
this seems like a simple one but I can't find it anywhere!
I want to zip up a file with this command:
#"a -tzip -mx0 -v500m -mmt -- C:\Greg_Folder\zippedPackage.zip D:\tmp\failedImages";
This basically means that I'd like to zip up the folder "D:\tmp\failedImages" and move it to "C:\Greg_Folder\zippedPackage.zip"
THE PROBLEM: I want to zip to the "Greg_Folder" and not 'Greg Folder"... but underscores seem to be translated to spaces in the cmd line. So I need a way to say that I want the underscores to be treated literally as part of the directory name.
any ideas?
I just compressed some folder on my drive using your command without the # and the quotes.
"C:\Program Files\7-Zip\7z.exe" a -tzip -mx0 -v500m -mmt -- C:\Greg_Folder\zippedPackage.zip K:\devkitPro\devkitARM\arm-eabi
I just did not put the "#" sign which is for some other option (and your bug seems to be a strange side-effect), and it even created the "Greg_Folder" directory and the .zip.001 file inside.
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).
Let's suppose that I am in some directory with two subdirectories, a and b. a has two files in it: t1.txt and t2.txt. That is, I have the following directory structure:
/.
/a
t1.txt
t2.txt
/b
I want to copy the file t1.txt from the a directory into the b directory.
I tried the following command
copy /b a/t1.txt b/t1.txt
but it copies the entire a directory into the b directory.
Why does this happen, and how can I make it so that only the t1.txt file is copied?
When copying to a new directory, you only need to specify the new directory. So
copy /b a\t1.txt b
should work.
That said, I don't think additionally specifying the file name would cause the error you've described -- the official help text says "Destination can consist of a drive letter and colon, a folder name, a file name, or a combination of these," which to me implies that how you have it is fine.
I've also reversed the slashes -- were you using forward slashes in your batch file or is that a typo in the post? Maybe that was the problem?
I have a simple xcopy script that I'm running from the command line that reads a CSV file of directories and file names. I've used a very similar script with no problems before. Here is the script:
Z:\HOME\>for /f "delims=, tokens=1,2,3,4" %i in (Z:\HOME\MissingImages.csv) do
echo f | xcopy "Y:\%j\%k\%l" "C:\Horizon\%j\%k\%l" >> Z:\HOME\MissingImagesLog.txt
However, it is not copying any of the files over
Here is an entry from the log file:
Does C:\Horizon\K\00\6bef500f.IMG specify a file name
or directory name on the target
(F = file, D = directory)? f
0 File(s) copied
It's finding the images because if I change the root directory to something else the script will just populate the log file with 0 File(s) copied for all entries, so the files are there and can be seen...
Also, the Z:\ drive is on a network and not local, but again I have used a very similar script across a network without problems (it just takes longer).
I've tried different options like /i, /s, etc. but I can't seem to get it to copy any files over.
xcopy will also report 0 File(s) copied if you use forward slashes "/" in paths instead of backslashes "\", though ONLY if you've enclosed the path in quotes.
This fails with "0 File(s) copied"
xcopy "pathname1/file" pathname2\file
This fails with "Invalid number of parameters"
xcopy pathname1/file pathname2\file
This works just fine
xcopy pathname1\file pathname2\file
It asks because it doesn't know whether you want to copy to directory (to be created) or you provide the full target pathname.
This will ask:
xcopy pathname1\file.from pathname2\file.to
However, adding slash will tell that you copy to directory:
xcopy pathname1\file.from pathname2\to\
But I haven't found the way to tell explicitly that I want to copy and rename file, except
echo Y | xcopy pathname1\file.from pathname2\file.to
I played a bit with your case (with for, do and xcopy) and found out that even if it asks Does SOMEFILE specify a file name or directory name on the target (F = file, D = directory)? it is provided with f from echo and it's copied successfully. Thus, it's not a problem with file/directory specifying, but with copying through network itself.
Well, that's annoying; I found the issue. It looks like when I generated my CSV file, it put a space at the end of each line, so xcopy was looking for files that had a space after the extension.
The thing that was throwing me off was that it was finding the files, but couldn't copy them, making me think it was a network or xcopy issue.
I just ran a sed script to remove the eol spaces and the xcopy script is now working as expected.