batch/bat to copy folder and content at once
I'm writing a batch script that does a copy. I want to script it to copy an entire folder. When I want to copy a single file, I do this
copy %~dp0file.txt file.txt
If I have a folder with this structure, is there a command to copy this entire folder with its contents all at once while preserving the exact structure.
mainfolder/
file1.txt
file2.txt
insidefolder/
file3.txt
file4.txt
file5.txt
Solution 1:
if you have xcopy
, you can use the /E
param, which will copy directories and subdirectories and the files within them, including maintaining the directory structure for empty directories
xcopy [source] [destination] /E
Solution 2:
The old way:
xcopy [source] [destination] /E
xcopy is deprecated. Robocopy replaces Xcopy. It comes with Windows 8, 8.1 and 10.
robocopy [source] [destination] /E
robocopy has several advantages:
- copy paths exceeding 259 characters
- multithreaded copying
More details here.
Solution 3:
I suspect that the xcopy
command is the magic bullet you're looking for.
It can copy files, directories, and even entire drives while preserving the original directory hierarchy. There are also a handful of additional options available, compared to the basic copy
command.
Check out the documentation here.
If your batch file only needs to run on Windows Vista or later, you can use robocopy
instead, which is an even more powerful tool than xcopy
, and is now built into the operating system. It's documentation is available here.
Solution 4:
For Folder Copy You can Use
robocopy C:\Source D:\Destination /E
For File Copy
copy D:\Sourcefile.txt D:\backup\Destinationfile.txt /Y
Delete file in some folder last modify date more than some day
forfiles -p "D:\FolderPath" -s -m *.[Filetype eg-->.txt] -d -[Numberof dates] -c "cmd /c del @PATH"
And you can Shedule task in windows perform this task automatically in specific time.