Xcopy - Trying to copy a folder with certain arguments to another location - batch-file

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.

Related

How can I batch copy certain files from multiple folders into multiple folders?

I am looking to copy files that are like "MAN" that reside inside multiple folders in one directory into all the folders in another directory.
This is what I have now, it copies the files correctly but not into all the folders inside the directory. just into the directory itself.
for /d %%a in ("C:\test123\*") do #copy "%%~Fa\MAN*" /d "c:\Test Destination\*" 2>NUL
to reiterate the problem, the files are copied into c:\Test Destination* but I want it to copy to every folder in that folder. It would be great if the file already exists there, to not copy it at all.
Thanks!
This may work, but is untested. Test it on some sample files.
#echo off
for /r "C:\test123" %%a in (man*) do (
pushd "c:\Test Destination"
for /d /r %%b in (*) do if not exist "%%b\%%~nxa" copy "%%a" "%%b"
popd
)
pause

xcopy wildcard source folder name to destination

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

bat file debug "back up used files"

Here is what I want to do: I want to write a "bat" file that will check all the files in a single partition to determine whether any file is revised/created today and if any, I would copy these file to a folder. So, if I run this bat everyday before I leave my office, I can backup all the files I used in a single folder. The bat file I have now copies the folder instead of file, and sometimes it doesn't work at all... Could you help me debug it? You might want to put it in a root directory such as C/D, and then change d:/test to whatever folder you plan to "test copy the targeted file.
Here is the code I have for now:
#echo off
set t=%date%
set t=%t:~0,10%
echo %t%
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=*" %%i in ('dir /b /a-d') do (
set d=%%~ti
set d=!d:~0,10!
echo !d!
if "!d!"=="%t%" (if not "%~nx0"=="%%i" copy "%%i" d:\test))
for /f "tokens=*" %%j in ('dir /b /ad') do (
set d=%%~tj
set d=!d:~0,10!
echo !d!
if "!d!"=="%t%" (echo d|xcopy /e /y "%%j" d:\test\%%j))
Can you just use robocopy? This line will copy all files in c:\source and its subfolders that have been modified in the last day, to d:\test.
robocopy c:\source d:\test *.* /s /maxage:1
Of course if you forget to run it one day, you'll miss any files touched that day. So if this is really for backups, the better approach is to use the Archive bit.
robocopy c:\source d:\test *.* /s /m
When a file is created or edited, Windows will clear the Archive bit. robocopy with the /m switch will only copy files with the Archive bit set (meaning only ones that have changed since the last time you ran your script), then sets the Archive bit.

delete all folder except one using batch

I have a folder named Scripts in current directory Say C:\QTP\Script\. I am not sure the folders names and count in that scripts folder. Inside each unknown folder name i have Objective Evidences folder. I want a code that deletes all the files and folders in unknown folders but not objective evidences folder.
EG:
Scripts\Folder1\Objective Evidences (this folder1 contains many files too)
Scripts\Folder2\Objective Evidences (This folder2 also contains many files)
I am not sure how many folders are present in scripts folder and wat are their names.
I should get the list of folder names present in Scripts folder and delete all files and folder present in it except Objective Evidences folder.
Please let me know the batch code for the same.
for /f "tokens=*" %%G in ('dir /b /s /a:d "C:\QTP\Script\*"') do (
echo %%~fG | FIND "Objective Evidences" || rd /s /q "%%G"
)
Try this...Or this
pushd "C:\QTP\Script\"
for /D /R %%a in (*) do echo "%%~fa" | find "Objective Evidences" || rd /s /q "%%a"
popd

batch script to find the directory where a file is located, copy all files located in that directory and sub-directories to another directory

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.

Resources