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/
Related
so this has been puzzling me
I need to help to make this ('Dir /D /S A:D')
work with ren "New Folder" New Files"
I have tried different ways, but I just can't get the folders renamed
First attempt
#ECHO OFF
FOR /D %%d IN ('Dir /D /S A:D') DO (
REN "New Folder" "New Files"
)
Second attempt
#echo off
FOR /D %%D IN ('Dir /D /S A:D') DO RENAME "%%D\New Folder" New Files
Third attempt
#echo off
FOR /D %%D IN ('Dir /D /S A:D') DO CALL :RENAME %%D
:RENAME
SET CRITERIA=\New Folder
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "New Files"
what I want to do is rename folders. I have many Folders and Subfolders
and every main Directory folder has different names so the script needs to read past every folder and find New Folder and rename it to New Files
layout
Main Folder
---- Folder 1
------| New Folder
---- Folder 2
------| Folder 22
----------| New Folder
New folder is found in many different locations so the script needs to be able to read main folders, sub folders, and so on
any help with this will great
Updated Question
#echo off
for /f "tokens=*" %%f in ('dir /s /b a:d "New Folder"') do (
set "filename=%%~nf"
ren "%%f" "New Files"
)
Before
Main Folder...
Rename folder.bat
....| New Folder
......| New Folder
........| New Folder
Bad RESULTS
Main Folder...
Rename folder.bat
....| New Folder
......| New Files
........| New Folder
What it should do
Main Folder...
Rename folder.bat
....| New Folder
......| New Files
........| New Files
Need help with this
I even tried this
#echo off
for /f %%f in ('dir /s /b /a:d "New Folder"') do ren "%%f" "New Files"
Firstly, you seem to confuse for /D with for /F, and it is the latter you need to use here:
for /F "tokens=*" %%I in ('dir /S /B /A:D-H-S "New Folder"') do (
…
)
This iterates through the whole directory tree rooted at the current working directory (which is not necessarily the container of the batch script!). The tokens=* option of for /F ensures that white-spaces in the paths are maintained (removing leading ones, but such are anyway not possible for paths). The switch /A:D-H-S of dir ensures that files, as well as hidden and system directories, are excluded.
But there is one more problem: dir /S returns the items in a per-branch basis from top to the bottom of the hierarchy, and for /F, together with dir, lets the whole directory tree be built before loop iteration begins. So when now renaming a certain directory in the loop body, any children of it get new paths, although the old ones are buffered, letting the renaming fail. For instance, when directory D:\Path\To\Root\New Folder becomes renamed to D:\Path\To\Root\New Files, a sub-directory D:\Path\To\Root\New Folder\New Folder does no longer exist, since its path is now D:\Path\To\Root\New Files\New Folder, hence it cannot be found.
To overcome this issue, simply reverse the whole directory tree to be iterated by sort:
cd /D "D:\Path\To\Root" & rem // (use `%~dp0.` to specify a path relative to the container of this script)
for /F "tokens=*" %f in ('dir /S /B /A:D-H-S "New Folder" ^| sort /R') do (
ren "%%f" "New Files"
)
That way, the sub-directory D:\Path\To\Root\New Folder\New Folder is renamed first, before its parent D:\Path\To\Root\New Folder becomes processed.
I need to batch copy two folders, let's call them A and B, from F:\Sourcefolder\ to F:\destinationfolder subfolders (not to destination folder itself).
Now I know when batch copying file (file.exe for example) it is supposed to look something like this
for /r "F:\destinationfolder" %%i in (.) do #copy "F:\Sourcefolder\file.exe" "%i"
In each of those subfolders there is a lot of files. After copying A and B folders to all subfolders, I would like to move all files within the subfolders to folder A within their folder. Is this possible to do?
the XCOPY command is designed for folder copy, FOR /D will list level1 folders:
for /d %%a in ("F:\destinationfolder\*") do (
XCOPY "F:\Sourcefolder\A\*" "%%~fa" /s /i
XCOPY "F:\Sourcefolder\B\*" "%%~fa" /s /i
)
for recursive copy (all subfolders):
for /r /d "F:\destinationfolder\" %%a in (*) do (
XCOPY "F:\Sourcefolder\A\*" "%%~fa" /s /i
XCOPY "F:\Sourcefolder\B\*" "%%~fa" /s /i
)
FOR /R will not work properly if there's no wildcard in the brackets - ? or *
ROBOCOPY,XCOPY
I am using the below but it is only moving the files and not the folders in the source folder.Is there anything I can add?
move "C:\source\*" "C:\destination\"
This is another way: test it on sample folders first.
robocopy "C:\source" "C:\destination" /move /s
EDIT: Robocopy copies the files and then deletes the original, so will take a long time for large files, even if the source and target locations are on the same hard drive.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=c:\source"
set "target=c:\destination"
(if not exist "%target%\" md "%target%" ) && (
pushd "%source%" && (
for /f "delims=" %%a in ('dir /a /b *') do move "%%a" "%target%\"
popd
)
)
Ensure that target folder exist, then, if source folder is accesible, change active directory to the source folder, and for each element inside it, execute a move operation to the target folder
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
)
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.