Attempt To Use Batch Files While Keep Structure












0















I wanted to use robocopy, but it is not possible due to my text file has filenames attached.
I ended up have to use copy instead.



I wanted to copy files like this...



Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf


Here is how my text file (ActivePDF.txt) listed..



product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf


My attempted batch file is looked like this



@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)


It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)



I think I'm getting there... but I do wonder if there is any cleaner way to do it.










share|improve this question























  • It's md "%my_folder%%%a" 2>nul that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%. To emphasise the point, you can omit %%a from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%".

    – AFH
    Feb 6 at 22:02













  • If you need to make sure that the directory path exists in the target, replace the md line by md "%my_folder%%%a.." 2>nul (this is a trick to avoid parsing the %%a variable).

    – AFH
    Feb 6 at 22:13











  • It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.

    – PanK
    Feb 11 at 21:26













  • If you want the source directory structure recreated in "%my_folder%", then you can use the same trick with the md on the copy command: copy /v /y "%main_folder%%%a" "%my_folder%%%a..".

    – AFH
    Feb 11 at 22:46











  • Yep, this copy command is the answer! copy /v /y "%main_folder%%%a" "%my_folder%%%a.." So, all I truly need is the .. part to go up by one folder!

    – PanK
    Feb 12 at 15:27


















0















I wanted to use robocopy, but it is not possible due to my text file has filenames attached.
I ended up have to use copy instead.



I wanted to copy files like this...



Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf


Here is how my text file (ActivePDF.txt) listed..



product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf


My attempted batch file is looked like this



@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)


It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)



I think I'm getting there... but I do wonder if there is any cleaner way to do it.










share|improve this question























  • It's md "%my_folder%%%a" 2>nul that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%. To emphasise the point, you can omit %%a from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%".

    – AFH
    Feb 6 at 22:02













  • If you need to make sure that the directory path exists in the target, replace the md line by md "%my_folder%%%a.." 2>nul (this is a trick to avoid parsing the %%a variable).

    – AFH
    Feb 6 at 22:13











  • It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.

    – PanK
    Feb 11 at 21:26













  • If you want the source directory structure recreated in "%my_folder%", then you can use the same trick with the md on the copy command: copy /v /y "%main_folder%%%a" "%my_folder%%%a..".

    – AFH
    Feb 11 at 22:46











  • Yep, this copy command is the answer! copy /v /y "%main_folder%%%a" "%my_folder%%%a.." So, all I truly need is the .. part to go up by one folder!

    – PanK
    Feb 12 at 15:27
















0












0








0








I wanted to use robocopy, but it is not possible due to my text file has filenames attached.
I ended up have to use copy instead.



I wanted to copy files like this...



Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf


Here is how my text file (ActivePDF.txt) listed..



product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf


My attempted batch file is looked like this



@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)


It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)



I think I'm getting there... but I do wonder if there is any cleaner way to do it.










share|improve this question














I wanted to use robocopy, but it is not possible due to my text file has filenames attached.
I ended up have to use copy instead.



I wanted to copy files like this...



Source: S:folderABCDEproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
Destination: C:wamp64FGproduct_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf


Here is how my text file (ActivePDF.txt) listed..



product_pdfDriveDriveDeluxeAluminumBathChair_RTL12202KDR.pdf
product_pdfDriveDriveCommodesNRS185007-11117N.pdf
product_pdfDriveDriveCommodes11125Series.pdf
product_pdfDriveDriveSuctionCupGrabBar_RTL13082.pdf
product_pdfDriveDriveChromeKnurledGrabBar.pdf


My attempted batch file is looked like this



@echo on
enableextensions
set main_folder=S:folderABCDE
set my_folder=C:wamp64FG
set log_file="%main_folder%CopyLog.txt"
for /f "delims=" %%a in (ActivePDF.txt) do if exist "%main_folder%%%a" (
md "%my_folder%%%a" 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a"
)


It did copied, but it ended up created folder with ".pdf" and actual PDF files are inside that ".pdf" folders. (Therefore, each PDF contained in the own folder...)



I think I'm getting there... but I do wonder if there is any cleaner way to do it.







windows batch file-transfer robocopy xcopy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 6 at 21:36









PanKPanK

32




32













  • It's md "%my_folder%%%a" 2>nul that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%. To emphasise the point, you can omit %%a from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%".

    – AFH
    Feb 6 at 22:02













  • If you need to make sure that the directory path exists in the target, replace the md line by md "%my_folder%%%a.." 2>nul (this is a trick to avoid parsing the %%a variable).

    – AFH
    Feb 6 at 22:13











  • It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.

    – PanK
    Feb 11 at 21:26













  • If you want the source directory structure recreated in "%my_folder%", then you can use the same trick with the md on the copy command: copy /v /y "%main_folder%%%a" "%my_folder%%%a..".

    – AFH
    Feb 11 at 22:46











  • Yep, this copy command is the answer! copy /v /y "%main_folder%%%a" "%my_folder%%%a.." So, all I truly need is the .. part to go up by one folder!

    – PanK
    Feb 12 at 15:27





















  • It's md "%my_folder%%%a" 2>nul that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%. To emphasise the point, you can omit %%a from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%".

    – AFH
    Feb 6 at 22:02













  • If you need to make sure that the directory path exists in the target, replace the md line by md "%my_folder%%%a.." 2>nul (this is a trick to avoid parsing the %%a variable).

    – AFH
    Feb 6 at 22:13











  • It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.

    – PanK
    Feb 11 at 21:26













  • If you want the source directory structure recreated in "%my_folder%", then you can use the same trick with the md on the copy command: copy /v /y "%main_folder%%%a" "%my_folder%%%a..".

    – AFH
    Feb 11 at 22:46











  • Yep, this copy command is the answer! copy /v /y "%main_folder%%%a" "%my_folder%%%a.." So, all I truly need is the .. part to go up by one folder!

    – PanK
    Feb 12 at 15:27



















It's md "%my_folder%%%a" 2>nul that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%. To emphasise the point, you can omit %%a from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%".

– AFH
Feb 6 at 22:02







It's md "%my_folder%%%a" 2>nul that's creating the subdirectories and causing your problem: omit this line and all your files will be copied to %my_folder%. To emphasise the point, you can omit %%a from the copy target, ie copy /v /y "%main_folder%%%a" "%my_folder%".

– AFH
Feb 6 at 22:02















If you need to make sure that the directory path exists in the target, replace the md line by md "%my_folder%%%a.." 2>nul (this is a trick to avoid parsing the %%a variable).

– AFH
Feb 6 at 22:13





If you need to make sure that the directory path exists in the target, replace the md line by md "%my_folder%%%a.." 2>nul (this is a trick to avoid parsing the %%a variable).

– AFH
Feb 6 at 22:13













It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.

– PanK
Feb 11 at 21:26







It ended up copy "my_folder" without sub directories that I'm intended to put into. I do needed those PDF files into sub directories as stated in text files.

– PanK
Feb 11 at 21:26















If you want the source directory structure recreated in "%my_folder%", then you can use the same trick with the md on the copy command: copy /v /y "%main_folder%%%a" "%my_folder%%%a..".

– AFH
Feb 11 at 22:46





If you want the source directory structure recreated in "%my_folder%", then you can use the same trick with the md on the copy command: copy /v /y "%main_folder%%%a" "%my_folder%%%a..".

– AFH
Feb 11 at 22:46













Yep, this copy command is the answer! copy /v /y "%main_folder%%%a" "%my_folder%%%a.." So, all I truly need is the .. part to go up by one folder!

– PanK
Feb 12 at 15:27







Yep, this copy command is the answer! copy /v /y "%main_folder%%%a" "%my_folder%%%a.." So, all I truly need is the .. part to go up by one folder!

– PanK
Feb 12 at 15:27












1 Answer
1






active

oldest

votes


















0














It's the commands in the for loop that are causing the problem: %%a contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul will create in the target folder a directory path which includes the PDF name as the last subdirectory.



When the target path is a directory the copy command copies the source file(s) into it, with the result you observed.



While it is possible to parse %%a to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a contains a file name with directory path, then %%a.. will reference the directory path. Even though %%a is a file path, the parsing of %%a.. will identify the containing directory without checking whether the last element of %%a is a file or directory.



So the commands in the for loop become:-



md "%my_folder%%%a.." 2>nul
copy /v /y "%main_folder%%%a" "%my_folder%%%a.."


The md is needed to create the target subdirectory if it does not exist.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1402874%2fattempt-to-use-batch-files-while-keep-structure%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It's the commands in the for loop that are causing the problem: %%a contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul will create in the target folder a directory path which includes the PDF name as the last subdirectory.



    When the target path is a directory the copy command copies the source file(s) into it, with the result you observed.



    While it is possible to parse %%a to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a contains a file name with directory path, then %%a.. will reference the directory path. Even though %%a is a file path, the parsing of %%a.. will identify the containing directory without checking whether the last element of %%a is a file or directory.



    So the commands in the for loop become:-



    md "%my_folder%%%a.." 2>nul
    copy /v /y "%main_folder%%%a" "%my_folder%%%a.."


    The md is needed to create the target subdirectory if it does not exist.






    share|improve this answer




























      0














      It's the commands in the for loop that are causing the problem: %%a contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul will create in the target folder a directory path which includes the PDF name as the last subdirectory.



      When the target path is a directory the copy command copies the source file(s) into it, with the result you observed.



      While it is possible to parse %%a to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a contains a file name with directory path, then %%a.. will reference the directory path. Even though %%a is a file path, the parsing of %%a.. will identify the containing directory without checking whether the last element of %%a is a file or directory.



      So the commands in the for loop become:-



      md "%my_folder%%%a.." 2>nul
      copy /v /y "%main_folder%%%a" "%my_folder%%%a.."


      The md is needed to create the target subdirectory if it does not exist.






      share|improve this answer


























        0












        0








        0







        It's the commands in the for loop that are causing the problem: %%a contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul will create in the target folder a directory path which includes the PDF name as the last subdirectory.



        When the target path is a directory the copy command copies the source file(s) into it, with the result you observed.



        While it is possible to parse %%a to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a contains a file name with directory path, then %%a.. will reference the directory path. Even though %%a is a file path, the parsing of %%a.. will identify the containing directory without checking whether the last element of %%a is a file or directory.



        So the commands in the for loop become:-



        md "%my_folder%%%a.." 2>nul
        copy /v /y "%main_folder%%%a" "%my_folder%%%a.."


        The md is needed to create the target subdirectory if it does not exist.






        share|improve this answer













        It's the commands in the for loop that are causing the problem: %%a contains both the subdirectory path and the PDF file name, so md "%my_folder%%%a" 2>nul will create in the target folder a directory path which includes the PDF name as the last subdirectory.



        When the target path is a directory the copy command copies the source file(s) into it, with the result you observed.



        While it is possible to parse %%a to extract the directory path and file names, there is a trick that can be used to reference the directory path without parsing: if %%a contains a file name with directory path, then %%a.. will reference the directory path. Even though %%a is a file path, the parsing of %%a.. will identify the containing directory without checking whether the last element of %%a is a file or directory.



        So the commands in the for loop become:-



        md "%my_folder%%%a.." 2>nul
        copy /v /y "%main_folder%%%a" "%my_folder%%%a.."


        The md is needed to create the target subdirectory if it does not exist.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 12 at 17:39









        AFHAFH

        14.4k31938




        14.4k31938






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1402874%2fattempt-to-use-batch-files-while-keep-structure%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

            Eduardo VII do Reino Unido