Copy single file instead of entire directory in windows batch - windows

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?

Related

Windows Script to Recursively Copy Files and add directory name prefix

I'm need a script to recursively copy a set of files from a folder and all the subfolders to a new folder. I do not want the directory structure copied. But I want to add the original directory name (not the full path) as a prefix to the copied file.
For example...copy
c:/photos/date1/photo1.jpg
c:/photos/date1/photo2.jpg
c:/photos/date1/photo3.jpg
c:/photos/date2/photo1.jpg
c:/photos/date2/photo2.jpg
c:/photos/date2/photo3.jpg
to:
c:/newfolder/date1-photo1.jpg
.....date1-photo2.jpg
.....date1-photo3.jpg
.....date2-photo1.jpg
.....date2-photo2.jpg
.....date2-photo3.jpg
I have this for loop to do the original copy which works just fine. But I am stuck at how to add the directory name as a prefix.
for /r %d in (*) do copy "%d" "x:\newfolder"
Appreciate advise/suggestions.

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.

Why file cannot be found to copy

I am writing a batch script which will copy a file from a folder into the C:\ drive:
#ECHO ON
COPY C:\RANDOMFILES\Weekly Reprort_Hew*.xls C:\Weekly Reprort_Hew???????????.xls
The filename in the RANDOMFILES folder is: Weekly Reprort_Hew, 6-29-2014 10-30-00 PM-642.xls (The date and time and the number at the end will always change so I used the * in the filename being copied in the script)
When I run the batch script, I get the following message:
c:\RANDOMFILES>COPY C:\RANDOMFILES\Weekly Reprort_Hewlett*.xls C:\Weekly Reprort
_Hewlett???????????.xls
The system cannot find the file specified.
How can I fix the issue?
You need double quotes to handle spaces etc. Double check the spelling too.
#ECHO ON
COPY "C:\RANDOMFILES\Weekly Reprort_Hew*.xls" "C:\Weekly Reprort_Hew???????????.xls"
Don't know why it is not working - is it hidden? Maybe the spaces in the name?
The following will work though:
FOR %%I in (C:\RANDOMFILES\Weekly Reprort_Hew*.xls) DO COPY "%%I" C:\
The destination file name isn't necessary; if not otherwise specified, it will remain unchanged provided the destination is different. That may be why the plain COPY command isn't working.
Also:
"It is an not-so-well-known fact that the question mark wildcard will match exactly one character only when the wildcard does not appear at the end of a file name. " from http://www.thefriendlycoder.com/2011/11/24/batch-file-gotcha-question-mark-wildcard/

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