I want to copy folder IMAGE to all other folders in a folder.
The folder structure is:
Test folder
FirstSubFolder
IMAGE
OneMoreSubFolder
Test Folder 1
TestFolder2
The result should be:
Test folder
FirstSubFolder
IMAGE
IMAGE
OneMoreSubFolder
IMAGE
Test Folder 1
IMAGE
TestFolder2
IMAGE
Which commands do I need in a batch script to do this folder copying task?
With Test folder being the current directory use following batch code:
#echo off
for /D %%D in (*) do (
if /I not "%%D" == "IMAGE" (
xcopy "IMAGE\*" "%%D\IMAGE\" /C /E /H /I /K /Q /R /Y >nul
)
)
Or if just the files and subfolders in IMAGE should be copied to all other subfolders of Test folder, use this code:
#echo off
for /D %%D in (*) do (
if /I not "%%D" == "IMAGE" (
xcopy "IMAGE\*" "%%D\" /C /E /H /I /K /Q /R /Y >nul
)
)
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
if /?
xcopy /?
Related
I just try move all directories from directory "D:\Download" to "D:\DownloadArchive"
I try this but doesnt work
move /y "D:\Download\*.*" "D:\DownloadArchive"
Use "FOR /D" to select only directories. (Disclaimer: untested)
FOR /d %1 IN ("D:\Download\*") DO move /y "%1" "D:\DownloadArchive"
A very simple solution is to use a FOR statement to move each file in the directory. A better alternative to move is xcopy as it allows to over-wright existing files.
For the switches I'm using type xcopy /? in an CMD Window.
From Batch:
FOR /d %%i IN ("D:\Download\*") DO xcopy "%%i" "D:\DownloadArchive" /E /I /Y /S
From CMD:
FOR /d %i IN ("D:\Download\*") DO xcopy "%i" "D:\DownloadArchive" /E /I /Y /S
del /s /q "C\:test\*.*"
for /d %%p in (C:\test\*.*) do rmdir "%%p" /s /q
This is a code I have which deletes files and sub folders in a folder. And it works, but I do not get what each of the command works. So my question is, what does the second line mean? Like, what is the %%p part, and what is the rmdir "%%p" part?
This is a batch file.
for /d is to iterate directories.%%p is a special kind of variable used in for loops - in this case it will change its value to each directory name in c:\tests . The part after do means execute rmdir (deletes directory) for each value of %%p. More info here
When in doubt, you should see the help of each command by typing YourCommand /? :
Del /?
RD /? or rmdir /?
For /?
I added an echo to see what happen when you execute your batch file :
#echo off
Echo This command to delete all files located on your "C\:test\" folder
Echo del /s /q "C\:test\*.*"
pause
echo this command is for looping thru your directory "C\:test\" to remove any subfolders on there
for /d %%p in (C:\test\*.*) do echo rmdir "%%p" /s /q
pause
What do I add to my DEL batch file if I want to exclude a folder inside a folder I want to delete?
I have this code to delete my all the contents of the temp folder
DEL /F /Q C:\temp
Now, I want to exclude a folder named imptfolder inside. This shouldn't be deleted whether it exists or not inside the temp folder. How do I do this?
Here's some batch script code that'll do what you're asking.
for /d %%I in (c:\temp\*) do (
if /i not "%%~nxI" equ "imptfolder" rmdir /q /s "%%~I"
)
del /q c:\temp\*
If you're just entering these commands from the console, use single percents rather than double.
cd /d c:\temp
for /d %I in (*) do #(if /i not "%~I" equ "imptfolder" rmdir /q /s "%~I")
del /q *
I have a folder with some content which I want to copy into 260 folders, which all are in the same folder.
I guess this is doable with a batch file and a for-loop, but I can't seem to understand how to do it.
you can use XCopy for copy the file
xcopy "src\*.*" "..\..\..\dst" /s /i /c /y
and can add the loop FOR loop
for /l %%a in (1,1,260) do (
call :Method1
)
:Method1
xcopy "src\*.*" "..\..\..\dst" /s /i /c /y
GOTO :EOF
Update
FOR /D %%a in ("dst\*") do
xcopy "src\*.*" "%%a" /s /i /c /y
I have checked some examples on internet but I can't get my (first) batch file to work. I would like to copy automatically my file from a folder to another one but nothing happen.
#echo off
xcopy "C:\source\" "C:\target\" /c /d /i /y
exit
Could you see anything wrong?
Thanks!!
Update: I have done the command given by Bali C but it still doesn't work. See snapshot
xcopy C:\folder1 C:\folder2\folder1 /t /e /i /y
xcopy C:\folder1 C:\folder2\ /t /e /i /y
Image:
I have to stop it with CTRL + C.
PS: I'm on Win 7
Update (Solution):
It works! The problem was the name xcopy,bat on my Desktop, and I was running the command from there, so it was executing the xcopy.bat file of my desktop instead of the Windows one.. I had to rename the file with "myxcopy.bat" :
#echo off
xcopy "C:\source" "C:\target" /c /d /i /y
exit
After testing most of the switches this worked for me:
xcopy C:\folder1 C:\folder2\folder1 /t /e /i /y
This will copy the folder folder1 into the folder folder2. So the directory tree would look like:
C:
Folder1
Folder2
Folder1
Based on xcopy help, I tried and found that following works perfectly for me (tried on Win 7)
xcopy C:\folder1 C:\folder2\folder1 /E /C /I /Q /G /H /R /K /Y /Z /J
If the requirement is to copy all files in "\Publish\Appfolder" into the parent "\Publish\" folder (inclusive of any subfolders, following works for me)
The switch '/s' allows copying of all subfolders, recursively.
xcopy src\main\Publish\Appfolder\*.* /s src\main\Publish\
You must specify your file in the copy:
xcopy C:\source\myfile.txt C:\target
Or if you want to copy all txt files for example
xcopy C:\source\*.txt C:\target