I want to copy from a wildcard source folder to a destination folder:
xcopy a:\parentfolder\n* x:\parentfolder
Only folders starting with "n" should therefore be copied to the destination.
Any help to get this working would be much appreciated.
for /f "delims=" %%a in ('dir /b/ad "a:\parentfolder\n*" ') do xcopy "a:\parentfolder\%%a\*" x:\parentfolder\
As you have it, XCOPY assumes that n* is a filespec, and there's no way to tell it otherwise.
If you first CD to the folder you want to copy it will work:
a:
cd \parentfolder
xcopy /s n*.* x:\parentfolder
Related
I need to copy a folder matching a wildcard, for instance FOLDER_*. That folder will be in the presence of other files, so I need the command to segregate it from everything else. Also, the command needs to recursively search through the directory, and return only the FOLDER that matches the wildcard, with its contents intact. Then it needs to copy it to another folder. Any ideas? I've tried quite a few variants - here is the last thing I tried.
for /D /R %%f in (FOLDER_*) do xcopy %%f %~dp0\TestResults
Next code snippet should copy all found FOLDER_* folders including their subfolders but omits all their parent folder(s) like upfolder\ in upfolder\FOLDER_* (however could be improved to include it, of course).
for /F loop against dir /b /s /ad (a static list of subfolders) is used instead of for /D /R as the option /d /r is undocumented.
Pay your attention to recursion like FOLDER_main\FOLDER_sub1\FOLDER_sub11 etc.
Operational mkdir and xcopy commands are merely echoed for debugging purposes only:
#echo OFF
for /F "delims=" %%f in ('dir /B /S /AD FOLDER_*') do (
echo mkdir "%~dp0\TestResults\%%~nxf" 2>NUL
echo xcopy /S /E /C "%%~ff" "%~dp0\TestResults\%%~nxf\"
)
Consider adding more switches to xcopy, for instance
/H Copies hidden and system files also.
/R Overwrites read-only files.
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.
I have 100+ sub-directories all under the same folder that I'm looking to copy the newest file to backup location with the directory structure intact.
\data\sub1\newest.file -> \backup\sub1\newest.file
\data\sub1\older.file1.ignore
\data\sub1\older.file2.ignore
\data\sub2\newest.file -> \backup\sub2\newest.file
\data\sub2\older.file1.ignore
etc....
Here's what I have so far, and i can't seem to piece it together. Any help would be greatly appreciated.
#echo off
set source="c:\data"
set dest="n:\backup"
if not exist %dest% md %dest%
cd /d %source%
for /d %%x in ("%source%"/*.*) do (
if not exist "%dest%\%%x" md "%dest%\%%x"
FOR /F %%I IN ('DIR *.* /A-D /B /O-D') DO COPY %%I "%DEST%\%%X" & #ECHO %%I COPIED TO "%DEST%\%%X"
)
I would try to do this with robocopy if I were you, because this will most likely be a more robust solution.
Windows' built-in xcopy program provides a flag to include empty directories.
C:\>xcopy "%source%" "%dest%" /E
But, it sounds like you may want to only copy newer/missing files. If that is the case, then #Marged has it right. You should use robocopy.
C:\>robocopy "%source%" "%dest%" /E
Check out robocopy /? for all the details and additional commands.
The metavariable %%x in your for statement is CaSe-SeNsItIvE so you must use %%x throughout the loop, but you are using %%X in the copy statement.
Since you only want to copy the first file, you should append &goto alabelotsideoftheforloop which terminates the for..x.. after the first file has been copied.
I need to copy a list of files in a text file to a new directory, while preserving the directory structure. My file looks like this:
F326819.B88
F326819.B89
F326819.B90
F326731.B44
F326733.B61
F326733.B62
I need a batch command that will "pick" the ones listed in the text file and copy them over to a new directory, preserving the directory structure. I tried this code but it says invalid number of parameters:
for /f "delims=" %%i in (W:\GasImages\ServiceCards\WindLake.txt) do echo D|xcopy %%i "W:\GasImages\ServiceCards" "D:\Marc\WindLake" /i /z /y /e
Any help would be appreciated.
xcopy takes in a list of source files, followed by a destination directory. When there are multiple directories passed in, it doesn't know what to do with them all.
Try this (note that this code assumes the files to copy are in C:\GasImages\ServiceCards)
#echo off
for /f "delims=" %%I in (C:\GasImages\ServiceCards\WindLake.txt) do (
xcopy "C:\GasImages\ServiceCards\%%I" "D:\Marc\Windlake\" /I /Z /Y /E
)
pause
Also, the echo D| is unnecessary with the /I flag.
I'm trying to xcopy certain folders from a directory of 5000 folders, that start with the number 9097*
Approx 2000 folders start with 9097.
for /f %%A in ('DIR /b /a:D') do (
set temp=%%A
set ID=!temp:~0,4!
if !ID!==9097 xcopy *!ID!* F:\%INPUT% /S /i
My problem is that when it finds the first folder starting with 9097, the code then goes and copies all contents of all the folders, I want it to only copy the 9097 folders. I understand why the code above is doing what it is doing but I can't seem to think of the way for it to just copy the folders and contents starting with 9097.
Any advice would be greatly appreciated.
for /d %%a in (9097*) do xcopy "%%~fa" "f:\%input%\%%~nxa" /s /i
For each folder with a name starting with 9097, xcopy from the full path of the source folder to the target inside a folder with the name and extension of the source folder.
So if I have C:\drivers\* with * indicating many sub-folders, I want to find out where my inf files are located, and then copy ALL files that are located in the same directory where my inf files are located and all sub-directories.
It has been easy to create a script that will copy all .inf files found to my directory:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y %%a C:\test
But copying the other files that are located in the same directory and all sub-directories has been difficult.
Such as, if the inf file is located under C:\drivers\sbdrv\hseries\usb30\amdhub\w7 and the sys file is located in the sub-folder of x86, I need the sys file to be kept in the same sub-folder but under the destination of C:\test\x86.
Any ideas?
EDIT: Maybe this will make it easier. As soon as it finds one .inf file in a folder, it should copy the entire folder over to test as well as all sub-folders and move on to the next one. So if it sees the first .inf file located C:\drivers\sb3045\sb407.inf it should copy all files and folders under sb3045 without copying the folder sb3045 itself, and then move on to folder C:\drivers\sb4055\drivers\oem\intel\id6077.inf and copy all files and folders under the intel folder without copying the intel folder itself.
EDIT2:
It looks like this will work, but it is slow as it finds every .inf and is copying over any old files if there is more than one .inf file per folder
#ECHO ON
SETLOCAL ENABLEDELAYEDEXPANSION
CD\
CD drivers
FOR /f "tokens=* delims=" %%B IN ('DIR /b /s /o:gen .inf') DO (
XCOPY "%%~dpB.*" "C:\test\" /e /c /h /y
If anyone has a cleaner or quicker idea, let me know please. Until then, I'll have to work with this one.
try this:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y "%%~dpa*" C:\test
for /R C:\WINDOWS\inf %%a in (*.inf*) do (
xcopy /qv /T %%a .\test\
xcopy /qv %%a .\test\
)
pause
this code worked for me.