Robocopy - make a copy of single file in same directory - robocopy

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.

Related

Recursive Folder/File Copy Powershell Script - Robocopy won't stop copying once done with list

I'm trying to come up with a PS script that reads folder names from a txt file and recursively finds the folders/files in a source directory and then uses robocopy to copy to a destination directory, keeping the folder names, structure and timestamps. It looks like (in the log output) the script goes down the list and copies the folders and files over just great, but when it gets to the end of the list, it begins copying EVERYTHING else in the source directory.
The txt file data looks like:
400033
400042
400045
400047
400058
etc..
The 'script' looks like:
foreach ($line in
[System.IO.File]::ReadLines("C:\temp\filecopy\TEST_400210_400033.txt")) {
robocopy "J:\testSRC\$line" "S:\testDEST\$line" /DCOPY:DAT /E /R:3 /W:15 /log+:C:\temp\logs\TEST_ROBOCOPY_filecopy_Log1.txt /v /tee
}
Can anyone point me in the right direction please?
I've tried using Powershell's Copy-Item in a whole different script and was able to get that script to work but it's way too slow. Trying robocopy now. It reads and copies the files it is supposed to in the order it's supposed to, but once it's copied the list it starts a whole copy of the entire directory instead of stopping at the files in the text file.
Is there an extra line at the end of the file?
400033
400042
400045
400047
400058
[Blank Line]
i.e. it runs through all the file names then the last line will translate to:
$line = ""
Therefore:
robocopy "J:\testSRC\" "S:\testDEST\" /DCOPY:DAT /E /R:3 /W:15 /log+:C:\temp\logs\TEST_ROBOCOPY_filecopy_Log1.txt /v /tee
e.g. robocopy the root folder: robocopy "J:\testSRC\"
EDIT:
Try outputting the file to the screen to verify the input file is correct and listing the expected directories.
foreach ($line in
[System.IO.File]::ReadLines("C:\temp\filecopy\TEST_400210_400033.txt")) {
Write-Host "J:\testSRC\$line"
}

copy "this folder" to network drive (Robocopy or PowerShell)

Have a folder structure on local drive like this
and a similar structure on network drive like
C:\Documents\Projects\
\\myServer\Storage\Projects\
For each new project I create locally folders like
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I need PowerShell or Batch file. The batch is stored in folder abc and it shall perform
Robocopy C:\Documents\Projects\abc \\myServer\Storage\Projects\abc
(of course some switches are reasonable)
Next project, for example xyz
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I expect
Robocopy C:\Documents\Projects\xyz \\myServer\Storage\Projects\xyz
Each time I create new project I copy my sample folder structure that is supposed to contain the batch so I can quickly do copy for the project to network,
I think this can be solved by using variables for local and server base path or by doing som
I don't know which Robocopy parameters you need, but this could be a start. This assumes that you place this script in each \Projects\abc directory and run it from there.
$DestinationShare = '\\myServer\Storage'
$SourcePath = $PSScriptRoot
$DestinationPath = $PSScriptRoot -replace ".*?(?<path>\\Projects\\.*)","$DestinationShare`${path}"
robocopy $SourcePath $DestinationPath
This could be turned into a function that would have less static coding.
It is going to be simpler to just robocopy the entire Projects directory whenever it is required.
In batch you could do something like this:
#echo off
set "src=%~dp0"
set "dst=\\myServer\Storage"
for /d %%d in (%src%.) do set "name=%%~nd"
robocopy %src% %dst%\%name%\ /mir
%~dp0 is the directory in which the script resides. The for loop then extracts the name of that directory from the full path.

Robocopy deletes existing files in destination folder

I am new to Powershell and Robocopy usage.
I am trying to send some files to destination path. But destination path contains some files that is not contained in source. I want that files remain intact after copy operation.
When i try to run Robocopy from a powershell script like this:
Robocopy sourcePath destinationPath /MIR
it syncs two paths. So that destinationPath lonely files are deleted.
Is there any way to prevent this behavior?
Don't use the /MIR flag. That is exactly to keep the source and destination in sync, including deletion of files missing in source.
Use robocopy /E to copy a folder structure including empty subfolders. By default it only copies new and newer files.

Delete file if it exists anywhere on the windows Device

I Am trying to delete a file which exists in like many folders which are dynamically created and I have no clue how and where to start. What would be the best approach using scripting to delete this file ?
del has a /s switch to process all subfolders:
del /s c:\googledrivesync.exe
it may take a while to scan the folder tree. Get yourself a cup of coffee...
Assuming you want to delete specific files in a directory structure (as opposed to just deleting everything), from PowerShell, you could do
dir -recurse -file -filter nameOfFileToRemove rootDirectory | remove-item
This will recurse through the file tree, looking for files matching nameOfFileToRemove and then removing them. Also nameOfFileToRemove may contain wildcard characters like "nameOfFile*.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"