howdy y'all! Working on coding some simple pipeline tools with batch files in windows and wanted to pick some brains in here. Ultimately what I'm trying to do is duplicate the entire absolute path of a target folder, but in another location. So say I have a target folder:
e:\folder01\folder02\folder03. (folder03 is my target folder)
What I want to do is drag and drop my target folder onto a batch file, and then make a copy of that folder in a folder of my choosing. So the result might look like:
d:\newfolder01\folder01\folder02\folder03
I only need to create the folders necessary to drill down to folder 03, no other files or folders
I know I can get the current folder path by using % arguments in the batch files, but not sure how append/modify the text string to make the new directory string to pass along to mkdir in the command line.
Thoughts?
-Chad
Creating a copy of a full directory path....
Moderator: DosItHelp
Re: Creating a copy of a full directory path....
That is all you need. Just use the command modifiers as described in the help file for the CALL and FOR commands.batchdos_cb wrote: ↑07 Jan 2021 17:32
I know I can get the current folder path by using % arguments in the batch files
Code: Select all
Substitution of batch parameters (%n) has been enhanced. You can
now use the following optional syntax:
%~1 - expands %1 removing any surrounding quotes (")
%~f1 - expands %1 to a fully qualified path name
%~d1 - expands %1 to a drive letter only
%~p1 - expands %1 to a path only
%~n1 - expands %1 to a file name only
%~x1 - expands %1 to a file extension only
%~s1 - expanded path contains short names only
%~a1 - expands %1 to file attributes
%~t1 - expands %1 to date/time of file
%~z1 - expands %1 to size of file
%~$PATH:1 - searches the directories listed in the PATH
environment variable and expands %1 to the fully
qualified name of the first one found. If the
environment variable name is not defined or the
file is not found by the search, then this
modifier expands to the empty string
The modifiers can be combined to get compound results:
%~dp1 - expands %1 to a drive letter and path only
%~nx1 - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
environment variable for %1 and expands to the
drive letter and path of the first one found.
%~ftza1 - expands %1 to a DIR like output line
-
- Posts: 2
- Joined: 07 Jan 2021 17:28
Re: Creating a copy of a full directory path....
Well, I got a script working that pretty much does what I want. See below:
Only thing I'm having issues with is that it doesn't like spaces in filenames? Is there any easy way to handle that?
Code: Select all
@echo off
REM Go to your target folder and drag a file from it onto the batch file to activate the script.
REM Change the drive letter of the destination path below in destdrive.
set "destdrive=E:\"
REM The line below grabs the file path dragged on to batch file. Does not currently work if the filename/path has spaces in it.
set "originpath=%~1"
REM You can add folders to the front of the path with line below.
set "newfolder=newfolder\"
FOR %%i IN ("%originpath%") DO (
set "filepath=%%~di%%~pi"
)
set "new=%destdrive%%newfolder%%filepath:~3%"
echo %new%
echo "Copying %filepath% to %new%"
robocopy %filepath% %new% /E
pause
Re: Creating a copy of a full directory path....
Quotes! Quotes! And more Quotes. That is a best practice for batch files. They protect spaces and special characters. You chose to use them with your SET command but not on your robocopy command.batchdos_cb wrote: ↑07 Jan 2021 22:30Well, I got a script working that pretty much does what I want. See below:Only thing I'm having issues with is that it doesn't like spaces in filenames? Is there any easy way to handle that?Code: Select all
@echo off REM Go to your target folder and drag a file from it onto the batch file to activate the script. REM Change the drive letter of the destination path below in destdrive. set "destdrive=E:\" REM The line below grabs the file path dragged on to batch file. Does not currently work if the filename/path has spaces in it. set "originpath=%~1" REM You can add folders to the front of the path with line below. set "newfolder=newfolder\" FOR %%i IN ("%originpath%") DO ( set "filepath=%%~di%%~pi" ) set "new=%destdrive%%newfolder%%filepath:~3%" echo %new% echo "Copying %filepath% to %new%" robocopy %filepath% %new% /E pause
There is no point in assigning the command line argument to an environmental variable and then using a FOR command to put it back together. You are basically back to where you were.
Not sure why you chose the way to get the filepath they way you did. Why put the drive letter in the path and then do a substring to get rid of it??????
Also your code doesn't work. When you put the original path back into the FOR command it is truncating the last folder because it doesn't have a backslash at the end of it. It thinks the last folder of the path is the file name. So you would also have to include %~nx as well.
And the paths that YOU created have a trailing back slash. Robocopy will see that as an escape character.
This is the most direct approach to what you are trying to accomplish
Code: Select all
@echo off
REM Go to your target folder and drag a file from it onto the batch file to activate the script.
REM Change the drive letter of the destination path below in destdrive.
set "destdrive=E:\"
REM You can add folders to the front of the path with line below.
set "newfolder=newfolder\"
set "new=%destdrive%%newfolder%%~pnx1"
echo Copying "%1" to "%new%"
robocopy "%~1" "%new%" /E
pause