Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 283
zmol Avatar asked Feb 21 '11 05:02

zmol


People also ask

How do I copy a folder and subfolders in CMD?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

How do I copy a directory in script?

PowerShell ScriptCopy-Item -Path[path of folder] -Destination[path of destination folder] -Recurse. The -Recurse parameter copies all the files and subfolders and their contents to the destination recursively. E.g., Copy-Item –Path“E:\Hexnode” -Destination“D\: Backup” -Recurse.

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

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 
like image 156
akf Avatar answered Sep 23 '22 18:09

akf