Drive letters and UNC paths

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Drive letters and UNC paths

Post by MigrationUser »

03 Jan 2008 22:18
drifty

I have been trying to create some logic for a robocopy script and could use some help.
What I would like to do is allow a user to select and copy files from a complete drive, specific folder or server.
For UNC paths and folders with spaces I need to encapsulate the source with quotes but when you only have a source drive such as C:\ the source can't have quotes in order to run.

Code

Code: Select all

set /P "Source=Type Source (C:\, D:\My Stuff, \\Server\Share, etc.): "

SET STRING=%Source%
  IF "%STRING:~-2%"==":\" SET STRING1=%Source%
  ECHO String: %STRING1%
Pause
If I enter F:\ at the prompt then this sets the source (string1) as D:\ and that will work.
However if a user wants a single folder such as D:\My Stuff I need to have "D:\My Stuff" as the source and a server share \\Server\Share needs to be set as "\\Server\Share" also within quotes.

Any help is appreciated.
Thanks
-Frank

----------------------------

#2 04 Jan 2008 11:23
bluesxman


The root of your problem stems from the way robocopy interprets the backslash "\" character.

From what I've seen, when a directory string with a trailing "\" is double-quoted it treats it as an "escape" of the closing double-quote, therefore it gets totally confused by the rest of your command. Why on earth it would want to do this has, thus far, escaped me (no pun intended). It actually took me quite some time to figure out why some of my scripts would fail at the robocopy command.

What you need to do, in order to double-quote such a string, is escape the trailing backslash with another backslash.

But I digress.

There are a few of things you could do to solve your problem, here are a couple of them:
* * UNTESTED * *

Code: Select all

set /P "Source=Type Source (C:\, D:\My Stuff, \\Server\Share, etc.): "

REM how I might do it (allows for if the user enters "D:\stuff\")...
if "%source:~-1%" EQU "\" (set string1=%source%\) ELSE (set string1=%source%)
ECHO Option 1 = %STRING1%

REM what you actually asked for...
IF "%source:~-2%" EQU ":\" (SET STRING2=%Source%) ELSE (set string2="%source%")
ECHO Option 2 = %STRING2%
Though there's no easy way to account for the user typing stupid things -- for that you really ought to, at the very least, add some logic to make sure the source exists as a directory, and demand new input if there's a problem.

cmd | *sh | ruby | chef

----------------------------

#3 04 Jan 2008 16:28
drifty


That works pretty well except for one new glitch. When I create the output folder structure for the destination I can't have the colon (:) in the path.
So if the source is "F:\Temp My stuff" I need to create the folder structure "F\Temp My stuff" by taking the first character, skipping the second and taking the remainder.
If the source is \\Server\Share I need to create the folder structure \Server\Share.

Code

Code: Select all

REM Remove :\ characters from Source Drive Letter to be used in the Destination path structure if they exist.
REM Extract only the last 2 characters ("%source:~-2%" EQU ":\" of the %Source%)
SET STRING=%Source%
IF "%source:~-2%" EQU ":\" (SET STRING1=%STRING:~0,-2%) ELSE (set string1="%source%")

REM Encapsulate the Source string if it is a UNC path or Drive Letter/Folder
IF "%source:~-2%" EQU ":\" (SET STRING2=%Source%) ELSE (set string2="%source%")
ECHO String1: %STRING1%
ECHO Source: %Source%
ECHO Option 2 = %STRING2%

REM the cmd line:
ECHO %drive_letter%\%Project_Alias%\Scripts\robocopy.exe /Job:"%drive_letter%\%Project_Alias%\Scripts\%Job_File%" %STRING2% "%drive_letter%\%Project_Alias%\%Collection_Type%\%Custodian%\%STRING1%" /E /Log+:"%drive_letter%\%Project_Alias%\Logs\%Collection_Type%_Collection.txt"   

Pause
----------------------------

#4 07 Jan 2008 13:29
bluesxman


If I understand your question correctly, you should be able manipulate the source to meet your needs, for example:
* * UNTESTED * *

Code: Select all

set /p "source=Enter source : "

REM remove any double quotes, as they shouldn't be there and will probably annoy the code further down
set "source=%source:"=%"

REM remove any colons
set "source.tidy=%source::=%"
REM I actually prefer to turn the colon into a different character, such as $
REM in which case use this instead of the above:
REM set "source.tidy=%source::=$%"

REM loop to remove duplicate \ symbols, one or two interations should be enough
REM but we'll be robust and keep going till all the blighters are gone.
:loop
if "%source.tidy:\\=\%" NEQ "%source.tidy%" (
    set "source.tidy=%source.tidy:\\=\%"
    goto :loop
)
Now you can use %source.tidy% as part of your target path.

Last edited by bluesxman (07 Jan 2008 13:30)

cmd | *sh | ruby | chef

----------------------------

#5 08 Jan 2008 16:32
drifty


Thanks for all the help.
With a little tweking this worked great.
-Frank
Post Reply