Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE  (Read 10774 times)

0 Members and 1 Guest are viewing this topic.

BRIANH

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows XP
    RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
    « on: September 29, 2008, 03:00:11 PM »
    I have reviewed several previous posted Q's and haven't seen anything which seems to fit my issue.

    I have files named     --  FILE_1.TXT,    FILE_2.TXT   FILE_3.TXT

    All files are in a single directory....

    I'd like to rename all files as  FILE_1-current date with no '/'s.TXT
                                                  FILE_2-current date with no '/'s.TXT
                                                  FILE_3-current date with no '/'s.TXT

    I've tried  ren  D:\TEST\*.TXT   *-%date:/=%.txt
    I end up with  --  FILE_1.txt-20080929.txt

    It repeats the .txt extention which is not  what I want...

    Hopefully someone will have a suggestion...
    thanks

    macdad-



      Expert

      Thanked: 40
      Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
      « Reply #1 on: September 29, 2008, 03:37:18 PM »
      well first you have to know that two files cant have the same filename. but i have a solution for you right here. place the batch program in the same directory as the files and run it, as you can see i've added a number after the am or pm(depending upon the time) that separates the files so you wont have the same filename error. save the attachment as a BAT file.

      hope this works
      ,Nick

      [Saving space - attachment deleted by admin]
      If you dont know DOS, you dont know Windows...

      Thats why Bill Gates created the Windows NT Family.

      BRIANH

        Topic Starter


        Rookie

        • Experience: Familiar
        • OS: Windows XP
        Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
        « Reply #2 on: September 29, 2008, 03:44:31 PM »
        thanks for your reply.. however I'm not entirely clear re: your solution.. I don't see it...

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
        « Reply #3 on: September 29, 2008, 04:24:49 PM »
        Renaming files using wildcards can lead to some very bizarre filenames. Might be easier to process one file at a time:

        Code: [Select]
        for  %%V in (*.txt) do ren  "%%V" "%%~nV_%date:/=%.txt"

         8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        BRIANH

          Topic Starter


          Rookie

          • Experience: Familiar
          • OS: Windows XP
          Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
          « Reply #4 on: September 30, 2008, 08:37:11 AM »

          tested this on my laptop then ran it on our server:

              for  %%V in (*.txt) do ren  "%%V" "%%~nV-%date:/=%.txt"

          on my laptop I ended up with what I was targeting:

          anomlog.1a28-09302008.txt



          on the server I ended up with something different due no doubt to how the DATE is formatted..
          imagine my surprise.....

          anomlog.1a28-Tue 09302008.txt


          Then attempted to remove the 'TUE ' from the filename and have not been successful..

          ran this test bat file

          CD D:\TEST

          REM @echo off
          REM setlocal enabledelayedexpansion
          for /f %%F in (*.TXT) do (
             set oldname=%%F
             set newname=!oldname!
             set newname=!newname:'TUE '=!
             ren !oldname! !newname!
             )


          I ended up with this:


          D:\>CD D:\TEST

          D:\TEST>REM @echo off

          D:\TEST>REM setlocal enabledelayedexpan

          D:\TEST>for /F %F in (*.TXT) do (
          set oldname=%F
           set newname=!oldname:'TUE '=!
           ren !oldname! !newname!
          )
          The system cannot find the file *.TXT.

          D:\TEST>pause
          Press any key to continue . . .


          So, as always... anyone with a suggestion or two.. if you'll take the time to pass them along to me.. I'll appreciate your time and effort...

          Dias de verano

          • Guest
          Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
          « Reply #5 on: September 30, 2008, 10:52:41 AM »
          Please answer a couple of questions:

          you have filenames in the format:

          filename.extension

          and you want them to be renamed to

          filename-date.extension

          where date is todays date and extension is always .txt

          is that right?

          It sounds like your system date format settings are USA style, that is today is shown as "Tue 09/30/2008"

          Is that right?

          « Last Edit: September 30, 2008, 12:29:11 PM by Dias de verano »

          macdad-



            Expert

            Thanked: 40
            Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
            « Reply #6 on: September 30, 2008, 12:41:56 PM »
            thanks for your reply.. however I'm not entirely clear re: your solution.. I don't see it...

            its the timestamp txt file i attached. save it as a BAT file
            If you dont know DOS, you dont know Windows...

            Thats why Bill Gates created the Windows NT Family.

            BRIANH

              Topic Starter


              Rookie

              • Experience: Familiar
              • OS: Windows XP
              Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
              « Reply #7 on: September 30, 2008, 01:00:25 PM »
              where date is todays date and extension is always .txt

              is that right?  >>>>>>>>>>>>>>> correct

              It sounds like your system date format settings are USA style, that is today is shown as "Tue 09/30/2008"

              Is that right? >>>>>>>>>>>>>>> according to what I see,  correct.

              Dias de verano

              • Guest
              Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
              « Reply #8 on: September 30, 2008, 01:01:45 PM »
              OK here's a quick hack I cooked up

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              REM get MM, DD and YYYY independent of system date format settings

              echo Wscript.echo ^(DatePart^("YYYY", Date^)^)>DateYear.vbs
              echo Wscript.echo ^(DatePart^("M", Date^)^)>DateMonth.vbs
              echo Wscript.echo ^(DatePart^("D", Date^)^)>DateDay.vbs

              for /f %%A in ('DateYear.vbs //nologo') do set /a yyyy=%%A
              for /f %%A in ('DateMonth.vbs //nologo') do set /a mm=%%A
              for /f %%A in ('DateDay.vbs //nologo') do set /a dd=%%A

              del DateYear.vbs
              del DateMonth.vbs
              del DateDay.vbs

              if %mm% LSS 10 set mm=0%mm%
              if %dd% LSS 10 set dd=0%dd%

              set DateString=%mm%%dd%%yyyy%

              echo %DateString%

              REM remove ECHO when happy
              for /f "delims==" %%A in ('dir /b *.txt') do (
                      set oldname=%%A
                      set newname=%%~nA-%DateString%.txt
                      ECHO ren "!oldname!" "!newname!"
              )

              Dias de verano

              • Guest
              Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
              « Reply #9 on: September 30, 2008, 02:24:10 PM »
              I just realised, if you leave the renamed files where they are, the next time you run the batch (e.g. the enxt or a later day) then they will get caught and processed because they still have a .txt extension, so why not make a sub folder named for the date and move them there?

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion

              REM get MM, DD and YYYY independent of system date format settings
              echo Wscript.echo ^(DatePart^("YYYY", Date^)^)>DateYear.vbs
              echo Wscript.echo ^(DatePart^("M", Date^)^)>DateMonth.vbs
              echo Wscript.echo ^(DatePart^("D", Date^)^)>DateDay.vbs
              for /f %%A in ('DateYear.vbs //nologo') do set /a yyyy=%%A
              for /f %%A in ('DateMonth.vbs //nologo') do set /a mm=%%A
              for /f %%A in ('DateDay.vbs //nologo') do set /a dd=%%A
              del DateYear.vbs
              del DateMonth.vbs
              del DateDay.vbs

              REM add leading zero to single digit numbers
              if %mm% LSS 10 set mm=0%mm%
              if %dd% LSS 10 set dd=0%dd%

              REM create date string in desired format
              set DateString=%mm%%dd%%yyyy%

              REM use date string for folder name
              set DirName=%DateString%

              MEM In various lines below,
              REM Remove ECHO *** when you are  happy

              REM create folder if it does not yet exist
              if not exist %DirName% (
                      echo Creating folder %DirName%
                      ECHO *** MD %DirName%
                      )

              REM if there are any text files in this folder
              if exist *.txt (
                   Echo Found text files to process
                   REM get their names and process in this loop which follows
              for /f "delims==" %%A in ('dir /b *.txt') do (
                      REM store filename
                      set oldname=%%A
                      REM create new name
                      set newname=%%~nA-%DateString%.txt
                      REM rename file
                      echo Renaming !oldname! to !newname!
                           ECHO *** ren "!oldname!" "!newname!"
                      REM if file does not exist in folder move it there
                      if not exist "%DirName%\!newname!" (
                              echo Moving !newname! to %DirName%
                              ECHO *** move "!newname!" %DirName%
                              )
              )
              )



              BRIANH

                Topic Starter


                Rookie

                • Experience: Familiar
                • OS: Windows XP
                Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
                « Reply #10 on: October 01, 2008, 11:59:20 AM »
                Macdad, Sidewinder, Dias de verano......

                To all... thanks for your help and your time... I have something now which does
                what I want.. files have the current date to their name and the .txt extension is
                maintained....... And, during the process I've learned somethings..still a raw beginner but somewhat more familiar with somethings..

                Strictly as a sidebar observation, I noticed that when I open a DOS window and run the bat file, I get a running status of what the move command is doing. When
                I attempted to force a non-display, I used   -   move infile outfile > &. This worked however there appears to be something else going on as well. I have a pause command following the move command and it doesn't seem to be executed...

                I could direct the move command output to a file but this would be a file which would have little importance and I'd probably want to delete it.

                I've tried a couple of things but not knowing everything that the   > & is setting up,  I've not had any success...

                Not super important but I found it curious...

                BC_Programmer


                  Mastermind
                • Typing is no substitute for thinking.
                • Thanked: 1140
                  • Yes
                  • Yes
                  • BC-Programming.com
                • Certifications: List
                • Computer: Specs
                • Experience: Beginner
                • OS: Windows 11
                Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
                « Reply #11 on: October 01, 2008, 12:08:34 PM »
                redirect to the NUL device- as in

                Code: [Select]

                Move infile outfile > NUL


                I recall being called out on this one because of filesystem differences... I'm pretty sure that was Dias at some point. Feel free to call me out again!
                I was trying to dereference Null Pointers before it was cool.

                BRIANH

                  Topic Starter


                  Rookie

                  • Experience: Familiar
                  • OS: Windows XP
                  Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
                  « Reply #12 on: October 01, 2008, 12:12:13 PM »
                  BC_Programmer...

                  thanks.. managed to flash read several posts and found the > nul

                  thanks again for your help...

                  Dias de verano

                  • Guest
                  Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
                  « Reply #13 on: October 01, 2008, 12:13:18 PM »
                  The only NUL thing to do with filesystem differences that I can recall is using the existence of foldername\NUL as a test for the existence of foldername, which does not always work reliably in NTFS.


                  BC_Programmer


                    Mastermind
                  • Typing is no substitute for thinking.
                  • Thanked: 1140
                    • Yes
                    • Yes
                    • BC-Programming.com
                  • Certifications: List
                  • Computer: Specs
                  • Experience: Beginner
                  • OS: Windows 11
                  Re: RENAME FILES IN ONE DIRECTORY USING A BATCH FILE
                  « Reply #14 on: October 01, 2008, 12:18:50 PM »
                  The only NUL thing to do with filesystem differences that I can recall is using the existence of foldername\NUL as a test for the existence of foldername, which does not always work reliably in NTFS.

                  All I remember is I got called out for something to do with NUL- I recall now it was some fellow trying to determine the existence of a folder.

                  It's good to know I'm not always wrong.
                  I was trying to dereference Null Pointers before it was cool.