I am using robocopy in a batch file to copy everything from the directory where the batch file is run, to a destination folder like so:
robocopy %~dp0 "D:\Destination Folder" /E
In the command window, running the above outputs the full path to %~dp0
like this:
C:\Folder\Another Folder\Batch File Runs From This Folder\This Folder Gets Copied\File.txt
I want the above output to exclude all of the folders leading up to the one where the batch file runs from, in other words exclude only %~dp0
but retain the rest of the path so I can see what was copied, so it would end up only outputting this:
This Folder Gets Copied\File.txt
There are no options in Robocopy to do this and I have looked at things like find.exe
and findstr.exe
but they cannot exclude a portion of a line, those both only have a /v
option that can hide lines containing the text you specify. That's no use since hiding %~dp0
would hide the entire line and show no output.
I know if there's a way to do this I can just put a pipe at the end of the robocopy line with something like this, if such a thing like a "hidestring" application exists:
robocopy %~dp0 "D:\Destination Folder" /E | hidestring %~dp0
That is just an example of how it might work, but like I said there is no such program "hidestring" - I wish there was!
Robocopy does have a /LEV:n
option where n
is the amount of directory levels deep you want to copy, but using that option has no bearing on the output, the full path including %~dp0
is output.
Is there any way to do this?
Cheers folks.
EDIT: @Jeff Zietlin, here's the full content of the command window:
C:\Folder\ToBeCopied>robocopy C:\Folder\ToBeCopied\ "D:\Destination" /E /w:0 /r:0 /NP /NS /NDL /NJH /NJS /XX
New File C:\Folder\ToBeCopied\Copy Current Directory.bat
New File C:\Folder\ToBeCopied\Folder With File\File.txt
C:\Folder\ToBeCopied>pause
Press any key to continue . . .
Where the above says this:
New File C:\Folder\ToBeCopied\Copy Current Directory.bat
New File C:\Folder\ToBeCopied\Folder With File\File.txt
I want it to just say this:
New File ToBeCopied\Copy Current Directory.bat
New File ToBeCopied\Folder With File\File.txt
In other words, do not display the current directory's path C:\Folder
(aka %~dp0
)
EDIT 2:
Jeff, if I run this in a batch file called Copy Current Directory.bat
pushd %~dp0
robocopy . "D:\Destination" /E
popd
pause
I get this in the command window:
C:\Folder\ToBeCopied>pushd C:\Folder\ToBeCopied\
C:\Folder\ToBeCopied>robocopy . "D:\Destination" /E
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : 11 June 2021 17:13:45
Source : C:\Folder\ToBeCopied\
Dest : D:\Destination\
Files : *.*
Options : *.* /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
1 C:\Folder\ToBeCopied\
100% New File 167 Copy Current Directory.bat
New Dir 1 C:\Folder\ToBeCopied\Folder With File\
100% New File 0 File.txt
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 2 1 1 0 0 0
Files : 2 2 0 0 0 0
Bytes : 167 167 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : 11 June 2021 17:13:45
C:\Folder\ToBeCopied>popd
C:\Folder\ToBeCopied>pause
Press any key to continue . . .
ROBOCOPY
on my box, I get quite a bit of formatted output. Are you just extracting the filename from the formatted output, or are there other options that you've eliminated as "irrelevant"?%~dp0
which I want to hide in the output. Since%~dp0
ends before the sub-directories that get copied, if there's a way to hide just%~dp0
it should leave the rest of the line displaying only the folders/files that are copied, not the entire path to them (which I don't care about or need to see, it's taking up a lot of space and making each line go off the edge of the command window).