3rutu5
3rutu5

Reputation: 121

XCopy to path with a prompt and wildcard

I'm wanting to repurpose a batch file and the latest constraint is the destination location has directories that has a unique identifier and description. I need to prompt for the unique identifer and use a wildcard to bypass the description. for example "\serverB\dest\ID - descriptions\DIR1\DIR2\

set DEST="\\serverB\Dest"
set /p UNIQUE ID=Enter ID
echo %UNIQUE_ID%
>NULL xcopy "\\serverA\source\something\file.txt" "%DEST%\%UNIQUE_ID% *\DIR1\DIR2" /i /y

Previously i didnt have this issue, so the long directory name wasnt an issue. Now i need to bypass the end component as the directories are not consistent in the description. once i get passed the directory with the unique ID\Desc the remainding subdirectories will be consistent.

set DEST="\\serverB\Dest"
set /p UNIQUE ID=Enter ID
echo %UNIQUE_ID%
>NULL xcopy "\\serverA\source\something\file.txt" "%DEST%\%UNIQUE_ID% *\DIR1\DIR2"

i get invalid drive specification as the error.

I also dont work in IT or coding for a living, so please be gentle with any responses.

Upvotes: 0

Views: 252

Answers (2)

aschipfl
aschipfl

Reputation: 34899

You cannot use wildcards in the middle of a path, they can only be used in the very last path element.

To work around that, you could use a for /D loop to resolve the wildcard:

set "DEST=\\serverB\Dest"
:LOOP
set /P UNIQUE_ID="Enter ID: " || goto :LOOP
echo %UNIQUE_ID%
for /D %%D in ("%DEST%\%UNIQUE_ID% *") do (
    > nul xcopy "\\serverA\source\something\file.txt" "%%~D\DIR1\DIR2"
)

There have also a few further things been changed:

  • the quoted set syntax has been applied, so the quotation marks do no longer become part of the assigned value (see the first line);
  • a typo has been fixed, the prompted variable is named UNIQUE_ID but not UNIQUE ID;
  • it is demonstrated how to detect whether or not the user entered something in the Enter ID prompt by using conditional execution;
  • the null device in Windows is called nul but not null;

Upvotes: 1

Ben Personick
Ben Personick

Reputation: 3264

You can't use a File Glob in a Directory name unless you are only looking to match a directory name, that is fairly consistent among windows executables.

instead, you can use a FOR Loop to match the Directory name using the File Glob for the directory name then save that into a variable and use that in your XCOPY Command.

@(
  ECHO OFF
  SETLOCAL
  SET "_Src_FilePath=\\serverA\source\something\file.txt"
  SET "_DstPath_Prefix=\\serverB\Dest"
  SET "_DstPath_Postfix=\DIR1\DIR2"
)

SET /P "_UNIQUE_ID=Enter ID: "
IF NOT DEFINED _UNIQUE_ID (
   ECHO. Exiting, you did not provide a Unique ID!
   GOTO :EOF
)
ECHO.ID Provided: "%_UNIQUE_ID%"

REM Get matching Director(y|ies) and copy file(s):

FOR /F "Tokens=*" %%D IN ('
  DIR /AD /B "%_DstPath_Prefix%\%_UNIQUE_ID% *"
') DO (
  ECHO.Matched Directory: "%%D" - Running Copy to: "%_DstPath_Prefix%\%%D%_DstPath_Postfix%"
  >NULL xcopy "%_Src_FilePath%" "%_DstPath_Prefix%\%%D%_DstPath_Postfix%"
)

Upvotes: 1

Related Questions