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

Author Topic: help with a .bat file  (Read 10032 times)

0 Members and 1 Guest are viewing this topic.

xfusion

    Topic Starter


    Rookie

    help with a .bat file
    « on: December 08, 2009, 02:30:31 PM »
    ok so i need to write a .bat file that will when placed in a folder, enter every folder in that folder, run this

    ren *_std *_std.mpg

    then also, if any of the file names start with 2,3,4,5,6,7,8,9 it will add a 0 before each of them, IE

    2-filename.mpg (they are all mpg's)  -> rename to -> 02-filename.mpg

    was wondering if anyone could help me create this, any help is appreciated, thanks

    Two-eyes



      Intermediate
    • Thanked: 4
      Re: help with a .bat file
      « Reply #1 on: December 09, 2009, 01:20:16 AM »
      Hi there.  What have you got so far?  Any code? More detailed algorithm?

      I suggest:
      -The dir command
      -The for command <--what out when you're using this
      -You might also use the for command to add the 0, if the number is always before a particular character (eg. '-').

      We will help you further if you show that you tried hard ;).

      Hope this helps

      Two-Eyes %
      Quote
      I believe the bushes in my yard will BURN before God picks up a PC to send a message


      gh0std0g74



        Apprentice

        Thanked: 37
        Re: help with a .bat file
        « Reply #2 on: December 09, 2009, 01:50:03 AM »
        Code: [Select]
        Set objFS=CreateObject("Scripting.FileSystemObject")
        strFolder="C:\test"
        Set objFolder = objFS.GetFolder(strFolder)
        For Each strFile In objFolder.Files
        strFileName = strFile.Name
        If objFs.GetExtensionName(strFileName) = "mpg" Then
        c=Mid(strFileName,1,1)
        If c >=2 Or c<=9 Then
        strNewFileName = "0"&strFileName
        WScript.Echo strNewFileName
        strFile.Name = strNewFileName
        End If
        End If
        Next
        save as change.vbs and one command line
        Code: [Select]
        c:\test> cscript //nologo change.vbs

        xfusion

          Topic Starter


          Rookie

          Re: help with a .bat file
          « Reply #3 on: December 09, 2009, 10:06:06 AM »
          all i have so far is

          ren *_std *_std.mpg

          i'm use to c++ so i'm not quite sure on -how- to completely accomplish, mostly on the searching through files aspect, i know i'll need a setup like
          go into directory
          ren *_std *_std.mpg
          if(starts with 2- add 0 to beginning of filename)
          if(starts with 3- add 0 to beginning of filename)
          ect ect
          go to next directory, repeat until it has gone through each directory (note that there is never definite amount of directory's in the folder, vary's between 15-30 most of the time)

          i'm just not sure on how exactly to code it



          billrich



            Rookie

            Thanked: 1
            Re: help with a .bat file
            « Reply #4 on: December 09, 2009, 11:20:00 AM »
            Mostly on the searching through files aspect, I know I'll need a setup like
            go into directory
            ren *_std
            Go to next directory, repeat until it has gone through each directory (note that there is never definite amount of directory's in the folder, vary's between 15-30 most of the time)



            C:\batch>type  fus2.bat
            REM  Goto root directory and search all subfolders with
            REM  dir /s en *_std*

            REM  Example:

            Code: [Select]
            @echo off

            E:

            dir /s toy*.txt  >   e:\std.txt

            cd e:\

            copy  e:\std.txt  c:\batch\std.txt


            for /f "tokens=5 delims= "  %%a in (c:\batch\std.txt) do (echo %%a )


            for /f "tokens=5 delims= " %%a in (c:\batch\std.txt) do (
            echo %%a.mpg >> c:\batch\ans.txt
            )

            C:
            echo Type  c:\batch\ans.txt
            echo.
            type  c:\batch\ans.txt

            del  c:\batch\ans.txt


            C:\batch> fus2.bat

            C:\batch>REM  Goto root directory and search all subfolders with

            C:\batch>REM  dir /s en *_std*


            TOY.TXT
            TOY2.TXT
            TOY3.TXT
            TOY4.TXT
            TOY41.TXT
            TOY5.TXT
            TOY52.TXT
            toy6.txt
            TOY62.TXT
            TOY7.TXT

            Type  c:\batch\ans.txt


            TOY.TXT.mpg
            TOY2.TXT.mpg
            TOY3.TXT.mpg
            TOY4.TXT.mpg
            TOY41.TXT.mpg
            TOY5.TXT.mpg
            TOY52.TXT.mpg
            toy6.txt.mpg
            TOY62.TXT.mpg
            TOY7.TXT.mpg

            C:\batch>

            Salmon Trout

            • Guest
            Re: help with a .bat file
            « Reply #5 on: December 09, 2009, 11:47:38 AM »
            so how does that answer the question, bill, Joan, Bill, and Mike? (Which one am I speaking to today?)


            xfusion

              Topic Starter


              Rookie

              Re: help with a .bat file
              « Reply #6 on: December 09, 2009, 11:58:12 AM »
              that just adds .mpg to all the files in the folder, i already have that working perfectly with

              ren *_std *_std.mpg

              (this only renames the files i need to rename because along with the mpg's in the folder, there's also .idx files that should not be renamed, along with a xml document that should not be renamed)
              its just that currently i have to copy my .bat into every sub folder and run it

              Salmon Trout

              • Guest
              Re: help with a .bat file
              « Reply #7 on: December 09, 2009, 12:04:40 PM »
              xfusion, billrich is a person with mental health issues who has been banned under several different names, but he keeps coming back, and the mods won't do anything about it. Please just ignore him.


              xfusion

                Topic Starter


                Rookie

                Re: help with a .bat file
                « Reply #8 on: December 09, 2009, 12:10:42 PM »
                i was kinda confused on why he sent me that last night as a message...

                billrich



                  Rookie

                  Thanked: 1
                  Re: help with a .bat file
                  « Reply #9 on: December 09, 2009, 12:19:52 PM »
                  i was kinda confused on why he sent me that last night as a message...

                  Please, xfusion,  post the solution that worked for you.  The solution will help many people.

                  Salmon Trout

                  • Guest
                  Re: help with a .bat file
                  « Reply #10 on: December 09, 2009, 12:22:59 PM »
                  i was kinda confused on why he sent me that last night as a message...

                  He has sent me a message or two. A sad case.


                  billrich



                    Rookie

                    Thanked: 1
                    Re: help with a .bat file
                    « Reply #11 on: December 09, 2009, 01:32:55 PM »
                    "I was kinda confused on why he sent me that last night as a message."

                    Negative messages should be sent as a message and not posted.

                    Indeed, negative messages should not be used by anyone.

                    Good luck with your project. I hope your rename process works. 

                    Salmon Trout

                    • Guest
                    Re: help with a .bat file
                    « Reply #12 on: December 09, 2009, 02:02:01 PM »
                    Indeed, negative messages should not be used by anyone.

                    Especially that one you sent me about my mother. That shameful message.

                    xfusion

                      Topic Starter


                      Rookie

                      Re: help with a .bat file
                      « Reply #13 on: December 09, 2009, 02:26:17 PM »
                      getting back on topic...

                      Two-eyes



                        Intermediate
                      • Thanked: 4
                        Re: help with a .bat file
                        « Reply #14 on: December 09, 2009, 03:25:05 PM »
                        I know I will be killed about this:  Salmon Trout and billrich have a...."interesting" relationship.  Don't think they did this for the first time....just a heads up.

                        getting back on topic...
                        Yes, let's.

                        The "dir /s" command will list every file in the directory the command is run in and in every sub-directory, sub-sub-directory etc (I think that's why it's an "s"), as you want it.
                        The "dir /b /s" will only list the names of the files. (see the difference the "b" switch does by trying the two different commands [without the "s" switch]).

                        Redirecting that output to a txt file works like this: "dir /b /s > myText.txt".

                        Then check out the for command.  It is too long and complicated to explain quickly, but you can extract the info you need for now.  What I think you need is "for /f %%x in (myText.txt) do ( ren %x% %x%.mpg)".  The renaming part might not work.  This might be because of "delayed expansion"...another long-ish topic that you can find a lot of information about :).

                        As for the 2-9 thing, if the number will always be before a hyphen('-') you can use the for command with the options "tokens=1 delims=-", then check (using an if statement) if it's between 2 and 9, and if so, add the '0'.

                        There is a lot of documentation out there that you can use to bring together this project of yours, go look for it ;D.

                        Hope this helps
                        Two-Eyes %

                        PS. do you strictly need a batch file solution?  ghostdog is an experiences vbs programmer and most probably...(99.99% ;D)... his script is correct....
                        Quote
                        I believe the bushes in my yard will BURN before God picks up a PC to send a message