Exclude %~dp0 from line output in Windows batch file?
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" /EIn 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.txtI 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.txtThere 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 %~dp0That 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.txtI want it to just say this:
New File ToBeCopied\Copy Current Directory.bat
New File ToBeCopied\Folder With File\File.txtIn 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
pauseI 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.ahkwith no path, which seems to be what you want.
7More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"