Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:34:57+00:00 2026-06-14T17:34:57+00:00

I am a bit new to DOS batch files, and I am having a

  • 0

I am a bit new to DOS batch files, and I am having a hard time wrapping my head around ways to solve my problem.

What I need to do: I have a large, nested source folder structure, let’s say it lives here:

C:\dir1\dir2\dir3\file.txt

And I have a mirrored destination structure, although a portion of the root is different:

x:\dirA\dirB\dir1\dir2\dir3\file.txt

My current batch copies all files in the source structure to a destination folder, keeping the folder structure, which is what I want.

The problem:
The intention of my script is to drag a folder from the source structure above onto the bat file, and have it copy the files to the destination. What I want to do, is to allow the user to drag a folder from source dir, let’s say /dir2 and all of its subfolders/files onto the batch file, and have it copy the contents over to the SAME spot in the destination structure…

So in this example, the batch file should copy everything in and below:

C:\dir1\dir2\

into

x:\dirA\dirB\dir1\dir2\.

Fortunately (I think) my destination folder structure won’t be changing, although the source might be in a different location on each machine. So, if there is a clever way to detect where in the source tree I am, and then replace a portion of the destination path… ???

Here’s my simple script so far:

    @echo off
    set /p FILETYPE="What file type do you want to copy? (For example use txt, max, f3d, or * to copy all files.)"

    xcopy %1\*.%FILETYPE% c:\output /s 
    pause

Thanks so much for any help you guys can give! 🙂
Ken

UPDATE: Adding updated code sample here because I cannot get my comment to format or allow me enough chars. The stuff below may not be completely correct, I am just trying my best to be clear. I have figured out more since I posted this, basically I need to figur out how (or if possible) to use a string after delims, it only seems to check for each character…

    @echo off
    rem user drag folder onto .bat (left click, move folder using mouse onto .bat icon)
    rem %1 in ex is C:\random_folder\another_folder\proj1\area1\scene23
    set destRoot=X:\companyname\allprojects\proj1
    set rootDir=proj1
    rem assume %1 is folder dragged onto .bat file 
    for /f "tokens=2* delims=<foldername???>" %%a in ("%1") do (
        set part1=%%a
        set chunk=%%b
    ) 
    set finalDest=destRoot+chunk
    xcopy %1\* %finalDest% /E /EXCLUDE:exclusions.txt
    pause

I am hoping to create this: “X:\companyname\allprojects\proj1\area1\scene23”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:34:58+00:00Added an answer on June 14, 2026 at 5:34 pm

    Is the x:\dirA\dirB\ part of the destination folder known? If so, the solution is easy:

    @echo off
    rem Get current directory, ie: "C:\dir1\dir2"
    set curDir=%cd%
    rem Eliminate the disk drive, ie: "\dir1\dir2"
    set curDir=%curDir:~2%
    rem Copy to right destination, ie: "x:\dirA\dirB\dir1\dir2"
    xcopy *.%FILETYPE% "x:\dirA\dirB%curDir%"
    

    I hope it helps…

    EDIT: Clarification to new comments requested

    Ok. There is a source folder and a destination folder. The program above assume that the current folder for the program is source folder, but this may be provided as a parameter if needed. The program achieve the following copy processes:

    From source folder          -> To destination folder
    C:\dir1\dir2                -> x:\dirA\dirB\dir1\dir2
    C:\dir1\dir2\dir3           -> x:\dirA\dirB\dir1\dir2\dir3
    C:\dir1\dir2\dir3\what\ever -> x:\dirA\dirB\dir1\dir2\dir3\what\ever
    C:\XYZ                      -> x:\dirA\dirB\XYZ
    

    If this is not what you want then explain it as concise as possible (use “files”, “folder”, “part of name” terms, not “I like”, “what if”, etc), and include three or four examples.

    Also, note that for me drag a folder mean a drag&drop operation taking the source folder pressing the right button of the mouse and leave it over the icon of the Batch file. If you are NOT refering to this operation, please do NOT use “drag” term; use “copy” instead (please also clarify this point).

    EDIT: Solution to new requirements

    OK! You have not explained correctly before your last example! This problem is just about matching two names: last part of first name must match the same part in second name, and combine first name and the rest of second name. Right?:

    First name:       X:\companyname\allprojects\proj1
    Second name: C:\random_folder\another_folder\proj1\area1\scene23
    Result:           X:\companyname\allprojects\proj1\area1\scene23
    

    This is the solution:

    @echo off
    setlocal EnableDelayedExpansion
    rem user drag folder onto .bat (left click, move folder using mouse onto .bat icon)
    rem %1 in ex is C:\random_folder\another_folder\proj1\area1\scene23
    set sourceFolder=%~1
    set destRoot=X:\companyname\allprojects\proj1
    rem Get last part of First name (destRoot) ie: lastPart=proj1
    for %%a in (%destRoot%) do set lastPart=%%~Na
    rem Delete from beginning of second name until that part, ie: result=\area1\scene23
    set result=!sourceFolder:*%lastPart%=!
    rem And insert the First name before the rest
    set result=%destRoot%%result%
    

    Or, combining the three steps in just one line:

    @echo off
    rem user drag folder onto .bat (left click, move folder using mouse onto .bat icon)
    rem %1 in ex is C:\random_folder\another_folder\proj1\area1\scene23
    set sourceFolder=%~1
    set destRoot=X:\companyname\allprojects\proj1
    for %%a in (%destRoot%) do set result=%destRoot%%sourceFolder:*%%~Na=%
    

    Please note that if destRoot may contain spaces, it must be enclosed in quotes in the FOR command: for %%a in ("%destRoot%") do ...

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit new to .Net development, been working in Java for some time
I am a bit new to SQLite , and I am having a slight
I'm a bit new to jQuery and I am having trouble setting up a
I am bit new with Rails (3.) and need your help to use renamed
I am a bit new to PHP OOP so I need you guys help
I'm a little bit new to PL/SQL and need something that looks a bit
I need to convert from a 32 bit Dos Date to a .NET System.DateTime
I am a bit new to Python, having dabbled in VB.NET, C, and a
Iam bit new to SpringMVC REST concept. Need a help from experts here to
I am a bit new to Android. What I need to do is send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.