Advanced copying script


  1. Posts : 6
    Windows 7 Enterprise x86
       #1

    Advanced copying script


    I've got some experience doing some basic batch scripting earlier, but it seems that this one is just too advanced for me.

    I should create a script that copies one file (should be given as argument) from specific location to another and keeps the directory structure which is deeper than some specific folder of the source path. Let me explain this a bit more:

    The destination folder is something like:

    x:\stuff\

    ex 1. If the source file is in x:\some\thing\stuff\test\file.txt, the file is copied to x:\stuff\test\file.txt

    ex 2. If the source file is in x:\some\thing\stuff\test\test2\file.txt, the file is copied to x:\stuff\test\test2\file.txt

    of course if those folders 'test' and 'test2' don't exit in the destination folder they are created.

    how I get the path after the 'stuff' folder and put it into the destination path? for example in ex. 1, how do get the '\test\file.txt' and put it to the destination path like this: x:\stuff\test\file.txt

    Thanks!
      My Computer


  2. Posts : 1,261
    Windows 7 Professional 32-bit SP1
       #2

    purpleglow said:
    I've got some experience doing some basic batch scripting earlier, but it seems that this one is just too advanced for me.

    I should create a script that copies one file (should be given as argument) from specific location to another and keeps the directory structure which is deeper than some specific folder of the source path. Let me explain this a bit more:

    The destination folder is something like:

    x:\stuff\

    ex 1. If the source file is in x:\some\thing\stuff\test\file.txt, the file is copied to x:\stuff\test\file.txt

    ex 2. If the source file is in x:\some\thing\stuff\test\test2\file.txt, the file is copied to x:\stuff\test\test2\file.txt

    of course if those folders 'test' and 'test2' don't exit in the destination folder they are created.

    how I get the path after the 'stuff' folder and put it into the destination path? for example in ex. 1, how do get the '\test\file.txt' and put it to the destination path like this: x:\stuff\test\file.txt

    Thanks!
    Have you tried using XCOPY:

    Code:
     
    Copies files and directory trees.
    XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
                               [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
                               [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B]
                               [/EXCLUDE:file1[+file2][+file3]...]
      source       Specifies the file(s) to copy.
      destination  Specifies the location and/or name of new files.
      /A           Copies only files with the archive attribute set,
                   doesn't change the attribute.
      /M           Copies only files with the archive attribute set,
                   turns off the archive attribute.
      /D:m-d-y     Copies files changed on or after the specified date.
                   If no date is given, copies only those files whose
                   source time is newer than the destination time.
      /EXCLUDE:file1[+file2][+file3]...
                   Specifies a list of files containing strings.  Each string
                   should be in a separate line in the files.  When any of the
                   strings match any part of the absolute path of the file to be
                   copied, that file will be excluded from being copied.  For
                   example, specifying a string like \obj\ or .obj will exclude
                   all files underneath the directory obj or all files with the
                   .obj extension respectively.
      /P           Prompts you before creating each destination file.
      /S           Copies directories and subdirectories except empty ones.
      /E           Copies directories and subdirectories, including empty ones.
                   Same as /S /E. May be used to modify /T.
      /V           Verifies the size of each new file.
      /W           Prompts you to press a key before copying.
      /C           Continues copying even if errors occur.
      /I           If destination does not exist and copying more than one file,
                   assumes that destination must be a directory.
      /Q           Does not display file names while copying.
      /F           Displays full source and destination file names while copying.
      /L           Displays files that would be copied.
      /G           Allows the copying of encrypted files to destination that does
                   not support encryption.
      /H           Copies hidden and system files also.
      /R           Overwrites read-only files.
      /T           Creates directory structure, but does not copy files. Does not
                   include empty directories or subdirectories. /T /E includes
                   empty directories and subdirectories.
      /U           Copies only files that already exist in destination.
      /K           Copies attributes. Normal Xcopy will reset read-only attributes.
      /N           Copies using the generated short names.
      /O           Copies file ownership and ACL information.
      /X           Copies file audit settings (implies /O).
      /Y           Suppresses prompting to confirm you want to overwrite an
                   existing destination file.
      /-Y          Causes prompting to confirm you want to overwrite an
                   existing destination file.
      /Z           Copies networked files in restartable mode.
      /B           Copies the Symbolic Link itself versus the target of the link.
      /J           Copies using unbuffered I/O. Recommended for very large files.
    The switch /Y may be preset in the COPYCMD environment variable.
    This may be overridden with /-Y on the command line.
      My Computer


  3. Posts : 6
    Windows 7 Enterprise x86
    Thread Starter
       #3

    Dzomlija said:

    Have you tried using XCOPY:
    Well, the basic copying isn't the problem. I'd like to know how I can get only certain part of the file's current path and put it into the destination path. Like this:

    current path:

    c:\files\foo\test.txt

    destination path is:

    x:\files\ + i have to add this part from the source path (current path) foo\test.txt, so the destination path should be x:\files\foo\

    Let's explain a little bit more: what I'm trying to do is a copying script that gets filename as an argument and checks whats the path under the certain folder (in the above example this certain folder is 'files') and adds this path to destination path. My plan is to add this script to window's 'Send to:' menu.. so user just have to right click some file under the certain folder (it might be in the subfolder and that's the problem here) and 'send to: ' a script as an argument.

    Hope you got my idea! Thanks!
      My Computer


  4. Posts : 6
    Windows 7 Enterprise x86
    Thread Starter
       #4

    I propably forgot to mention that if the source file's folder is x:\temp\test\sub1\file.txt, in the destination the folder 'test' (under this folder, in both source and destination, the directory structure is the same) might be in a different path, e.g. z:\misc\zoo\foo\test\
      My Computer


  5. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #5

    This command copies everything, subfolders and files, from C:\Example to D:\Test\Test2\Example, creating the necessary folders:
    Code:
    robocopy C:\Example\ D:\Test\Test2\Example\ /MIR
    The command makes an exact mirror copy (the /MIR switch) of source folder structure to destination, i.e. file
    • C:\Example\Next_Level\And_One_More_Level\Some_File.xxx

    would be copied to
    • D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx


    Kari
      My Computer


  6. Posts : 6
    Windows 7 Enterprise x86
    Thread Starter
       #6

    Kari said:
    This command copies everything, subfolders and files, from C:\Example to D:\Test\Test2\Example, creating the necessary folders:
    Code:
    robocopy C:\Example\ D:\Test\Test2\Example\ /MIR
    The command makes an exact mirror copy (the /MIR switch) of source folder structure to destination, i.e. file
    • C:\Example\Next_Level\And_One_More_Level\Some_File.xxx
    would be copied to
    • D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx

    Kari

    Thanks for the reply Kari,

    I can't mirror the whole main folder, because there are lots of files that are only in destination directory, but are not in the source directory. Mirroring would remove the files from the destination folder, right?

    So, if I use robocopy like this:
    Code:
    robocopy c:\Example\Next_Level\And_One_More_Level\Some_File.xxx D:\Test\Test2\Example\ /MIR
    Will there be a folder D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx ?

    Thanks!
      My Computer


  7. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #7

    purpleglow said:
    Thanks for the reply Kari,

    I can't mirror the whole main folder, because there are lots of files that are only in destination directory, but are not in the source directory. Mirroring would remove the files from the destination folder, right?

    So, if I use robocopy like this:
    Code:
    robocopy c:\Example\Next_Level\And_One_More_Level\ D:\Test\Test2\Example\Some_File.xxx /MIR
    Will there be a folder D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx ?

    Thanks!
    Yes, if Some_File.xxx were a folder. Both source and destination strings in robocopy can only contain folders, no files. If you need to copy single files, use xcopy instead.

    If you don't want to / need to replicate the folder structure, replace the /MIR switch with /S or /E. These two switches do a copy but do not purge destination (delete files). Switch /S copies folders only if they contain files or subfolders, /E copies even the empty folders.

    Kari
    Last edited by Kari; 17 Jan 2011 at 08:39.
      My Computer


  8. Posts : 6
    Windows 7 Enterprise x86
    Thread Starter
       #8

    Kari said:
    purpleglow said:
    Thanks for the reply Kari,

    I can't mirror the whole main folder, because there are lots of files that are only in destination directory, but are not in the source directory. Mirroring would remove the files from the destination folder, right?

    So, if I use robocopy like this:
    Code:
    robocopy c:\Example\Next_Level\And_One_More_Level\ D:\Test\Test2\Example\Some_File.xxx /MIR
    Will there be a folder D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx ?

    Thanks!
    Yes, if Some_File.xxx were a folder. Both source and destination strings in robocopy can only contain folders, no files. If you need to copy single files, use xcopy instead.

    If you don't want to / need to replicate the folder structure, replace the /MIR switch with /S or /E. These two switches do a copy but do not purge destination (delete files). Switch /S copies folders only if they contain files or subfolders, /E copies even the empty folders.
    If I use robocopy with /E switch, without mirroring, doesn't it also copy the files that are already copied(appear in both source & destination already)?
      My Computer


  9. Posts : 6
    Windows 7 Enterprise x86
    Thread Starter
       #9

    Ok, I finally had a chance to do some testing with robocopy and it seems that robocopy skips the files that already exist in the destination folder. Thanks a lot for the help!
      My Computer


  10. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #10

    With robocopy you have several methods to include and / or exclude files. For instance:
    Code:
    robocopy C:\Example\ D:\Test\Test2\Example\ /E /MAXAGE:2
    The command copies the exact source folder structure to destination folder, including empty subfolders, excluding files that are over 2 days old.

    Here's a complete list of robocopy switches: Batch file copy made easy

    Kari
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 08:11.
Find Us