Batch - copy folder to many folders using wildcard - windows

I’m trying to copy a subfolder of one folder into a number of other folders with unknown names. The intention ist to backup the source file of a program in all employees folders who use it. If the programs folder isn’t found in an employees folder, nothing should be done. This looks as follows:
Source:
F:\Users\myFolder\programFolder\Sourcefolder
Target:
F:\Users\anotherOnesFolder\programFolder\Sourcefolder
So my idea was to do the following:
xcopy "F:\Users\myFolder\programFolder\Sourcefolder" "F:\Users\*\programFolder\Sourcefolder" /e /y
But this wildcard doesn’t seem to work. I’ve found a lot about wildcards at the end of the path, but that doesn’t apply here.

for /d %%d in ("F:\Users\*") do (
if /i not "%%~nxd"=="myFolder" if exist "%%~fd\folder\programFolder\Sourcefolder" (
robocopy "F:\Users\myFolder\programFolder\Sourcefolder" "%%~fd\folder\programFolder\Sourcefolder" * /mir
)
)

for /f %a in ('dir /ad /b "F:\Users*"') do (
xcopy F:\Users\myFolder\programFolder\Sourcefolder "%~dpfa\folder\programFolder\Sourcefolder" /e /y
)

Related

Delete Specific Extension files from the C: drive excepts from two folders

I'm looking for a batch script commands that deletes all the files of specific extension (example .exe, .log) from my C: drive except from the two folders (folder_A and folder_B). Any idea how can I achieve this.
I'm using the below code but it's deleting the files from the manual folder also
If exist "D:\" (
for /D %%D in (*.*) do (
if /I not "%%~nxD"=="manual" del *.txt /s "%%~D"
)
pause
Thank you.
This will work for you:
If exist "D:\" (
for /D %%D in (*.*) do (
if /I not "%%~nD"=="manual" del /q /s "%%~D\*.txt"
)
)
pause
Note that you didn't also close the if exist code block.

windows bat file copy issue

The below lines need to copy all the folders from \Reports\Input to
\Deliver\Unidentified\Requests. Input directory contains lot of folders which contains files.
but Below code is not copying.
If I change dir /b /ad *`) then it is copying only the files from the input folder subdirectories files.
How to change this Please help on this. Thanks advance
Struture:
under Report --> Deliver,Input folder is there
under Input folder -- Response 1,2,3... folder which contains files.
CD %AUTOMATEDTESTHTTPDIR%\..\Reports\Input
FOR /F "usebackq tokens=*" %%i IN (`dir /ad *`) DO (
ECHO %%i
CD "%%i"
COPY * ..\..\Deliver\Unidentified\Requests
cd..
)
xcopy /S %AUTOMATEDTESTHTTPDIR%\..\Reports\Input\* %AUTOMATEDTESTHTTPDIR%\..\Deliver\Unidentified\Request\
should work. The /S parameter is for recursively copying the files.
You might not need to use a for-loop at all.

xcopy batch folder tree

S:
cd \newclients
xcopy "s:\clients\*\MER" . /s /d
pause
This is my batch file. In my folder tree we have clients then names folders then MER folder. I want to be able to search the directory for the MER folder and copy that folder along with the client name before it. Is there a way to do this with batch files?
Using if exist should do the thing.
pushd "s:\newclients"
for /f "delims=" %%f in ('dir /b /ad "s:\clients\*"') do (
if exist "s:\clients\%%f\MER\nul" ( xcopy "s:\clients\%%f\MER" "s:\newclients" /s /d )
)
popd
pause
Note: Add a slash if it's to check folder ie. if exist "s:\clients\%%f\MER\" ... or if exist "s:\clients\%%f\MER\nul" ...
To suppress prompt about destination file/folder add /I in xcopy :
/I If in doubt always assume the destination is a folder
e.g. when the destination does not exist.
https://stackoverflow.com/a/33445866/

how to traverse specified subfolders in a windows batch file?

Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.
Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.

Move Wildcard Folder structure files to a destination folder

I want to move all the folders starting with "Temp_*****" to a different folder. It does not seem like we can use wild card with Folders. I was looking online, and someone posted this piece of code but I'm not sure how to apply it to my scenario.
#echo off for /d %%a in ({*}) do xcopy "%%a" "C:\Home\a\b\tmp\%%a\" /E
Here's one way to do it, replace C:\TEST01\ with your source folder location:
for /F %%a in ('dir C:\TEST01\TEMP_* /ad /b') do move C:\TEST01\%%a C:\Home\a\b\tmp\%%a

Resources