Batch move files to folders. Folders are named based off of the filename












0















Trying to find a bat that can do this but no luck. With my very limited knowledge of batch coding I have no idea where to begin editing similar existing code.



I have files in a folder as seen below:



    \NASART1234.pdf
\NASART1235.ai
\NASART1236.eps


I want to move these files to another folder on a server where the folder structure is as follows:



\NASArt1234Original1234.pdf
\NASArt1235Original1235.ai
\NASArt1236Original1236.eps


It would place the file into the original folder of the filenames folder. Sorry if that is confusing.



I have found this which is close to what i want but minus the removal of characters.



@ECHO OFF
SETLOCAL
SET "sourcedir=U:sourcedir"
SET "destdir=U:destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%*.xml" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%*%%b*" '
) DO (
ECHO(MOVE "%%a" "%destdir%%%d"
)
)
)

GOTO :EOF









share|improve this question


















  • 1





    Your example files show different extensions but your code only lists *.xml files? To much for loops, you need only one. And use the for meta variable modifiers %%~na for the name, %%~dpafor drive and path etc. See ss64.com/nt/syntax-args.html and ss64.com/nt/pushd.html

    – LotPings
    Nov 23 '18 at 19:10


















0















Trying to find a bat that can do this but no luck. With my very limited knowledge of batch coding I have no idea where to begin editing similar existing code.



I have files in a folder as seen below:



    \NASART1234.pdf
\NASART1235.ai
\NASART1236.eps


I want to move these files to another folder on a server where the folder structure is as follows:



\NASArt1234Original1234.pdf
\NASArt1235Original1235.ai
\NASArt1236Original1236.eps


It would place the file into the original folder of the filenames folder. Sorry if that is confusing.



I have found this which is close to what i want but minus the removal of characters.



@ECHO OFF
SETLOCAL
SET "sourcedir=U:sourcedir"
SET "destdir=U:destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%*.xml" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%*%%b*" '
) DO (
ECHO(MOVE "%%a" "%destdir%%%d"
)
)
)

GOTO :EOF









share|improve this question


















  • 1





    Your example files show different extensions but your code only lists *.xml files? To much for loops, you need only one. And use the for meta variable modifiers %%~na for the name, %%~dpafor drive and path etc. See ss64.com/nt/syntax-args.html and ss64.com/nt/pushd.html

    – LotPings
    Nov 23 '18 at 19:10
















0












0








0








Trying to find a bat that can do this but no luck. With my very limited knowledge of batch coding I have no idea where to begin editing similar existing code.



I have files in a folder as seen below:



    \NASART1234.pdf
\NASART1235.ai
\NASART1236.eps


I want to move these files to another folder on a server where the folder structure is as follows:



\NASArt1234Original1234.pdf
\NASArt1235Original1235.ai
\NASArt1236Original1236.eps


It would place the file into the original folder of the filenames folder. Sorry if that is confusing.



I have found this which is close to what i want but minus the removal of characters.



@ECHO OFF
SETLOCAL
SET "sourcedir=U:sourcedir"
SET "destdir=U:destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%*.xml" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%*%%b*" '
) DO (
ECHO(MOVE "%%a" "%destdir%%%d"
)
)
)

GOTO :EOF









share|improve this question














Trying to find a bat that can do this but no luck. With my very limited knowledge of batch coding I have no idea where to begin editing similar existing code.



I have files in a folder as seen below:



    \NASART1234.pdf
\NASART1235.ai
\NASART1236.eps


I want to move these files to another folder on a server where the folder structure is as follows:



\NASArt1234Original1234.pdf
\NASArt1235Original1235.ai
\NASArt1236Original1236.eps


It would place the file into the original folder of the filenames folder. Sorry if that is confusing.



I have found this which is close to what i want but minus the removal of characters.



@ECHO OFF
SETLOCAL
SET "sourcedir=U:sourcedir"
SET "destdir=U:destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%*.xml" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%*%%b*" '
) DO (
ECHO(MOVE "%%a" "%destdir%%%d"
)
)
)

GOTO :EOF






batch-file batch-processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 18:40









JakeschJakesch

32




32








  • 1





    Your example files show different extensions but your code only lists *.xml files? To much for loops, you need only one. And use the for meta variable modifiers %%~na for the name, %%~dpafor drive and path etc. See ss64.com/nt/syntax-args.html and ss64.com/nt/pushd.html

    – LotPings
    Nov 23 '18 at 19:10
















  • 1





    Your example files show different extensions but your code only lists *.xml files? To much for loops, you need only one. And use the for meta variable modifiers %%~na for the name, %%~dpafor drive and path etc. See ss64.com/nt/syntax-args.html and ss64.com/nt/pushd.html

    – LotPings
    Nov 23 '18 at 19:10










1




1





Your example files show different extensions but your code only lists *.xml files? To much for loops, you need only one. And use the for meta variable modifiers %%~na for the name, %%~dpafor drive and path etc. See ss64.com/nt/syntax-args.html and ss64.com/nt/pushd.html

– LotPings
Nov 23 '18 at 19:10







Your example files show different extensions but your code only lists *.xml files? To much for loops, you need only one. And use the for meta variable modifiers %%~na for the name, %%~dpafor drive and path etc. See ss64.com/nt/syntax-args.html and ss64.com/nt/pushd.html

– LotPings
Nov 23 '18 at 19:10














1 Answer
1






active

oldest

votes


















0














You could use this batch file to move all non-hidden files in \NASART to the suitable subdirectories created before if not already existing with overwriting a file with same name in target directory.



@echo off
for %%I in ("\NASART*") do (
if not "%%I" == "%~f0" (
md "%%~dpnIOriginal" 2>nul
move /Y "%%I" "%%~dpnIOriginal"
)
)


The IF condition avoids moving the batch file on being stored also in directory \NASART and can be removed if that is surely never the case.



For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.




  • echo /?

  • for /?

  • if /?

  • md /?

  • move /?


Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul used to suppress the error message output by command MD to handle STDERR on directory to create already existing by redirecting this error message to device NUL.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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%2fstackoverflow.com%2fquestions%2f53451589%2fbatch-move-files-to-folders-folders-are-named-based-off-of-the-filename%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














    You could use this batch file to move all non-hidden files in \NASART to the suitable subdirectories created before if not already existing with overwriting a file with same name in target directory.



    @echo off
    for %%I in ("\NASART*") do (
    if not "%%I" == "%~f0" (
    md "%%~dpnIOriginal" 2>nul
    move /Y "%%I" "%%~dpnIOriginal"
    )
    )


    The IF condition avoids moving the batch file on being stored also in directory \NASART and can be removed if that is surely never the case.



    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.




    • echo /?

    • for /?

    • if /?

    • md /?

    • move /?


    Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul used to suppress the error message output by command MD to handle STDERR on directory to create already existing by redirecting this error message to device NUL.






    share|improve this answer




























      0














      You could use this batch file to move all non-hidden files in \NASART to the suitable subdirectories created before if not already existing with overwriting a file with same name in target directory.



      @echo off
      for %%I in ("\NASART*") do (
      if not "%%I" == "%~f0" (
      md "%%~dpnIOriginal" 2>nul
      move /Y "%%I" "%%~dpnIOriginal"
      )
      )


      The IF condition avoids moving the batch file on being stored also in directory \NASART and can be removed if that is surely never the case.



      For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.




      • echo /?

      • for /?

      • if /?

      • md /?

      • move /?


      Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul used to suppress the error message output by command MD to handle STDERR on directory to create already existing by redirecting this error message to device NUL.






      share|improve this answer


























        0












        0








        0







        You could use this batch file to move all non-hidden files in \NASART to the suitable subdirectories created before if not already existing with overwriting a file with same name in target directory.



        @echo off
        for %%I in ("\NASART*") do (
        if not "%%I" == "%~f0" (
        md "%%~dpnIOriginal" 2>nul
        move /Y "%%I" "%%~dpnIOriginal"
        )
        )


        The IF condition avoids moving the batch file on being stored also in directory \NASART and can be removed if that is surely never the case.



        For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.




        • echo /?

        • for /?

        • if /?

        • md /?

        • move /?


        Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul used to suppress the error message output by command MD to handle STDERR on directory to create already existing by redirecting this error message to device NUL.






        share|improve this answer













        You could use this batch file to move all non-hidden files in \NASART to the suitable subdirectories created before if not already existing with overwriting a file with same name in target directory.



        @echo off
        for %%I in ("\NASART*") do (
        if not "%%I" == "%~f0" (
        md "%%~dpnIOriginal" 2>nul
        move /Y "%%I" "%%~dpnIOriginal"
        )
        )


        The IF condition avoids moving the batch file on being stored also in directory \NASART and can be removed if that is surely never the case.



        For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.




        • echo /?

        • for /?

        • if /?

        • md /?

        • move /?


        Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul used to suppress the error message output by command MD to handle STDERR on directory to create already existing by redirecting this error message to device NUL.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 19:54









        MofiMofi

        28.5k83777




        28.5k83777
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53451589%2fbatch-move-files-to-folders-folders-are-named-based-off-of-the-filename%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin