Rename files sequentially from input using batch file?












2














The company I work for processes bills, and I regularly have to check these bills for accuracy by comparing what was entered into the database vs. the actual bill image. Any time I find an error in processing, I must delete the bill from the database and send the image to a processor to re-enter the bill correctly. However, our system frequently encounters a glitch where if I try to upload the bill to the server that gets it to the processor, it says that a duplicate file exists. To bypass this issue I have to manually rename each image with a new name (we use rolls of control numbers to name our images).



I'd like to automate this process so that I can rename these files quickly in sequential order. I would like my batch file to have an input so that I can enter in the starting control number and it will rename each file in the directory in sequential order, starting with the control number I enter +1 to each additional file in the directory, for example it would go like this:



Batch file asks me starting control #. I enter in 6654821.





  • bill3210851.pdf renamed to 6654821


  • imagebill654.pdf renamed to 6654822


  • random_name.pdf renamed to 6654823


  • billerror.pdf renamed to 6654824


  • sendtomanual.pdf renamed to 6654825


The original name of the file is not at all relevant, they don't need to remain in any specific order as long as they are renamed sequentially.



So far what I have is this:



@echo OFF
title Batch Rename
color 5f

echo Hello! I'm the batch pdf renamer. Please enter the starting control number:
set /p start=

ren %userprofile%desktopneedrenamed*.pdf "%p%.pdf"

pause


Obviously this doesn't address renaming the files sequentially. I'm kind of stuck here.










share|improve this question
























  • How about using a 3rd party utility like Bulk rename tool? Follow this link and find out more. This tool will give the flexibility you need
    – Prasanna
    Nov 14 '14 at 3:17


















2














The company I work for processes bills, and I regularly have to check these bills for accuracy by comparing what was entered into the database vs. the actual bill image. Any time I find an error in processing, I must delete the bill from the database and send the image to a processor to re-enter the bill correctly. However, our system frequently encounters a glitch where if I try to upload the bill to the server that gets it to the processor, it says that a duplicate file exists. To bypass this issue I have to manually rename each image with a new name (we use rolls of control numbers to name our images).



I'd like to automate this process so that I can rename these files quickly in sequential order. I would like my batch file to have an input so that I can enter in the starting control number and it will rename each file in the directory in sequential order, starting with the control number I enter +1 to each additional file in the directory, for example it would go like this:



Batch file asks me starting control #. I enter in 6654821.





  • bill3210851.pdf renamed to 6654821


  • imagebill654.pdf renamed to 6654822


  • random_name.pdf renamed to 6654823


  • billerror.pdf renamed to 6654824


  • sendtomanual.pdf renamed to 6654825


The original name of the file is not at all relevant, they don't need to remain in any specific order as long as they are renamed sequentially.



So far what I have is this:



@echo OFF
title Batch Rename
color 5f

echo Hello! I'm the batch pdf renamer. Please enter the starting control number:
set /p start=

ren %userprofile%desktopneedrenamed*.pdf "%p%.pdf"

pause


Obviously this doesn't address renaming the files sequentially. I'm kind of stuck here.










share|improve this question
























  • How about using a 3rd party utility like Bulk rename tool? Follow this link and find out more. This tool will give the flexibility you need
    – Prasanna
    Nov 14 '14 at 3:17
















2












2








2







The company I work for processes bills, and I regularly have to check these bills for accuracy by comparing what was entered into the database vs. the actual bill image. Any time I find an error in processing, I must delete the bill from the database and send the image to a processor to re-enter the bill correctly. However, our system frequently encounters a glitch where if I try to upload the bill to the server that gets it to the processor, it says that a duplicate file exists. To bypass this issue I have to manually rename each image with a new name (we use rolls of control numbers to name our images).



I'd like to automate this process so that I can rename these files quickly in sequential order. I would like my batch file to have an input so that I can enter in the starting control number and it will rename each file in the directory in sequential order, starting with the control number I enter +1 to each additional file in the directory, for example it would go like this:



Batch file asks me starting control #. I enter in 6654821.





  • bill3210851.pdf renamed to 6654821


  • imagebill654.pdf renamed to 6654822


  • random_name.pdf renamed to 6654823


  • billerror.pdf renamed to 6654824


  • sendtomanual.pdf renamed to 6654825


The original name of the file is not at all relevant, they don't need to remain in any specific order as long as they are renamed sequentially.



So far what I have is this:



@echo OFF
title Batch Rename
color 5f

echo Hello! I'm the batch pdf renamer. Please enter the starting control number:
set /p start=

ren %userprofile%desktopneedrenamed*.pdf "%p%.pdf"

pause


Obviously this doesn't address renaming the files sequentially. I'm kind of stuck here.










share|improve this question















The company I work for processes bills, and I regularly have to check these bills for accuracy by comparing what was entered into the database vs. the actual bill image. Any time I find an error in processing, I must delete the bill from the database and send the image to a processor to re-enter the bill correctly. However, our system frequently encounters a glitch where if I try to upload the bill to the server that gets it to the processor, it says that a duplicate file exists. To bypass this issue I have to manually rename each image with a new name (we use rolls of control numbers to name our images).



I'd like to automate this process so that I can rename these files quickly in sequential order. I would like my batch file to have an input so that I can enter in the starting control number and it will rename each file in the directory in sequential order, starting with the control number I enter +1 to each additional file in the directory, for example it would go like this:



Batch file asks me starting control #. I enter in 6654821.





  • bill3210851.pdf renamed to 6654821


  • imagebill654.pdf renamed to 6654822


  • random_name.pdf renamed to 6654823


  • billerror.pdf renamed to 6654824


  • sendtomanual.pdf renamed to 6654825


The original name of the file is not at all relevant, they don't need to remain in any specific order as long as they are renamed sequentially.



So far what I have is this:



@echo OFF
title Batch Rename
color 5f

echo Hello! I'm the batch pdf renamer. Please enter the starting control number:
set /p start=

ren %userprofile%desktopneedrenamed*.pdf "%p%.pdf"

pause


Obviously this doesn't address renaming the files sequentially. I'm kind of stuck here.







windows batch-file batch-rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '14 at 16:19









Hennes

58.8k792141




58.8k792141










asked Nov 14 '14 at 1:58









Molly

1112




1112












  • How about using a 3rd party utility like Bulk rename tool? Follow this link and find out more. This tool will give the flexibility you need
    – Prasanna
    Nov 14 '14 at 3:17




















  • How about using a 3rd party utility like Bulk rename tool? Follow this link and find out more. This tool will give the flexibility you need
    – Prasanna
    Nov 14 '14 at 3:17


















How about using a 3rd party utility like Bulk rename tool? Follow this link and find out more. This tool will give the flexibility you need
– Prasanna
Nov 14 '14 at 3:17






How about using a 3rd party utility like Bulk rename tool? Follow this link and find out more. This tool will give the flexibility you need
– Prasanna
Nov 14 '14 at 3:17












4 Answers
4






active

oldest

votes


















0














I have written this batch script and put it on my Gist Repo. It requires Windows Scripting Host access. Here is the file:



@set @junk=1 /*
@echo off
cscript //nologo //E:jscript %0 %*
goto :eof
*/

var args=WScript.Arguments, shell=WScript.CreateObject("WScript.Shell"), bForced=false, nStartIndex=-1;

if(args.length==0||(args.length==1&&(bForced=args(0).toLowerCase()=='-f'||args(0).toLowerCase()=='--force'))) {
showHelp();
WScript.Echo("nERROR: You must provide a starting value to begin counting at.");
WScript.Quit(1);
}
if(args(0)=='-?'||args(0).toLowerCase()=='--help') {
showHelp();
WScript.Quit(0);
}
if(isNaN(nStartIndex=parseInt(args(bForced?1:0)))||nStartIndex<0) {
showHelp();
WScript.Echo(sprintf("nERROR: The value [%s] which was given for start index is invalid. It should be a positive integer.", nStartIndex));
WScript.Quit(2);
}

var fso=new ActiveXObject("Scripting.FileSystemObject");
var folder=fso.GetFolder(shell.CurrentDirectory);
var enFiles=new Enumerator(folder.Files);
var oFile, file_path, file_ext;

if(!bForced) {
for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
oFile=enFiles.item();
file_path=oFile.Name;
file_ext=getFileExt(file_path);
WScript.Echo(sprintf("Rename %s to %s", file_path, nCurDX+file_ext));
}
WScript.Echo("Type 'yes' to continue...");
if(WScript.StdIn.ReadLine().toLowerCase()!="yes") {
WScript.Echo("nAction cancelled by user.");
WScript.Quit(-1);
}
enFiles=new Enumerator(folder.Files);
}

for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
oFile=enFiles.item();
file_path=oFile.Name;
file_ext=getFileExt(file_path);
try {
oFile.Name = nCurDX+file_ext;
} catch(e) {
WScript.Echo(sprintf("nERROR: Cannot rename file:nt%snnReason: %sn", file_path, e.description));
WScript.Quit(3);
}
}

/////////////////////////////////
/////////// FUNCTIONS ///////////
/////////////////////////////////

function sprintf(format, etc) { var arg=arguments, i=1; return format.replace(/%((%)|s)/g, function (m) { return m[2] || arg[i++] }); }
function showHelp() {
WScript.Echo("------------------");
WScript.Echo("Numeric Rename All");
WScript.Echo("------------------");
WScript.Echo("");
WScript.Echo("This file will change the file title of all files is a directory");
WScript.Echo("so that they are all just an incremented number with the original");
WScript.Echo("extension still in place.");
WScript.Echo("");
WScript.Echo("Usage: NUMERIC_RENAME_ALL [ -? | --help ] [ -f | --force ] <start-index>");
WScript.Echo("");
WScript.Echo("t-?, --helptDisplay this help screen and exit.");
WScript.Echo("");
WScript.Echo("t-f, --forcetRename the files without prompting first.");
WScript.Echo("");
WScript.Echo("t<start-index>tThe value to begin counting at.");
WScript.Echo("");
WScript.Echo("This script takes one parameter and that is the number to begin at.");
WScript.Echo("For example (if called with 12345 as the parameter):");
WScript.Echo("");
WScript.Echo("ttest1.jpg becomes 12345.jpg");
WScript.Echo("ttest1.jpg.txt becomes 12346.jpg.txt");
WScript.Echo("ttest2.pdf becomes 12347.jpg");
WScript.Echo("");
WScript.Echo("Currently, there is no way to sort the items and they will be");
WScript.Echo("numbered according to the default file order for your system.");
WScript.Echo("");
WScript.Echo("----------------------");
}
function getFileExt(file_path) {
var dx=file_path.lastIndexOf("\"), ret;
if(dx==-1) dx=file_path.lastIndexOf("/");
ret=file_path.substring(dx+1);
return (dx=ret.indexOf("."))==-1?"":("."+ret.substring(dx+1));
}


Save it as NUMERIC_RENAME_ALL.BAT in a place easy to get to.



Doing this in a plain batch file would be difficult because cmd.exe fails at setting variable inside of loops. This is why we are using a BatchWSH Hybrid file to do this (this is declared in those first 5 lines of the script). There is probably a fairly easy way to do this is power shell too.






share|improve this answer























  • The Gist page has now been updated to support the use of regular expressions in order to select files to be renamed instead of always renaming them all.
    – krowe
    Nov 14 '14 at 7:25



















0














This has been tested in Windows 7 Professional. No error checking of any kind is performed. It renames the files in the current directory, so the script should be somewhere on your path.



@echo off
set /p start=Please enter the starting control number:

setlocal enableDelayedExpansion

for /r %%g in (*.pdf) do (call :RenameIt %%g)
goto :eof
goto :exit

:RenameIt
echo Renaming "%~nx1" to !start!%~x1
ren "%~nx1" !start!%~x1
set /a start+=1
goto :eof

:exit
exit /b


Hope that helps!






share|improve this answer





























    0














    @echo off
    setlocal enableextensions disabledelayedexpansion

    set /p "number=Please enter the starting control number:" || goto :eof

    for /f "delims=" %%a in ('dir /b *.pdf') do (
    setlocal enabledelayedexpansion
    for %%b in (!number!) do (
    endlocal
    echo ren "%%~fa" %%b
    )
    set /a "number+=1"
    )


    For each .pdf file in the current folder, rename the file to the current number and increase the number.



    Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes the ren command.






    share|improve this answer





























      0














      I found this question while looking for a means to sequentially rename all files in each subfolder recursively, but where I required the numbers to be padded with leading zeros, and also to be agnostic to the file extension. I found a solution here that led me to the final solution which is:



      @echo off
      setlocal enabledelayedexpansion

      for /d /r %%a in (%1*.*) do (
      set /a counter=0
      for %%b in ("%%a*.*") do (
      set /a counter += 1
      set zcounter=0000!counter!
      set source="%%~fb"
      set target="Receipt !zcounter:~-4!%%~xb"
      echo Renaming !source! to !target!
      ren !source! !target!
      )
      )


      Put the above in a batch file and call passing a directory as a parameter. The script pads with up to three leading zeros and prefixes with the word "Receipt", but change according to your requirements. This script will work fine also if you don't have subfolders to recurse into. And if you don't want leading zeros, just remove the set zcounter line, and replace zcounter:~-4 with counter.



      Hope this helps anyone stumbling across this page looking for something similar.






      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%2f839886%2frename-files-sequentially-from-input-using-batch-file%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        I have written this batch script and put it on my Gist Repo. It requires Windows Scripting Host access. Here is the file:



        @set @junk=1 /*
        @echo off
        cscript //nologo //E:jscript %0 %*
        goto :eof
        */

        var args=WScript.Arguments, shell=WScript.CreateObject("WScript.Shell"), bForced=false, nStartIndex=-1;

        if(args.length==0||(args.length==1&&(bForced=args(0).toLowerCase()=='-f'||args(0).toLowerCase()=='--force'))) {
        showHelp();
        WScript.Echo("nERROR: You must provide a starting value to begin counting at.");
        WScript.Quit(1);
        }
        if(args(0)=='-?'||args(0).toLowerCase()=='--help') {
        showHelp();
        WScript.Quit(0);
        }
        if(isNaN(nStartIndex=parseInt(args(bForced?1:0)))||nStartIndex<0) {
        showHelp();
        WScript.Echo(sprintf("nERROR: The value [%s] which was given for start index is invalid. It should be a positive integer.", nStartIndex));
        WScript.Quit(2);
        }

        var fso=new ActiveXObject("Scripting.FileSystemObject");
        var folder=fso.GetFolder(shell.CurrentDirectory);
        var enFiles=new Enumerator(folder.Files);
        var oFile, file_path, file_ext;

        if(!bForced) {
        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        WScript.Echo(sprintf("Rename %s to %s", file_path, nCurDX+file_ext));
        }
        WScript.Echo("Type 'yes' to continue...");
        if(WScript.StdIn.ReadLine().toLowerCase()!="yes") {
        WScript.Echo("nAction cancelled by user.");
        WScript.Quit(-1);
        }
        enFiles=new Enumerator(folder.Files);
        }

        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        try {
        oFile.Name = nCurDX+file_ext;
        } catch(e) {
        WScript.Echo(sprintf("nERROR: Cannot rename file:nt%snnReason: %sn", file_path, e.description));
        WScript.Quit(3);
        }
        }

        /////////////////////////////////
        /////////// FUNCTIONS ///////////
        /////////////////////////////////

        function sprintf(format, etc) { var arg=arguments, i=1; return format.replace(/%((%)|s)/g, function (m) { return m[2] || arg[i++] }); }
        function showHelp() {
        WScript.Echo("------------------");
        WScript.Echo("Numeric Rename All");
        WScript.Echo("------------------");
        WScript.Echo("");
        WScript.Echo("This file will change the file title of all files is a directory");
        WScript.Echo("so that they are all just an incremented number with the original");
        WScript.Echo("extension still in place.");
        WScript.Echo("");
        WScript.Echo("Usage: NUMERIC_RENAME_ALL [ -? | --help ] [ -f | --force ] <start-index>");
        WScript.Echo("");
        WScript.Echo("t-?, --helptDisplay this help screen and exit.");
        WScript.Echo("");
        WScript.Echo("t-f, --forcetRename the files without prompting first.");
        WScript.Echo("");
        WScript.Echo("t<start-index>tThe value to begin counting at.");
        WScript.Echo("");
        WScript.Echo("This script takes one parameter and that is the number to begin at.");
        WScript.Echo("For example (if called with 12345 as the parameter):");
        WScript.Echo("");
        WScript.Echo("ttest1.jpg becomes 12345.jpg");
        WScript.Echo("ttest1.jpg.txt becomes 12346.jpg.txt");
        WScript.Echo("ttest2.pdf becomes 12347.jpg");
        WScript.Echo("");
        WScript.Echo("Currently, there is no way to sort the items and they will be");
        WScript.Echo("numbered according to the default file order for your system.");
        WScript.Echo("");
        WScript.Echo("----------------------");
        }
        function getFileExt(file_path) {
        var dx=file_path.lastIndexOf("\"), ret;
        if(dx==-1) dx=file_path.lastIndexOf("/");
        ret=file_path.substring(dx+1);
        return (dx=ret.indexOf("."))==-1?"":("."+ret.substring(dx+1));
        }


        Save it as NUMERIC_RENAME_ALL.BAT in a place easy to get to.



        Doing this in a plain batch file would be difficult because cmd.exe fails at setting variable inside of loops. This is why we are using a BatchWSH Hybrid file to do this (this is declared in those first 5 lines of the script). There is probably a fairly easy way to do this is power shell too.






        share|improve this answer























        • The Gist page has now been updated to support the use of regular expressions in order to select files to be renamed instead of always renaming them all.
          – krowe
          Nov 14 '14 at 7:25
















        0














        I have written this batch script and put it on my Gist Repo. It requires Windows Scripting Host access. Here is the file:



        @set @junk=1 /*
        @echo off
        cscript //nologo //E:jscript %0 %*
        goto :eof
        */

        var args=WScript.Arguments, shell=WScript.CreateObject("WScript.Shell"), bForced=false, nStartIndex=-1;

        if(args.length==0||(args.length==1&&(bForced=args(0).toLowerCase()=='-f'||args(0).toLowerCase()=='--force'))) {
        showHelp();
        WScript.Echo("nERROR: You must provide a starting value to begin counting at.");
        WScript.Quit(1);
        }
        if(args(0)=='-?'||args(0).toLowerCase()=='--help') {
        showHelp();
        WScript.Quit(0);
        }
        if(isNaN(nStartIndex=parseInt(args(bForced?1:0)))||nStartIndex<0) {
        showHelp();
        WScript.Echo(sprintf("nERROR: The value [%s] which was given for start index is invalid. It should be a positive integer.", nStartIndex));
        WScript.Quit(2);
        }

        var fso=new ActiveXObject("Scripting.FileSystemObject");
        var folder=fso.GetFolder(shell.CurrentDirectory);
        var enFiles=new Enumerator(folder.Files);
        var oFile, file_path, file_ext;

        if(!bForced) {
        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        WScript.Echo(sprintf("Rename %s to %s", file_path, nCurDX+file_ext));
        }
        WScript.Echo("Type 'yes' to continue...");
        if(WScript.StdIn.ReadLine().toLowerCase()!="yes") {
        WScript.Echo("nAction cancelled by user.");
        WScript.Quit(-1);
        }
        enFiles=new Enumerator(folder.Files);
        }

        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        try {
        oFile.Name = nCurDX+file_ext;
        } catch(e) {
        WScript.Echo(sprintf("nERROR: Cannot rename file:nt%snnReason: %sn", file_path, e.description));
        WScript.Quit(3);
        }
        }

        /////////////////////////////////
        /////////// FUNCTIONS ///////////
        /////////////////////////////////

        function sprintf(format, etc) { var arg=arguments, i=1; return format.replace(/%((%)|s)/g, function (m) { return m[2] || arg[i++] }); }
        function showHelp() {
        WScript.Echo("------------------");
        WScript.Echo("Numeric Rename All");
        WScript.Echo("------------------");
        WScript.Echo("");
        WScript.Echo("This file will change the file title of all files is a directory");
        WScript.Echo("so that they are all just an incremented number with the original");
        WScript.Echo("extension still in place.");
        WScript.Echo("");
        WScript.Echo("Usage: NUMERIC_RENAME_ALL [ -? | --help ] [ -f | --force ] <start-index>");
        WScript.Echo("");
        WScript.Echo("t-?, --helptDisplay this help screen and exit.");
        WScript.Echo("");
        WScript.Echo("t-f, --forcetRename the files without prompting first.");
        WScript.Echo("");
        WScript.Echo("t<start-index>tThe value to begin counting at.");
        WScript.Echo("");
        WScript.Echo("This script takes one parameter and that is the number to begin at.");
        WScript.Echo("For example (if called with 12345 as the parameter):");
        WScript.Echo("");
        WScript.Echo("ttest1.jpg becomes 12345.jpg");
        WScript.Echo("ttest1.jpg.txt becomes 12346.jpg.txt");
        WScript.Echo("ttest2.pdf becomes 12347.jpg");
        WScript.Echo("");
        WScript.Echo("Currently, there is no way to sort the items and they will be");
        WScript.Echo("numbered according to the default file order for your system.");
        WScript.Echo("");
        WScript.Echo("----------------------");
        }
        function getFileExt(file_path) {
        var dx=file_path.lastIndexOf("\"), ret;
        if(dx==-1) dx=file_path.lastIndexOf("/");
        ret=file_path.substring(dx+1);
        return (dx=ret.indexOf("."))==-1?"":("."+ret.substring(dx+1));
        }


        Save it as NUMERIC_RENAME_ALL.BAT in a place easy to get to.



        Doing this in a plain batch file would be difficult because cmd.exe fails at setting variable inside of loops. This is why we are using a BatchWSH Hybrid file to do this (this is declared in those first 5 lines of the script). There is probably a fairly easy way to do this is power shell too.






        share|improve this answer























        • The Gist page has now been updated to support the use of regular expressions in order to select files to be renamed instead of always renaming them all.
          – krowe
          Nov 14 '14 at 7:25














        0












        0








        0






        I have written this batch script and put it on my Gist Repo. It requires Windows Scripting Host access. Here is the file:



        @set @junk=1 /*
        @echo off
        cscript //nologo //E:jscript %0 %*
        goto :eof
        */

        var args=WScript.Arguments, shell=WScript.CreateObject("WScript.Shell"), bForced=false, nStartIndex=-1;

        if(args.length==0||(args.length==1&&(bForced=args(0).toLowerCase()=='-f'||args(0).toLowerCase()=='--force'))) {
        showHelp();
        WScript.Echo("nERROR: You must provide a starting value to begin counting at.");
        WScript.Quit(1);
        }
        if(args(0)=='-?'||args(0).toLowerCase()=='--help') {
        showHelp();
        WScript.Quit(0);
        }
        if(isNaN(nStartIndex=parseInt(args(bForced?1:0)))||nStartIndex<0) {
        showHelp();
        WScript.Echo(sprintf("nERROR: The value [%s] which was given for start index is invalid. It should be a positive integer.", nStartIndex));
        WScript.Quit(2);
        }

        var fso=new ActiveXObject("Scripting.FileSystemObject");
        var folder=fso.GetFolder(shell.CurrentDirectory);
        var enFiles=new Enumerator(folder.Files);
        var oFile, file_path, file_ext;

        if(!bForced) {
        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        WScript.Echo(sprintf("Rename %s to %s", file_path, nCurDX+file_ext));
        }
        WScript.Echo("Type 'yes' to continue...");
        if(WScript.StdIn.ReadLine().toLowerCase()!="yes") {
        WScript.Echo("nAction cancelled by user.");
        WScript.Quit(-1);
        }
        enFiles=new Enumerator(folder.Files);
        }

        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        try {
        oFile.Name = nCurDX+file_ext;
        } catch(e) {
        WScript.Echo(sprintf("nERROR: Cannot rename file:nt%snnReason: %sn", file_path, e.description));
        WScript.Quit(3);
        }
        }

        /////////////////////////////////
        /////////// FUNCTIONS ///////////
        /////////////////////////////////

        function sprintf(format, etc) { var arg=arguments, i=1; return format.replace(/%((%)|s)/g, function (m) { return m[2] || arg[i++] }); }
        function showHelp() {
        WScript.Echo("------------------");
        WScript.Echo("Numeric Rename All");
        WScript.Echo("------------------");
        WScript.Echo("");
        WScript.Echo("This file will change the file title of all files is a directory");
        WScript.Echo("so that they are all just an incremented number with the original");
        WScript.Echo("extension still in place.");
        WScript.Echo("");
        WScript.Echo("Usage: NUMERIC_RENAME_ALL [ -? | --help ] [ -f | --force ] <start-index>");
        WScript.Echo("");
        WScript.Echo("t-?, --helptDisplay this help screen and exit.");
        WScript.Echo("");
        WScript.Echo("t-f, --forcetRename the files without prompting first.");
        WScript.Echo("");
        WScript.Echo("t<start-index>tThe value to begin counting at.");
        WScript.Echo("");
        WScript.Echo("This script takes one parameter and that is the number to begin at.");
        WScript.Echo("For example (if called with 12345 as the parameter):");
        WScript.Echo("");
        WScript.Echo("ttest1.jpg becomes 12345.jpg");
        WScript.Echo("ttest1.jpg.txt becomes 12346.jpg.txt");
        WScript.Echo("ttest2.pdf becomes 12347.jpg");
        WScript.Echo("");
        WScript.Echo("Currently, there is no way to sort the items and they will be");
        WScript.Echo("numbered according to the default file order for your system.");
        WScript.Echo("");
        WScript.Echo("----------------------");
        }
        function getFileExt(file_path) {
        var dx=file_path.lastIndexOf("\"), ret;
        if(dx==-1) dx=file_path.lastIndexOf("/");
        ret=file_path.substring(dx+1);
        return (dx=ret.indexOf("."))==-1?"":("."+ret.substring(dx+1));
        }


        Save it as NUMERIC_RENAME_ALL.BAT in a place easy to get to.



        Doing this in a plain batch file would be difficult because cmd.exe fails at setting variable inside of loops. This is why we are using a BatchWSH Hybrid file to do this (this is declared in those first 5 lines of the script). There is probably a fairly easy way to do this is power shell too.






        share|improve this answer














        I have written this batch script and put it on my Gist Repo. It requires Windows Scripting Host access. Here is the file:



        @set @junk=1 /*
        @echo off
        cscript //nologo //E:jscript %0 %*
        goto :eof
        */

        var args=WScript.Arguments, shell=WScript.CreateObject("WScript.Shell"), bForced=false, nStartIndex=-1;

        if(args.length==0||(args.length==1&&(bForced=args(0).toLowerCase()=='-f'||args(0).toLowerCase()=='--force'))) {
        showHelp();
        WScript.Echo("nERROR: You must provide a starting value to begin counting at.");
        WScript.Quit(1);
        }
        if(args(0)=='-?'||args(0).toLowerCase()=='--help') {
        showHelp();
        WScript.Quit(0);
        }
        if(isNaN(nStartIndex=parseInt(args(bForced?1:0)))||nStartIndex<0) {
        showHelp();
        WScript.Echo(sprintf("nERROR: The value [%s] which was given for start index is invalid. It should be a positive integer.", nStartIndex));
        WScript.Quit(2);
        }

        var fso=new ActiveXObject("Scripting.FileSystemObject");
        var folder=fso.GetFolder(shell.CurrentDirectory);
        var enFiles=new Enumerator(folder.Files);
        var oFile, file_path, file_ext;

        if(!bForced) {
        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        WScript.Echo(sprintf("Rename %s to %s", file_path, nCurDX+file_ext));
        }
        WScript.Echo("Type 'yes' to continue...");
        if(WScript.StdIn.ReadLine().toLowerCase()!="yes") {
        WScript.Echo("nAction cancelled by user.");
        WScript.Quit(-1);
        }
        enFiles=new Enumerator(folder.Files);
        }

        for(var nCurDX=nStartIndex; !enFiles.atEnd(); enFiles.moveNext(), nCurDX++) {
        oFile=enFiles.item();
        file_path=oFile.Name;
        file_ext=getFileExt(file_path);
        try {
        oFile.Name = nCurDX+file_ext;
        } catch(e) {
        WScript.Echo(sprintf("nERROR: Cannot rename file:nt%snnReason: %sn", file_path, e.description));
        WScript.Quit(3);
        }
        }

        /////////////////////////////////
        /////////// FUNCTIONS ///////////
        /////////////////////////////////

        function sprintf(format, etc) { var arg=arguments, i=1; return format.replace(/%((%)|s)/g, function (m) { return m[2] || arg[i++] }); }
        function showHelp() {
        WScript.Echo("------------------");
        WScript.Echo("Numeric Rename All");
        WScript.Echo("------------------");
        WScript.Echo("");
        WScript.Echo("This file will change the file title of all files is a directory");
        WScript.Echo("so that they are all just an incremented number with the original");
        WScript.Echo("extension still in place.");
        WScript.Echo("");
        WScript.Echo("Usage: NUMERIC_RENAME_ALL [ -? | --help ] [ -f | --force ] <start-index>");
        WScript.Echo("");
        WScript.Echo("t-?, --helptDisplay this help screen and exit.");
        WScript.Echo("");
        WScript.Echo("t-f, --forcetRename the files without prompting first.");
        WScript.Echo("");
        WScript.Echo("t<start-index>tThe value to begin counting at.");
        WScript.Echo("");
        WScript.Echo("This script takes one parameter and that is the number to begin at.");
        WScript.Echo("For example (if called with 12345 as the parameter):");
        WScript.Echo("");
        WScript.Echo("ttest1.jpg becomes 12345.jpg");
        WScript.Echo("ttest1.jpg.txt becomes 12346.jpg.txt");
        WScript.Echo("ttest2.pdf becomes 12347.jpg");
        WScript.Echo("");
        WScript.Echo("Currently, there is no way to sort the items and they will be");
        WScript.Echo("numbered according to the default file order for your system.");
        WScript.Echo("");
        WScript.Echo("----------------------");
        }
        function getFileExt(file_path) {
        var dx=file_path.lastIndexOf("\"), ret;
        if(dx==-1) dx=file_path.lastIndexOf("/");
        ret=file_path.substring(dx+1);
        return (dx=ret.indexOf("."))==-1?"":("."+ret.substring(dx+1));
        }


        Save it as NUMERIC_RENAME_ALL.BAT in a place easy to get to.



        Doing this in a plain batch file would be difficult because cmd.exe fails at setting variable inside of loops. This is why we are using a BatchWSH Hybrid file to do this (this is declared in those first 5 lines of the script). There is probably a fairly easy way to do this is power shell too.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '14 at 6:02

























        answered Nov 14 '14 at 5:47









        krowe

        4,83111428




        4,83111428












        • The Gist page has now been updated to support the use of regular expressions in order to select files to be renamed instead of always renaming them all.
          – krowe
          Nov 14 '14 at 7:25


















        • The Gist page has now been updated to support the use of regular expressions in order to select files to be renamed instead of always renaming them all.
          – krowe
          Nov 14 '14 at 7:25
















        The Gist page has now been updated to support the use of regular expressions in order to select files to be renamed instead of always renaming them all.
        – krowe
        Nov 14 '14 at 7:25




        The Gist page has now been updated to support the use of regular expressions in order to select files to be renamed instead of always renaming them all.
        – krowe
        Nov 14 '14 at 7:25













        0














        This has been tested in Windows 7 Professional. No error checking of any kind is performed. It renames the files in the current directory, so the script should be somewhere on your path.



        @echo off
        set /p start=Please enter the starting control number:

        setlocal enableDelayedExpansion

        for /r %%g in (*.pdf) do (call :RenameIt %%g)
        goto :eof
        goto :exit

        :RenameIt
        echo Renaming "%~nx1" to !start!%~x1
        ren "%~nx1" !start!%~x1
        set /a start+=1
        goto :eof

        :exit
        exit /b


        Hope that helps!






        share|improve this answer


























          0














          This has been tested in Windows 7 Professional. No error checking of any kind is performed. It renames the files in the current directory, so the script should be somewhere on your path.



          @echo off
          set /p start=Please enter the starting control number:

          setlocal enableDelayedExpansion

          for /r %%g in (*.pdf) do (call :RenameIt %%g)
          goto :eof
          goto :exit

          :RenameIt
          echo Renaming "%~nx1" to !start!%~x1
          ren "%~nx1" !start!%~x1
          set /a start+=1
          goto :eof

          :exit
          exit /b


          Hope that helps!






          share|improve this answer
























            0












            0








            0






            This has been tested in Windows 7 Professional. No error checking of any kind is performed. It renames the files in the current directory, so the script should be somewhere on your path.



            @echo off
            set /p start=Please enter the starting control number:

            setlocal enableDelayedExpansion

            for /r %%g in (*.pdf) do (call :RenameIt %%g)
            goto :eof
            goto :exit

            :RenameIt
            echo Renaming "%~nx1" to !start!%~x1
            ren "%~nx1" !start!%~x1
            set /a start+=1
            goto :eof

            :exit
            exit /b


            Hope that helps!






            share|improve this answer












            This has been tested in Windows 7 Professional. No error checking of any kind is performed. It renames the files in the current directory, so the script should be somewhere on your path.



            @echo off
            set /p start=Please enter the starting control number:

            setlocal enableDelayedExpansion

            for /r %%g in (*.pdf) do (call :RenameIt %%g)
            goto :eof
            goto :exit

            :RenameIt
            echo Renaming "%~nx1" to !start!%~x1
            ren "%~nx1" !start!%~x1
            set /a start+=1
            goto :eof

            :exit
            exit /b


            Hope that helps!







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '14 at 0:09









            JSanchez

            1,51289




            1,51289























                0














                @echo off
                setlocal enableextensions disabledelayedexpansion

                set /p "number=Please enter the starting control number:" || goto :eof

                for /f "delims=" %%a in ('dir /b *.pdf') do (
                setlocal enabledelayedexpansion
                for %%b in (!number!) do (
                endlocal
                echo ren "%%~fa" %%b
                )
                set /a "number+=1"
                )


                For each .pdf file in the current folder, rename the file to the current number and increase the number.



                Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes the ren command.






                share|improve this answer


























                  0














                  @echo off
                  setlocal enableextensions disabledelayedexpansion

                  set /p "number=Please enter the starting control number:" || goto :eof

                  for /f "delims=" %%a in ('dir /b *.pdf') do (
                  setlocal enabledelayedexpansion
                  for %%b in (!number!) do (
                  endlocal
                  echo ren "%%~fa" %%b
                  )
                  set /a "number+=1"
                  )


                  For each .pdf file in the current folder, rename the file to the current number and increase the number.



                  Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes the ren command.






                  share|improve this answer
























                    0












                    0








                    0






                    @echo off
                    setlocal enableextensions disabledelayedexpansion

                    set /p "number=Please enter the starting control number:" || goto :eof

                    for /f "delims=" %%a in ('dir /b *.pdf') do (
                    setlocal enabledelayedexpansion
                    for %%b in (!number!) do (
                    endlocal
                    echo ren "%%~fa" %%b
                    )
                    set /a "number+=1"
                    )


                    For each .pdf file in the current folder, rename the file to the current number and increase the number.



                    Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes the ren command.






                    share|improve this answer












                    @echo off
                    setlocal enableextensions disabledelayedexpansion

                    set /p "number=Please enter the starting control number:" || goto :eof

                    for /f "delims=" %%a in ('dir /b *.pdf') do (
                    setlocal enabledelayedexpansion
                    for %%b in (!number!) do (
                    endlocal
                    echo ren "%%~fa" %%b
                    )
                    set /a "number+=1"
                    )


                    For each .pdf file in the current folder, rename the file to the current number and increase the number.



                    Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes the ren command.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '14 at 16:15









                    MC ND

                    1,141612




                    1,141612























                        0














                        I found this question while looking for a means to sequentially rename all files in each subfolder recursively, but where I required the numbers to be padded with leading zeros, and also to be agnostic to the file extension. I found a solution here that led me to the final solution which is:



                        @echo off
                        setlocal enabledelayedexpansion

                        for /d /r %%a in (%1*.*) do (
                        set /a counter=0
                        for %%b in ("%%a*.*") do (
                        set /a counter += 1
                        set zcounter=0000!counter!
                        set source="%%~fb"
                        set target="Receipt !zcounter:~-4!%%~xb"
                        echo Renaming !source! to !target!
                        ren !source! !target!
                        )
                        )


                        Put the above in a batch file and call passing a directory as a parameter. The script pads with up to three leading zeros and prefixes with the word "Receipt", but change according to your requirements. This script will work fine also if you don't have subfolders to recurse into. And if you don't want leading zeros, just remove the set zcounter line, and replace zcounter:~-4 with counter.



                        Hope this helps anyone stumbling across this page looking for something similar.






                        share|improve this answer




























                          0














                          I found this question while looking for a means to sequentially rename all files in each subfolder recursively, but where I required the numbers to be padded with leading zeros, and also to be agnostic to the file extension. I found a solution here that led me to the final solution which is:



                          @echo off
                          setlocal enabledelayedexpansion

                          for /d /r %%a in (%1*.*) do (
                          set /a counter=0
                          for %%b in ("%%a*.*") do (
                          set /a counter += 1
                          set zcounter=0000!counter!
                          set source="%%~fb"
                          set target="Receipt !zcounter:~-4!%%~xb"
                          echo Renaming !source! to !target!
                          ren !source! !target!
                          )
                          )


                          Put the above in a batch file and call passing a directory as a parameter. The script pads with up to three leading zeros and prefixes with the word "Receipt", but change according to your requirements. This script will work fine also if you don't have subfolders to recurse into. And if you don't want leading zeros, just remove the set zcounter line, and replace zcounter:~-4 with counter.



                          Hope this helps anyone stumbling across this page looking for something similar.






                          share|improve this answer


























                            0












                            0








                            0






                            I found this question while looking for a means to sequentially rename all files in each subfolder recursively, but where I required the numbers to be padded with leading zeros, and also to be agnostic to the file extension. I found a solution here that led me to the final solution which is:



                            @echo off
                            setlocal enabledelayedexpansion

                            for /d /r %%a in (%1*.*) do (
                            set /a counter=0
                            for %%b in ("%%a*.*") do (
                            set /a counter += 1
                            set zcounter=0000!counter!
                            set source="%%~fb"
                            set target="Receipt !zcounter:~-4!%%~xb"
                            echo Renaming !source! to !target!
                            ren !source! !target!
                            )
                            )


                            Put the above in a batch file and call passing a directory as a parameter. The script pads with up to three leading zeros and prefixes with the word "Receipt", but change according to your requirements. This script will work fine also if you don't have subfolders to recurse into. And if you don't want leading zeros, just remove the set zcounter line, and replace zcounter:~-4 with counter.



                            Hope this helps anyone stumbling across this page looking for something similar.






                            share|improve this answer














                            I found this question while looking for a means to sequentially rename all files in each subfolder recursively, but where I required the numbers to be padded with leading zeros, and also to be agnostic to the file extension. I found a solution here that led me to the final solution which is:



                            @echo off
                            setlocal enabledelayedexpansion

                            for /d /r %%a in (%1*.*) do (
                            set /a counter=0
                            for %%b in ("%%a*.*") do (
                            set /a counter += 1
                            set zcounter=0000!counter!
                            set source="%%~fb"
                            set target="Receipt !zcounter:~-4!%%~xb"
                            echo Renaming !source! to !target!
                            ren !source! !target!
                            )
                            )


                            Put the above in a batch file and call passing a directory as a parameter. The script pads with up to three leading zeros and prefixes with the word "Receipt", but change according to your requirements. This script will work fine also if you don't have subfolders to recurse into. And if you don't want leading zeros, just remove the set zcounter line, and replace zcounter:~-4 with counter.



                            Hope this helps anyone stumbling across this page looking for something similar.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 23 '17 at 12:41









                            Community

                            1




                            1










                            answered Apr 16 '17 at 15:05









                            Neo

                            11116




                            11116






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f839886%2frename-files-sequentially-from-input-using-batch-file%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

                                Plaza Victoria

                                In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                                How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...