M BUZZ CRAZE NEWS
// general

Exclude %~dp0 from line output in Windows batch file?

By Emma Martinez

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 . . .
5

1 Answer

Instead of using %~dp0 directly in the ROBOCOPY command, use

...
pushd %~dp0
ROBOCOPY . "D:\Destination Folder" /E
popd
...

When I do that, the filename lines from ROBOCOPY look like e.g.,

100% New File 800 Esperanto.ahk
100% New File 644 Sharp-S.ahk
100% New File 473 Sharp-S2.ahk

with no path, which seems to be what you want.

7

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy