Computer Hope

Microsoft => Microsoft DOS => Topic started by: xfusion on December 08, 2009, 02:30:31 PM

Title: help with a .bat file
Post by: xfusion 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
Title: Re: help with a .bat file
Post by: Two-eyes 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 (http://www.computerhope.com/dirhlp.htm) command
-The for (http://www.computerhope.com/forhlp.htm) 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 %
Title: Re: help with a .bat file
Post by: gh0std0g74 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
Title: Re: help with a .bat file
Post by: xfusion 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


Title: Re: help with a .bat file
Post by: billrich 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>
Title: Re: help with a .bat file
Post by: Salmon Trout 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?)

Title: Re: help with a .bat file
Post by: xfusion 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
Title: Re: help with a .bat file
Post by: Salmon Trout 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.

Title: Re: help with a .bat file
Post by: xfusion on December 09, 2009, 12:10:42 PM
i was kinda confused on why he sent me that last night as a message...
Title: Re: help with a .bat file
Post by: billrich 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.
Title: Re: help with a .bat file
Post by: Salmon Trout 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.

Title: Re: help with a .bat file
Post by: billrich 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. 
Title: Re: help with a .bat file
Post by: Salmon Trout 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.
Title: Re: help with a .bat file
Post by: xfusion on December 09, 2009, 02:26:17 PM
getting back on topic...
Title: Re: help with a .bat file
Post by: Two-eyes 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....
Title: Re: help with a .bat file
Post by: xfusion on December 09, 2009, 04:08:57 PM
thanks a lot i really appreciate the steering in the correct direction, i think if i only do the
"dir > mytext.txt" it will give me all the information i need, since there will never be any sub-sub directories and i only need the list of the directories in the main folder, so i will give that a try, that coupled with the for command should give me the enter directory, run "ren *_std *_std.mpg" exit go to next directory, as for the adding 0, all of the the files will ALWAYS be named "2-, 3-, 4-, 5-, 6-, 7-, 8-, 9-, 10-, 11-, ect.." the - always followed by random letters and only the 2-9 need the zero added to it (to make them alphabetical in the folder)

i attached a few pictures to help visually display what needs to be done (a before and after) and a overview of the main folder (where it will be run)

as for the vbs i'm not exactly sure on how to run it, i saved the one part as vbs and got stuck, i haven't had time to look further into it, and yes it can be a vbs, it can be anything honestly as long as the job gets done  :P

i will work on making the .bat and see how it goes  ;D

[Saving space, attachment deleted by admin]
Title: Re: help with a .bat file
Post by: xfusion on December 09, 2009, 04:10:16 PM
btw in the after picture, the little purple/orange.. things are media files, means they have been renamed .mpg
Title: Re: help with a .bat file
Post by: The_Game_Bane on December 11, 2009, 09:21:48 AM
i think i cud solve the 1st query

FOR /R %%i in (*_std) do ren *_std.mpg %%i
Title: Re: help with a .bat file
Post by: Salmon Trout on December 11, 2009, 09:29:43 AM
i think i cud solve the 1st query

FOR /R %%i in (*_std) do ren *_std.mpg %%i

I think you got that backwards
Title: Re: help with a .bat file
Post by: xfusion on December 11, 2009, 11:55:41 AM
i have the renaming and the adding 0 done

ren *_std *_std.mpg
ren 2-* 02-*
ren 3-* 03-*
ren 4-* 04-*
ren 5-* 05-*
ren 6-* 06-*
ren 7-* 07-*
ren 8-* 08-*
ren 9-* 09-*


that works perfectly, all i need now is for it to go into every directory in a folder and run that so i only have to run it once instead of 15-20 times, once in each folder
Title: Re: help with a .bat file
Post by: billrich on December 11, 2009, 03:55:47 PM
Sounds great.

List all your code plus  the input and the output.

The code will help many people.

Thanks for your help.
Title: Re: help with a .bat file
Post by: ghostdog74 on December 11, 2009, 05:57:46 PM
as for the vbs i'm not exactly sure on how to run it, i saved the one part as vbs and got stuck, i haven't had time to look further into it, and yes it can be a vbs, it can be anything honestly as long as the job gets done  :P
got stuck? how? what errors you got? did you change working folder to c:\test? I done the script in c:\test, so you should change to appropriate path at your side where you want to rename your files.
Title: Re: help with a .bat file
Post by: ghostdog74 on December 11, 2009, 05:59:30 PM
Sounds great.

List all your code plus  the input and the output.

The code will help many people.

Thanks for your help.


did you see reply #19 ?? that's all he has at the moment. 9 rename commands, which i think he can just use a for loop.
Title: Re: help with a .bat file
Post by: Salmon Trout on December 12, 2009, 04:09:48 AM
assuming the subfolders are all directly below a root folder

that is, in the top folder dir /ad shows something like this

Code: [Select]
12/12/2009  11:00    <DIR>          .
12/12/2009  11:00    <DIR>          ..
12/12/2009  11:00    <DIR>          Subfolder1
12/12/2009  11:00    <DIR>          Subfolder2
12/12/2009  11:00    <DIR>          Subfolder3
12/12/2009  11:00    <DIR>          Subfolder4
12/12/2009  11:00    <DIR>          Subfolder5
12/12/2009  11:00    <DIR>          Subfolder6
12/12/2009  11:00    <DIR>          Subfolder7
12/12/2009  11:00    <DIR>          Subfolder8

note... for testing purposes the subfolders have identical contents thus

Code: [Select]
2-apniia_std
3-apniqe_std
4-apniyi_std
5-apnjgm_std
6-apnjoq_std
7-apnjwu_std
8-apnkey_std
9-apnknc_std

Code: [Select]
@echo off

set thisdir=%cd%

Echo Scanning subfolders

for /f "delims=" %%D in ('dir /b /ad') do (
       
        echo Entering subfolder: %%D
       
        cd /d "%%D"
        Echo Renaming *_std to *_std.mpg
       
        if exist *_std (
               
                for /f "delims=" %%S in ('dir /b *_std') do (
               
                        echo Renaming %%S to %%S.mpg
                       
                        ren "%%S" "%%S.mpg"
               
                )
               
        ) else (
       
                echo *_std : no files to process
       
        )
       
       
        Echo Renaming 2-* to 9-* to 02-* to 09-*
        for %%N in (2 3 4 5 6 7 8 9) do (
               
                if exist "%%N-*" (
                       
                        for /f "delims=" %%F in ('dir /b %%N-*') do (
                       
                                echo Renaming %%F to 0%%F
                               
                                ren "%%F" "0%%F"
                       
                        )
               
                ) else (
               
                        echo %%N-* :   no files to process
               
                )
       
        )
       
        cd /d %thisdir%

)   

pause
     
     

Code: [Select]
Scanning subfolders
Entering subfolder: Subfolder1
Renaming 3-apniqe_std to 3-apniqe_std.mpg
Renaming 4-apniyi_std to 4-apniyi_std.mpg
Renaming 5-apnjgm_std to 5-apnjgm_std.mpg
Renaming 6-apnjoq_std to 6-apnjoq_std.mpg
Renaming 7-apnjwu_std to 7-apnjwu_std.mpg
Renaming 8-apnkey_std to 8-apnkey_std.mpg
Renaming 9-apnknc_std to 9-apnknc_std.mpg
Renaming 2-apniia_std to 2-apniia_std.mpg
Renaming 2-apniia_std.mpg to 02-apniia_std.mpg
Renaming 3-apniqe_std.mpg to 03-apniqe_std.mpg
Renaming 4-apniyi_std.mpg to 04-apniyi_std.mpg
Renaming 5-apnjgm_std.mpg to 05-apnjgm_std.mpg
Renaming 6-apnjoq_std.mpg to 06-apnjoq_std.mpg
Renaming 7-apnjwu_std.mpg to 07-apnjwu_std.mpg
Renaming 8-apnkey_std.mpg to 08-apnkey_std.mpg
Renaming 9-apnknc_std.mpg to 09-apnknc_std.mpg

Entering subfolder: Subfolder2
Renaming 3-apniqe_std to 3-apniqe_std.mpg
Renaming 4-apniyi_std to 4-apniyi_std.mpg
Renaming 5-apnjgm_std to 5-apnjgm_std.mpg
Renaming 6-apnjoq_std to 6-apnjoq_std.mpg
Renaming 7-apnjwu_std to 7-apnjwu_std.mpg
Renaming 8-apnkey_std to 8-apnkey_std.mpg
Renaming 9-apnknc_std to 9-apnknc_std.mpg
Renaming 2-apniia_std to 2-apniia_std.mpg
Renaming 2-apniia_std.mpg to 02-apniia_std.mpg
Renaming 3-apniqe_std.mpg to 03-apniqe_std.mpg
Renaming 4-apniyi_std.mpg to 04-apniyi_std.mpg
Renaming 5-apnjgm_std.mpg to 05-apnjgm_std.mpg
Renaming 6-apnjoq_std.mpg to 06-apnjoq_std.mpg
Renaming 7-apnjwu_std.mpg to 07-apnjwu_std.mpg
Renaming 8-apnkey_std.mpg to 08-apnkey_std.mpg
Renaming 9-apnknc_std.mpg to 09-apnknc_std.mpg

Entering subfolder: Subfolder3

[etc]


Subfolder contents after run

Code: [Select]
02-apniia_std.mpg
03-apniqe_std.mpg
04-apniyi_std.mpg
05-apnjgm_std.mpg
06-apnjoq_std.mpg
07-apnjwu_std.mpg
08-apnkey_std.mpg
09-apnknc_std.mpg

Of course the same code could be written in a much more compact form, but for (hopefully) instructive purposes I have used plenty of echo statements, multilining and indenting to make the flow of control easier to grasp (I hope). I have got into the habit of using "if exist" tests before file operations because I don't like error messages cluttering up the console.
Title: Re: help with a .bat file
Post by: ghostdog74 on December 12, 2009, 04:37:16 AM
how about this, since we only have 2-9 (or is it 1-9?)
Code: [Select]
...
       for /f "delims=" %%F in ('dir /b 2-* 3-* 4-* 5-* 6-* 7-* 8-* -9-*') do (
               echo Renaming %%F to 0%%F
       )
...
you reduce 1 extra for loop that calls dir 8 more times. Also, why is it you don't use /s for dir? like dir /b /s *-*std ?
Title: Re: help with a .bat file
Post by: Salmon Trout on December 12, 2009, 05:11:20 AM
how about this, since we only have 2-9 (or is it 1-9?)
Code: [Select]
...
       for /f "delims=" %%F in ('dir /b 2-* 3-* 4-* 5-* 6-* 7-* 8-* -9-*') do (
               echo Renaming %%F to 0%%F
       )
...
you reduce 1 extra for loop that calls dir 8 more times. Also, why is it you don't use /s for dir? like dir /b /s *-*std ?

Well, to tell the truth I had forgotten that you can have multiple wild cards in one dir statement like this

dir /b 2-* 3-* 4-* 5-* 6-* 7-* 8-* -9-*

but even if I had remembered I would have done it the other way because (stepping back from the purely 'writing code' perspective) it seems more orderly to process each number separately, and similarly for the dir /b /s *-*std question. Processing all the sub folders down to all levels may not be what the OP wants. Sometimes (I feel) the pursuit of code that looks good in the editor can obscure other advantages. Processing power is cheap but clarity of thought and its expression are valuable things.





Title: Re: help with a .bat file
Post by: ghostdog74 on December 12, 2009, 05:24:44 AM
Processing power is cheap but clarity of thought and its expression are valuable things.
true, that's why i never program in batch :)
Title: Re: help with a .bat file
Post by: Salmon Trout on December 12, 2009, 05:35:13 AM
I think that programming scripting in batch is a useful ability, but I do not think it is an essential one. However, there is much that can be done with it.
Title: Re: help with a .bat file
Post by: xfusion on December 14, 2009, 12:13:23 PM
only ran into one issue with the program you put salmon, it renames the _std.idx files _std.mpg.idx so i changed it to

Code: [Select]
if exist *_std (
               
if *_std.idx(
echo idx file
)
else
(
                for /f "delims=" %%S in ('dir /b *_std') do (
               
                        echo Renaming %%S to %%S.mpg
                       
                        ren "%%S" "%%S.mpg"
        )       
                )
               
        ) else (
       
                echo *_std : no files to process
       
        )

that should work correct?
Title: Re: help with a .bat file
Post by: xfusion on December 14, 2009, 12:48:20 PM
then again, wouldn't this work?

Code: [Select]
@echo off

set thisdir=%cd%

Echo Scanning subfolders

for /f "delims=" %%D in ('dir /b /ad') do (
       
        echo Entering subfolder: %%D
       
        cd /d "%%D"
        Echo Renaming *_std to *_std.mpg
ren *_std *_std.mpg
Echo renaming 2 to 02
ren 2-* 02-*
ren 3-* 03-*
ren 4-* 04-*
ren 5-* 05-*
ren 6-* 06-*
ren 7-* 07-*
ren 8-* 08-*
ren 9-* 09-*

       
        cd /d %thisdir%

)   

pause

less informative
Title: Re: help with a .bat file
Post by: Salmon Trout on December 14, 2009, 12:50:29 PM
Quote
if *_std.idx(

not sure what you think this will do?

Anyhow, I use this modified version (changed line in blue)


@echo off

set thisdir=%cd%

Echo Scanning subfolders

for /f "delims=" %%D in ('dir /b /ad') do (
       
        echo Entering subfolder: %%D
       
        cd /d "%%D"
        Echo Renaming *_std to *_std.mpg
       
        if exist *_std (
               
                for /f "delims=" %%S in ( ' dir /b *_std ^| find /v "_std.idx" ' ) do (
               
                        echo Renaming %%S to %%S.mpg
                       
                        ren "%%S" "%%S.mpg"
               
                )
               
        ) else (
       
                echo *_std : no files to process
       
        )
       
       
        Echo Renaming 2-* to 9-* to 02-* to 09-*
        for %%N in (2 3 4 5 6 7 8 9) do (
               
                if exist "%%N-*" (
                       
                        for /f "delims=" %%F in ('dir /b %%N-*') do (
                       
                                echo Renaming %%F to 0%%F
                               
                                ren "%%F" "0%%F"
                       
                        )
               
                ) else (
               
                        echo %%N-* :   no files to process
               
                )
       
        )
       
        cd /d %thisdir%

)   

pause
Title: Re: help with a .bat file
Post by: Salmon Trout on December 14, 2009, 12:51:32 PM
then again, wouldn't this work?

Code: [Select]
@echo off

set thisdir=%cd%

Echo Scanning subfolders

for /f "delims=" %%D in ('dir /b /ad') do (
       
        echo Entering subfolder: %%D
       
        cd /d "%%D"
        Echo Renaming *_std to *_std.mpg
ren *_std *_std.mpg
Echo renaming 2 to 02
ren 2-* 02-*
ren 3-* 03-*
ren 4-* 04-*
ren 5-* 05-*
ren 6-* 06-*
ren 7-* 07-*
ren 8-* 08-*
ren 9-* 09-*

       
        cd /d %thisdir%

)   

pause

less informative

Why not give it a try?