*.* as pattern for folders and files - windows

In batch file scripts and the doc pages (e.g. here) I often see *.* as (I guess) a way to specify multiple folder/ file names. My question is: How exactly this string *.* is interpreted by cmd.exe?
I know that specify folder/ file names two special characters can be used:
* means any number of character (including zero)
? one character
So *.txt would mean all files with extension .txt in the current directory. In light of this, I would read *.* as any folder/file name that has . (dot) in it.
Why then when I run DIR *.* in a folder that has only a subfolder named folder and a file named script.txt, it displays folder and script.txt instead of just showing script.txt?

Related

Batch File to copy files from one subdirectory to another subdirectory that contains a "_" character

I've been struggling to get this script working and have been doing a lot of searching but can't seem to figure out how to do.
I have a specific directory and this directory contains two additional subdirectories, "User Data" and "a_bcdefg". The first subdirectory name is static but the 2nd subdirectory name is always random characters but always contains a single character at the start then an underscore "_". I am trying to copy all the files from "User Data" into "a_bcdefg" without having to specify the directory since the name is always different.
I've been trying various iterations of the following but can't get it to work:
xcopy /e /c "C:\temp\test\User Data\*.*" "C:\temp\test\*_*"

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.

Copy single file instead of entire directory in windows batch

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?

Why does xcopy not copy files when using these parameters?

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.

Resources