How to copy files from folder tree dropping all the folders with Robocopy? - command-line

I have the following folder structure:
FolderA
--Folder1
--Folder2
--Folder3
...
--Folder99
Folders 1 through 99 have files in them.
All I want to do is to copy ALL THE FILES into ONE FOLDER, basically do a FolderA copy, and wipe out Folders 1-99 keeping all the files.
I'd like to do it with Robocopy from cmd.exe if possible (Windows Server 2008)

Why use robocopy? It's a good tool for a specific task but this is not the one.
You can simply use what cmd already gives you:
for /r %f in (*) do #copy "%f" target
This will essentially "flatten" your directory hierarchy. for /r will walk a directory tree recursively, looking for file names matching the given pattern. You can also specify the directory to start in:
for /r FolderA %f in (*) do #copy "%f" target
Within the loop it's just a simply copy of the file into a specified folder.

Robocopy is a great tool... when you have a job it can handle. Why not use xcopy?
If you have two drives you can just use xcopy:
XCOPY C:\*.* D:\NewFolder\ /S
Or use XXCOPY for one drive:
XXCOPY C:\*.* C:\NewFolder\ /S /CCY
XXCOPY

Get-ChildItem -Path source -Recurse -File | Move-Item -Destination dest

Related

Command Line Copy or Delete

I used robocopy to copy just pdf's from an external hard drive and place them on our server.
source destination *.pdf
It copied over all the folders and subfolders even if there wasn't a .pdf file in the folder. Can someone help me either....
a) Delete empty folders or b) Copy over only folders and subfolders that have .pdf's in them?
Thank you!
Use the following command for copy over only folders and subfolders that have an extension in them in this case ".pdf"
ROBOCOPY sourcePath destPath *.pdf /MIR /S
This will only all files with that extensions and folders that have files with that extension and does not include empty folder.
Try using this ROBOCOPY command for deleting empty folders:
ROBOCOPY myfolder myfolder /S /MOVE
Here source and destination both are 'myfolder'.

Copying folder structure to location that doesn't exist

I want to copy a folder, complete with subdirectories, files and files within subdirectories, preserving the structure and create them in a new location that did not previously exist. This is my PowerShell code
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\foldertwo -recurse -Container
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\folderthree -recurse -Container
foldertwo exists and is empty, folderthree does not exist.
If I run the script, the structure is created correctly in foldertwo, however folderthree gets created, but contains only all the files from the entire substructure, all at the root folderthree level. It has not recreated the subfolders within folderone, just put all the files at the root level of folderthree.
What have I done wrong?
Here's a very basic, but fully working and tested example, building on your confirmation above that I understood the issue at-hand:
$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
if (!(Test-Path "C:\Development\PowerShell\$folder"))
{
mkdir ("C:\Development\PowerShell\$folder") | Out-Null
}
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\$folder -recurse -Container
}
From what I understand, the question is about recreating the folder structure from [source] to [destination]. As using CmdLets is kind of overkill (and performance loss), I suggest simple batch command that may also be ran in powershell.
xcopy [source] [destination] /T /E
xcopy is a function to copy file and directory trees.
Help provides us info on usefult parameters on the case:
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.

robocopy /MIR - don't delete desktop.ini in destination

I want to sync folders between two computers, one with XP and one with Vista. I want the two folders mirrored, except for security settings and folder settings. It is my understanding that /MIR switch will delete any 'extra' files in the destination folder, which would include the desktop.ini files. I can avoid copying desktop.ini files with /XA:SH How can I prevent robocopy from deleting the destination desktop.ini files?
If I have to do any extra scripting, I prefer PowerShell. But I hope robocopy can do it on its own.
Thanks.
/XF desktop.ini
Will exclude Desktop.ini (from copy or purge).
Replace the /MIR switch with /E, and don't use the /PURGE parameter.
Explanation: /MIR is the equivalent of using /E /PURGE, so by using /E without /PURGE, you achieve the results you desire.
Create a bat file so you can mirror the source at the destination and later once the mirror process is finish erase the files you desired.
For example:
robocopy SOURCE DEST /E /PURGE or /MIR or /E
command to erase recursively (test the command before running at production):
del /s DRIVe:\DESTINATION\desktop.ini
Hope this helps,
Luis

How to save file structure to text file?

I have a folder of media files that are about 1TB big. I want to save the file names and directory structure to a text file for backup and reference. I want to attach a batch or PowerShell script to my backup process so the file gets saved before the backup. Does anyone know an easy way to do this?
You can use the built in tree.com utility:
tree c:\folder /F
There's also a PowerShell function, Show-Tree, in PSCX 2.0:
http://rkeithhill.wordpress.com/2010/05/10/pscx-2-0-show-tree/
a pure dir solution
dir /b /s c:\folder >foldertree.txt
has the advantages over Shay and mjolinor solutions that
it does not require powershell, just a plain cmd command
the result list contains the fully specified filenames which is a better format for postprocessing of any kind.
To just save the directory structure and file names:
get-childitem <dir> -recurse | select -expand fullname > dirtree.txt
Open powershell in the folder
then run this to show the files tree in your powershell:
tree /a /f
or save it as txt file:
tree /a /f > tree.txt

Robocopy copy contents of current folder

How would you translate this xcopy command into Robocopy:
xcopy *.* "C:\DestinationFolder\"
Keeping in mind that the current folder where the command is run changes dynamically (and hence the source folder is unknown in advance).
Thanks.
robocopy . "c:\dest"
Note you don't need to specify a wildcard in robocopy, by default it copies everything unless you use the /xf /xd flags to exclude certain files.
Robocopy DOES support wildcards.
You're expecting > robocopy SOURCE DEST but type > robocopy *.txt c:\folderdest\ to copy the current folder. If you look at the output from robocopy it will show "Files : *.txt" and "Source = c:\folderdest"
So in fact you can do > robocopy WILDCARD SOURCE DEST. If you want to use the CURRENT folder you need to use . (as has been mentioned here). So you would use > robocopy *.txt . c:\folderdest\.
Screenshot: http://i.stack.imgur.com/Xyxt4.png
As an addition:
If robocopy is started from an administrator console, the current folder "." will point to Windows\system32.
You can use the following commands at the top of your batch file to fix this:
#setlocal enableextensions
#cd /d "%~dp0"