0

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 . . .
bat_cmd
  • 511
  • 1
  • 6
  • 19
  • When I run `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"? – Jeff Zeitlin Jun 11 '21 at 13:31
  • Everything being output is relevant, apart from the text string `%~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). – bat_cmd Jun 11 '21 at 13:44
  • As a test, create a folder with this batch file and ONE other file in it, then execute the batch file, and cutpaste the ENTIRE output of ROBOCOPY. My testing MAY have a way of addressing your issue, but I want to see EXACTLY what you're getting to compare with what I am getting before I propose a solution. – Jeff Zeitlin Jun 11 '21 at 14:36
  • I can't paste that here, I will edit it into my question... – bat_cmd Jun 11 '21 at 15:22
  • OK, I get a bunch of header and footer info as well, but I think my solution below will work for you. – Jeff Zeitlin Jun 11 '21 at 15:29

1 Answers1

0

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.

Jeff Zeitlin
  • 4,416
  • 2
  • 16
  • 30
  • Note: I wasn't aware of the extra parameters /NS, NDL, etc., so I didn't include them. The principal still applies; instead of using `%~dp0` directly in the `ROBOCOPY` command, use the pushd/robocopy ./popd sequence instead. – Jeff Zeitlin Jun 11 '21 at 15:39
  • Sorry I can't get it to work. I tried it on 3 lines like you suggested, then tried a one-liner `pushd %~dp0 & ROBOCOPY . "D:\Destination Folder" /E & popd` but it doesn't work. I tried all combinations swapping the `%~dp0` for `.` and still always get the full path showing up. What gets me is there isn't a simple command line program to hide a string of text on a line, where it could just be run at the end of a line with a `|` character before it. – bat_cmd Jun 11 '21 at 15:43
  • Hmmm... Yes, when I use all of your additional switches, I get the same thing. – Jeff Zeitlin Jun 11 '21 at 15:47
  • The key seems to be the `/NDL` switch. When I include that, I get your results. When I don't, I get an additional line at the top listing the directory, but the individual files have no path. If you can tolerate the additional line, just drop that switch; you likely won't even need the pushd/popd. – Jeff Zeitlin Jun 11 '21 at 15:58
  • With only `/E` that's robocopy showing you a new file without the path, because robocopy shows files that way by default - above or below that line it also says `New Dir 1 C:\Folder\ToBeCopied\Folder With File` instead of the would-be solution: `New Dir 1 ToBeCopied\Folder With File` – bat_cmd Jun 11 '21 at 16:00
  • Where you had it displaying `Esperanto.ahk` above, that is cutting off any sub-directory that file might reside in :p I want to keep that, but only omit the current directory where this batch file is being run from. In the case of your `Esperanto.ahk` above, that's just robocopy itself removing the path (as per normal in robocopy). In other words adding the `pushd` command isn't going to affect that particular line since it's hiding the entire path (except the file) by default. This is so tough haha. – bat_cmd Jun 11 '21 at 16:09
  • I wasn't working with multiple levels of files; everything I was testing with was at the same level. I don't think you can get the hybrid that you want; it's likely to be all-or-nothing, with each individual directory listed separately from the files in it. – Jeff Zeitlin Jun 11 '21 at 16:13