Robocopy copy contents of current folder - robocopy

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"

Related

Robocopy is not working in PowerShell

I am trying to run a robocopy script in PowerShell so I can insert it into my other PowerShell script:
$SERVERPATH = "E:\Cisco Config Backups"
$NETWORKPATH = "\\SERVER\Cisco Config Backups"
robocopy $SERVERPATH $NETWORKPATH /E /XC /XN /XO /R:0 /W:0 /COPY:T /LOG:C:\logs\robocopy_logs\config_copy.log /TEE /NP
However when I run this code I see robocopy working where it gives me the following output:
ROBOCOPY :: Robust File Copy for Windows
Started : Thursday, June 30, 2016 1:52:56 PM
Source : E:\Cisco Config Backups
Dest : \\SERVER\Cisco Config Backups
Files : *.*
Options : *.* /TEE /S /E /DCOPY:D /COPY:T /NP /XO /XN /XC /R:0 /W:0
But nothing gets copied over, I want to use robocopy for the logging and so I can have it skip files that already exist.
Please let me know if I am doing anything wrong.
The /COPY:T criteria is specifying to only copy file timestamps, so it is not copying the actual data of the file. However, since there are no files on the destination to copy the timestamps to, nothing happens.
You need to specify at least /COPY:DT so that the file data will be copied, but you can probably just remove the /COPY switch completely as you likely want the file attributes as well, which will get copied by default along with the file data and timestamps.
From robocopy /?:
/COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT).
(copyflags : D=Data, A=Attributes, T=Timestamps).
(S=Security=NTFS ACLs, O=Owner info, U=aUditing info)
You also need to review all of the switches you are using as some are conflicting--not sure what robocopy does in those cases. As Matt points out in the comment below, you have /E and /S. Do you want to copy empty subdirectories or not? /E specifies to copy empty directories, but /S specifies to NOT copy empty directories.

Robocopy - make a copy of single file in same directory

Using robocopy is it possible to make a copy of a file in same directory?
Something like this...
robocopy c:\temp\file.txt c:\temp\file_copy.txt
COPY could be used. Use /Y to confirm an overwrite.
COPY /Y "C:\temp\file.txt" "C:\temp\file_copy.txt"
I was pulling my hair out to try and figure this problem out. I finally found my own solution and maybe it will help you too.
I noticed that the syntax used to select the entire directory could be used to select a single file.
ROBOCOPY "*" "Directory source" "Directory Output unc path or non"
The above code will copy everything from the directory source folder to the directory output path.
Let's say you only wanted to copy 1 file from the directory source named "test.txt"
In order to do that use the following code:
ROBOCOPY "*test.txt" "Directory source" "Directory Output unc path or non"
Thats about it. It works really well and will only copy the file name you want.
Alternatively you can use
ROBOCOPY "*.txt" "Directory source" "Directory Output unc path or non"
to copy out all text documents from the directory source.
Similarly this will also work with any .ext
.zip .exe .txt .pdf etc..
I signed up to answer this question with a better method. Let me know if I succeeded.

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

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

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