Using xcopy to copy files from several directories to one directory - msbuild

Is it possible to use xcopy to copy files from several directories into one directory using only one xcopy command?
Assuming that I have the directory tree
root\Source\Sub1\Sub2
I want to copy all .xml files from the directory root\Source including sub folder to root\Destination. I don't want to copy the folder structure, just the files.

As DandDI said, you don't need xcopy. for statement helps much. However, you don't need to state process outcome of dir command as well, this command helps better
for /R c:\source %f in (*.xml) do copy "%f" x:\destination\
By the way, when you use it from a batch file, you need to add spare % in front of variable %f hence your command line should be;
for /R c:\source %%f in (*.xml) do copy %%f x:\destination\
when you use it within a batch
Should surround %f with double quotes otherwise it will fail copying file names with spaces

You don't need xcopy for that.
You can get a listing of all the files you want and perform the copy that way.
For example in windows xp command prompt:
for /f "delims==" %k in ('dir c:\source\*.xml /s /b') do copy "%k" x:\destination\
The /s goes into all subdirectories and the /b lists only the files name and path. Each file inturn is assigned to the %k variable, then the copy command copies the file to the destination. The only trick is making sure the destination is not part of the source.

The Answer to this problem which I think is "How to gather all your files out of all the little subdirectories into one single directory" is to download a piece of software called XXCOPY. This is freely available via XXCOPY.COM and there's a free non-commercial version fortunately. One of the Frequently Asked Questions on the help facility on XXCOPY.COM is effectively "How do I gather all my files into one directory" and it tells you which switch to use. XXCOPY is though a surefire way of doing this and it comes in a .zip archive so unzipping it can be not that straightforward but it's not particularly difficult either. There is an unzipping program called ZipGenius available through the ZipGenius.it website so maybe before you download XXCOPY then download ZipGenius then it's a smallpart smalltime double wammy(!)

Might not be the exact answer but if anyone would like to do this without coding.
You can search the name of the item inside a specific folder, and then you can copy the results and later paste it into your desired folder. It will rename the same file to be the folder I believe as the prefix and then the repeated name.

Related

Back up script issues

Not entirely sure why our script isnt backing up, i suspect that it may be something with the Robocopy. Ik robo copy is a sys32 file but looking at the script its shown to be apart of another path then followed by it is the correct path c:\windows\sys32.
So im looking at the two "copy "lines. lines 2&3. Is that normal formatting, because the robocopy is not in that path.
if exist "C:\windows\system32\robocopy.exe" Goto SkipPrep
copy"%logonserver%\netlogon\tools\robocopy.exe""C:\Windows\system32"
copy"%logonserver%\netlogon\tools\sleep.exe" "C:\Windows\system32\
cls
:SkipPrep
This a bat file

Using variables to specify a part of a file name in CMD

I'm trying to move files from one portion a system to another using an inputted variable but for some reason xcopy won't use wildcards or variables.
set /p EIID = "Enter Aircraft EI_ID:"
xcopy C:\ULLSA\Data\ARCHIVE\IN\"%EIID%.LogbookData.qep" E:\ULLS_MEDIA\Data\3rdParty\ARCHIVE
xcopy C:\ULLSA\Data\ARCHIVE\IN\"%EIID%.LogbookData.qep" "E:\ULLS_MEDIA\Data\MigOut\%EIID%"
Have a go at this one mate. No need to quotations unless you believe there will be spaces in the file path. Also close your file path otherwise it wont work bud.
set /p EIID=Enter Aircraft EI_ID:
xcopy C:\ULLSA\Data\ARCHIVE\IN\%EIID%.LogbookData.qep E:\ULLS_MEDIA\Data\3rdParty\ARCHIVE\
xcopy C:\ULLSA\Data\ARCHIVE\IN\%EIID%.LogbookData.qep E:\ULLS_MEDIA\Data\MigOut\%EIID%\

Best way to process set variables in batch files

This question is probably unnecessary as I probably have been searching for the wrong criteria. It's pretty easy I think but I just can't find the answer. I use batch files to automate installs with my work, setting folder permissions, copying files here, and there, silently running programs, removing old ones etc. The batch files contain lots of repetition and I want to tidy them up greatly for easier management. I have decided to set the repeated commands and copy folder locations as variables then use them instead. This is all fine but I'm adding the variables as a LIST which looks rubbish to me, I'm sure I can load them together in a sentence rather than a new line for each. Here's what I mean...
set dir1=md c:\newfolder
set killtask=taskkill /im someprog.exe /f >nul 2>&1
set config=echo F| XCOPY %~dp0configfile3.cfg /y C:\newfolder
And on and on...
So at the minute my batch now looks like this
JOB1
%dir1%
%killtask%
%config%
What I want to be able to do is have it like this (to reduce length if batch file etc.)
JOB1
%dir1% %killtask% %config%
(summary of my comments to the question):
Check your variables! Some chars like pipe (|) or redirection (>) change how the line is interpreted. Use this syntax to correct it:
set "killtask=taskkill /im someprog.exe /f >nul 2>&1"
(note the quotes and their position)
you can call several commands with &:
echo hello&echo world.
With your complex variables this would probably fail/work in an unexpected way. Try
(%dir1%)&(%killtask%)&(%config%)
so that the redirections and pipes stay at their intended commands.

batch scripting: how to get parent dir name without full path?

I'm working on a script that processes a folder and there is always one file in it I need to rename. The new name should be the parent directory name. How do I get this in a batch file? The full path to the dir is known.
It is not very clear how the script is supposed to become acquainted with the path in question, but the following example should at least give you an idea of how to proceed:
FOR %%D IN ("%CD%") DO SET "DirName=%%~nxD"
ECHO %DirName%
This script gets the path from the CD variable and extracts the name only from it to DirName.
You can use basename command:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=$(basename "$FULLPATH")
You can use built-in bash tricks:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=${FULLPATH##*/}
Explanations:
first # means 'remove the pattern from the begining'
second # means 'remove the longer possible pattern'
*/ is the pattern
Using built-in bash avoid to call an external command (i.e. basename) therefore this optimises you script. However the script is less portable.

How do I extend this batch command?

I came across this piece of batch code. It should find the path to every single .exe file if you enter it.
#Set Which=%~$PATH:1
#if "%Which%"=="" ( echo %1 not found in path ) else ( echo %Which% )
For instance, if you save this code in the file which.bat and then go to its directory in DOS, you can write
which notepad.exe
The result will be: C:\WINDOWS\System32\notepad.exe
But it's a bit limited in that it can't find other executables. I've done a bit of batch, but I don't see how I can edit this code so that it can crawl the hard drive and return the exact path.
When you want to find an executable (or other file) anywhere on the drive, not just in PATH, then perhaps only the following will work reliably:
dir /s /b \*%!~x1 | findstr "%1"
But still, it's horribly slow. And it doesn't work with cyclic directory structures. And it probably eats children.
You may be much better off using either Windows Search (dependin on OS) or writing a program from scratch which does exactly what you want (the cyclic dir thing might happen on recent Windows versions pretty easily; afaik they have that already by default).
Here's the same thing written in python:
import os
def which(program,additional_dirs=[]):
path = os.environ["PATH"]
path_components = path.split(":")
path_components.extend(additional_dirs)
for item in path_components:
location = os.path.join(item,program)
if os.path.exists(location):
return location
return None
If called with just an argument, this will only search the path. If called with two arguments ( the second being an array ), other directories will be searched.Here are some snippets :
# this will search notepad.exe in the PATH variable
print which("notepad.exe")
# this will search whatever.exe in PATH. If not found there,
# it will continue searching in the D:\ drive and in the Program Files
print which("whatever.exe",["D:/","C:/Program Files"])