batch/bat to copy folder and content at once - windows

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

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

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.

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.

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.

I've been interested in the original
question here and related ones.
For an answer, this week I did some
experiments with XCOPY.
To help answer the original question, here
I post the results of my experiments.
I did the experiments on Windows 7 64 bit
Professional SP1 with the copy of XCOPY
that came with the operating system.
For the experiments, I wrote some code in
the scripting language Open Object Rexx
and the editor macro language Kexx with
the text editor KEdit.
XCOPY was called from the Rexx code. The
Kexx code edited the screen output of
XCOPY to focus on the crucial results.
The experiments all had to do with using
XCOPY to copy one directory with several
files and subdirectories.
The experiments consisted of 10 cases.
Each case adjusted the arguments to XCOPY
and called XCOPY once. All 10 cases were
attempting to do the same copying
operation.
Here are the main results:
(1) Of the 10 cases, only three did
copying. The other 7 cases right away,
just from processing the arguments to
XCOPY, gave error messages, e.g.,
Invalid path
Access denied
with no files copied.
Of the three cases that did copying, they
all did the same copying, that is, gave
the same results.
(2) If want to copy a directory X and all
the files and directories in directory X,
in the hierarchical file system tree
rooted at directory X, then apparently
XCOPY -- and this appears to be much of
the original question -- just will NOT do
that.
One consequence is that if using XCOPY to
copy directory X and its contents, then
CAN copy the contents but CANNOT copy the
directory X itself; thus, lose the
time-date stamp on directory X, its
archive bit, data on ownership,
attributes, etc.
Of course if directory X is a subdirectory
of directory Y, an XCOPY of Y will copy
all of the contents of directory Y WITH
directory X. So in this way can get a copy
of directory X. However, the copy of
directory X will have its time-date stamp
of the time of the run of XCOPY and NOT
the time-date stamp of the original
directory X.
This change in time-date stamps can be
awkward for a copy of a directory with a
lot of downloaded Web pages: The HTML
file of the Web page will have its
original time-date stamp, but the
corresponding subdirectory for files used
by the HTML file will have the time-date
stamp of the run of XCOPY. So, when
sorting the copy on time date stamps, all
the subdirectories, the HTML files and the
corresponding subdirectories, e.g.,
x.htm
x_files
can appear far apart in the sort on
time-date.
Hierarchical file systems go way back,
IIRC to Multics at MIT in 1969, and since
then lots of people have recognized the
two cases, given a directory X, (i) copy
directory X and all its contents and (ii)
copy all the contents of X but not
directory X itself. Well, if only from
the experiments, XCOPY does only (ii).
So, the results of the 10 cases are below.
For each case, in the results the first
three lines have the first three arguments
to XCOPY. So, the first line has the tree
name of the directory to be copied, the
'source'; the second line has the tree
name of the directory to get the copies,
the 'destination', and the third line has
the options for XCOPY. The remaining 1-2
lines have the results of the run of
XCOPY.
One big point about the options is that
options /X and /O result in result
Access denied
To see this, compare case 8 with the other
cases that were the same, did not have /X
and /O, but did copy.
These experiments have me better
understand XCOPY and contribute an answer
to the original question.
======= case 1 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_1\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 2 ==================
"k:\software\dir_time-date\*"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_2\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 3 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_3\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 4 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_4\"
options = /E /F /G /H /K /R /V /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 5 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_5\"
options = /E /F /G /H /K /O /R /S /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 6 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_6\"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 7 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_7"
options = /E /F /G /H /I /K /R /S /Y
Result: 20 File(s) copied
======= case 8 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_8"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 9 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_9"
options = /I /S
Result: 20 File(s) copied
======= case 10 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_10"
options = /E /I /S
Result: 20 File(s) copied

Related

Batch job to delete folder older than 7 days on windows 7

Hi I need to delete folders on my Windows 7 computer which are older than 7 days and have data in it.
I have used the following commands which haven't worked out well for me
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del #path"
The second command which I referred from Batch file to delete files older than N days
also didn't work for me.
Can someone please suggest what to use in the second command on C:\what\ever or suggest an alternative command to delete my folders older than 7 days.
So your task is to delete older backup directories no longer needed.
The tricky solution would be working with "delete older than X days".
The simple solution is following:
#echo off
set "BackupDirectory=C:\Backup"
for /F "skip=7 delims=" %%D in ('dir "%BackupDirectory%" /AD /B /O-D 2^>nul') do (
rd /Q /S "%BackupDirectory%\%%D"
)
The command DIR returns a list which contains
because of /AD (attribute directory) just the subdirectories and
because of /B (bare format) just directory names and
because of /O-D ordered by date with newest at top and oldest last.
The command FOR skips the first 7 lines from list, i.e. the 7 newest subdirectories, and executes on the other (older) directories the command to remove the subdirectory.
In your case with directory names starting with yyyy-mm-dd it would be also possible to use /O-N (ordered reverse by name) instead of /O-D to keep skip=x newest subdirectories and delete all others.
Note: On NTFS partitions the last modification date of a folder changes if any file/folder in this folder is added/modified/deleted, but not on FAT16, FAT32 or exFAT partitions.
On DIR command line after /O-D the option /TC can be added get output the directory list sorted reverse by creation date of the folders. But backup folders are usually not modified later after backup was created. It is a matter of opinion to keep the newest folders taking modifications in the folders into account or rate only folder creation time.
In my experience on deleting backups the date is not so important. Important is only limiting the number of backups to avoid filling a storage media. For example if a backup usually needs 5 GiB, the number of backups to keep might be 10 or 20, but if a backup usually needs only 500 KiB, the number could be increased to 100. The date does not matter, just the total amount of bytes required for the backups, as the limit is the storage media size and not the time span.
And for a log file with lines always appended on each execution of a backup operation, the size of the log file must be usually observed and not the time span on which lines are appended to same log file to avoid that the log file becomes larger and larger. Moving a log file with size > x KiB or MiB to *_old.log with /Y to overwrite an already existing *_old.log and then redirect the new lines into a new log file is quite often the right strategy to have just 2 logs files (*.log and *_old.log) with a defined maximum file size containing log lines of the last x backup operations.
Remove old backup folders older than /d days. So use a minus value.
Oneliner...
forfiles /p y:\foldername /d -50 /c "cmd /c if #isdir==TRUE rmdir #file /s /q"
You want to leave off the forfiles "/s" if you only want to scan the top level folders and once you have that folder, you rmdir the entire folder.
The reason for using rmdir is because if you use del or erase, once it deletes a file, it can change the date on the holding folder. Then the command leaves folders behind.
/p - for path
/d - minus for 50 days old
/c - run a command
#isdir - if folder
Rmdir - better than del or erase
#file - what it was looking for
/s - subfolders
/q - quiet

xcopy returns error "Invalid number of parameters" when exclude parameter is set

Issuing:
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y
works as expected. However:
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:"Y:\...\exclude.txt"
returns error:
Invalid number of parameters
Which also occurs when path names (containing spaces) are not enclosed by quotation marks. This however, is not the case. Paths (edited for readability) all correspond correctly. Syntax (as per Product Documentation - Xcopy) is also correct. Concerning OS is Windows XP Professional x32 SP3.
Why is second cmd returning error and how is it to be solved? I am not looking for alternatives to xcopy (robocopy etc.).
XCOPY is an old command harking back to the days of DOS. It looks like the /EXCLUDE option was never updated to support long file names. Ugh :-(
If you remove the quotes, then the text after the space is interpreted as an additional parameter, and you get the "Invalid number of parameters" error. If you keep the quotes, then it treats the quotes as part of the path, and reports it cannot find the file.
I believe you have three possible solutions:
1) Use the short 8.3 folder names in your path.
Of course this cannot work if your volume has short names disabled.
2) Use the SUBST command to create a drive alias for your troublesome path.
subst Q: "Y:\path with spaces"
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:Q:exclude.txt
subst Q: /d
This could be a problem if you don't know a drive letter that is free.
3) (my favorite) Simply PUSHD do the troublesome path and run the command from there :-)
pushd "Y:\path with spaces"
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:exclude.txt
popd
See https://sevenx7x.wordpress.com/2009/01/02/xcopy-with-exclude-option-shows-cant-read-file/ and http://forums.majorgeeks.com/showthread.php?t=54300 for more information.
/EXCLUDE:file switch will not exclude the file specified. As per xcopy command reference:
/exclude:FileName1[+[FileName2][+[FileName3](…)] Specifies a list of
files. At least one file must be specified.
Each file will contain search strings with each string on a separate line in the file. When any of the strings match any part of the
absolute path of the file to be copied, that file will be excluded
from being copied.
It took me some time to get this right as well (I had the same errors), but ultimately, this format worked for me. As with all things DOS, absolute precision is critical, so feel free to copy and paste the below.
xcopy /t /e "C:\Users\username\Your Folder" "C:\Users\user\Your Folder"

Xcopy complete operation successfully, but the copy does not occur

i'm having a weird problem about copying with Xcopy.
I'm using Windows Server 2008 R2. There is a Batch file (.CMD) - in fact, 2 Batches - which executes many operations. Among them there are 2 Xcopy.
The first Xcopy completes the copy successfully. Then some operations of batch are executed and...the second Xcopy is executed after some time. It shows a successfully status, like "300 files copied" but...the files are not copied !
THE strange behaviour: i put a pause command into the Batch and, after this second weird Xcopy, i run THE SAME command at the Prompt and..it works !!!
I cannot explain this, so i'm asking for help here. The full command is:
xcopy /s /e /h /r /i /y E: D:
As a matter of curiosity/information, the other Xcopy (which worked!) is:
xcopy /s /e /h /r /i /y E: D:
Or....the same command !!! Each one runs in a separated Batch file - the first Batch "calls" the second one.
E: is CD-ROM, so after the first Xcopy, the first Batch asks for another Disc and calls the second Batch, which (supposed) copies entire CD content to D: (HDD).
I'm sorry if the question is silly and i'm not realizing how simple is to correct the erratic behaviour of Xcopy. Thanks in advance...
Thanks very much Bali C !
But i found the problem: the correct syntax is
xcopy /s /e /h /r /i /y E:\ D:\
You can see that the backslashes made all the difference ! The files were being copied to another directory, in fact, the current working directory. With backslashes the copy is fine.
But i appreciate your suggestion...
To copy the entire contents try using
xcopy /e /h /r /i /y /t E:\*.* D:\
I left out the /s as it contradicts the /e switch, I doubt it will be the source of the problem but it's worth a shot. I used the \t switch to copy the directory structure of the source.
I have also used wildcards *.* to copy the contents, rather than just the drive letter, some things work at cmd prompt but not in batch, but try using this.
Another option would be to use robocopy.

Copy a file to all folders batch file?

I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
Give this a try:
for /r "D:\Software\destinationfolder" %i in (.) do #copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"
Of course, if it's to go into a batch file, double the '%'.
This was the first search I found on google for batch file copy file to all subfolders.
Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on xcopy.
This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.
cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
the for loop - See help here. Or type for /? in a command prompt.
/r - Loop through files (recurse subfolders)
/d - Loop through several folders
%%I - %%parameter: A replaceable parameter
xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
C:\temp\file.ext - The file you want to copy
"%%~fsI" - Expands %%I to a full pathname with short names only
/H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
/K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.
for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"
#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir
I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).

How can I move the contents of one directory tree into another?

I have a directory which contains files and a number of levels of subdirectories:
C:\Source
I would like to move the contents of C:\Source into:
C:\Destination
Requirements:
All files and all subdirectories
within C:\SourceData must be moved
I will be running the command in a
batch file
I can't use Powershell or
any other scripting languages
Attempt 0
XCOPY /E "C:\Source" "C:\Destination"
This works perfectly, but it copies instead of moves. I can't copy then delete the source as I'm moving a very large set of files and there isn't enough disk space to have two copies of them at a time.
Attempt 1
MOVE "C:\Source" "C:\Destination"
This moves the entire C:\Source directory into C:\Destination so I end up with:
C:\Destination\Source
Attempt 2
With some help from this question and accepted answer I came up with:
for /r "C:\Source" %%x in (*) do move "%%x" "C:\Destination"
This moves the files within C:\Source but not the subdirectories or their contents. Note that I used %%x instead of %x as I'm using it in a batch file.
Using FOR seems promising but I'm not sure I've used the right syntax? Have I missed something?
Attempt 3
As suggested by Nick D, I tried rename:
RENAME "C:\Source" Destination
For the example scenario I gave this works fine. Unfortunately my real Destination directory is at a different level to the Source directory and this doesn't seem to be supported:
C:\>REN /?
Renames a file or files.
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.
I get "The syntax of the command is incorrect." errors if I try to specify a more complex destination path, for example:
RENAME "C:\Source" "C:\MyOtherDirectory\Destination"
RENAME "C:\Source" "MyOtherDirectory\Destination"
Undoubtedly use robocopy. It is a simple but brilliantly useful tool.
robocopy /move /e sourcedir destdir
This will move all the files and folders, including empty ones, deleting each original file after it has moved it.
If you don't have robocopy installed you can download it by itself or as part of a Microsoft resource kit.
Update:
Adjusted code to a. check whether folders already exist at the destination, in which case move files in that folder over (and continue traversing the source directory structure), otherwise move the folder wholesale.
At the end of the script the source folder is removed altogether to eliminate these folders which have had their files moved over to an already existent folder at the destination (meaning these folders have been emptied but not deleted at the source).
Additionally we check whether a folder is both empty and already exists at the destination in which case we do nothing (and leave the source folder to be deleted to the last line of the script). Not doing this results in "The filename, directory name, or volume label syntax is incorrect." errors.
Phew! Please let me know how you get on with this! I have tested this and it seems to be working well.
for /d /r "c:\source" %%i in (*) do if exist "c:\destination\%%~ni" (dir "%%i" | find "0 File(s)" > NUL & if errorlevel 1 move /y "%%i\*.*" "c:\destination\%%~ni") else (move /y "%%i" "c:\destination")
move /y c:\source\*.* c:\destination
rd /s /q c:\source
In response to ufukgun's answer:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
/s - Copies directories and subdirectories except empty ones.
/e - Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/i - If destination does not exist and copying more than one file, assumes that destination must be a directory.
/y - Suppresses prompting to confirm you want to overwrite an existing destination file.
As stated in another answer, using xcopy may be not as efficient as it would be a native move command that only changes pointers in the filesystem.
Since XCOPY works, you could use XCOPY and DELETE, it's a workaround, but should work?
on Vista use
robocopy source destination /MIR
/MIR .. mirror a complete directory tree (also deletes files in the destination)
else
xcopy
Of course you have to delete the source afterwards :)
I have a directory which contains
files and a number of levels of
subdirectories:
C:\Source
I would like to move the contents of
C:\Source into:
C:\Destination
Maybe I'm missing something, but can't you just rename the folder?
As sent on twitter:
Try combining attempt 3 with attempt 1. Rename to get the destination folder correct, then move "Destination" to the correct place.
#echo on
set SOURCE=C:\Source
set DESTINATION=C:\Destination
xcopy %SOURCE%\* %DESTINATION%\* /s /e /i /Y
PAUSE
i use batch file like this...
or simply call:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
I use a utility called xxcopy. It can move files and have many useful options, you can use it like this :
xxcopy C:\Source C:\Destination /E /RC
the options :
/E to copy everything even empty folders
/RC to remove every source file after each successful copy
you can download free copy of xxcopy for personal use from :
http://www.xxcopy.com/xcpydnld.htm
Maybe a little off-topic, but still usefull:
Some people suggest to just copy + delete the source files, but
moving files is not the same as copying + deleting!
When using the (x)copy function, you allocate new space on the same volume on a harddisk. After the copying the files, the old files (the old allocated space which was required for the files) are beign marked as deleted. While you will achieve the same result as an end-user, moving does something diffrent and more efficient.
Moving files actually only changes some records in the MFT (master file table). These changes only consist of a different path for the user to locate its files. Physically the files still remain in the same sectors on the harddisk.
If all your trying to do is move a directory and the content up one level:
MOVE folder_you_wan_to_move ..
Note that .. refers to the next directory up.
I leave this code I have written based on ljs's attempt,
It moves directory trees as Windows does, overwriting the files that already exists in destination with those in source, and avoiding the slow copy and erase method except if the destination is in a different drive.
If your S.O is not in english must change line 6 with the text used by the Dir command when it finds 0 files.
movedir.bat source_dir destination_dir
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
if %1.==. echo movedir dir1[\*] dir2 & echo move dir1 inside dir2 overwriting coincident files & echo option \* moves only dir1 content & goto end
set S=%~f1
set D=%~f2
set "noFiles= 0 File"
set "R=0" rem R is option [\*] flag
if %S:~-2%.==\.. set "S=%S:~,-2%" & set "R=1"
if not exist "%S%" goto paramERR
if not exist "%D%" goto paramERR
set "Trim=0" & set "Sc=%S%"
:LP
set /A Trim+=1 & (set "Sc=!Sc:~1!") & if NOT !Sc!.==. goto LP
if %R%==0 (if exist "%D%\%~n1%~x1" (set "D=%D%\%~n1%~x1")) else move /y "%S%" "%D%" & goto end
CALL:movefiles "%S%" "%D%"
for /D /R "%S%" %%I in (*) do (set "Way=%%~fI") & (set "Way=!Way:~%Trim%!") & if exist "%D%!Way!" (CALL:movefiles "%%I" "%D%!Way!") else (move /y "%%I" "%D%!Way!\.." > NUL)
rd /s/q "%S%" & if %R%==1 md "%S%"
goto end
:movefiles
dir %1 | find "%noFiles%" > NUL & if ERRORLEVEL 1 move /y "%~1\*.*" %2 > NUL
goto :eof
:paramERR
echo Source or Destination does not exist
:end
I think the easiest thing you could do is to modify your Attempt 2
from
MOVE "C:\Source" "C:\Destination"
to
MOVE "C:\Source\*" "C:\Destination"
KISS ;-)
Edit: this seem not to work, so my advice is to trash away the crappy DOS command line and use Cygwin with BASH as a shell! (or just add the cygwin binaries to the path so you can use mv within DOS, thus not having to change the shell as your requirements state).

Resources