Subscribe to
Posts
Comments

I was trying to write a batch file to rename

all files in a folder on windows. And the following is what I came up with. It worked very well. I found one of the great usage of my blog was to archive whatever script I came up with. It could help someone to quickly find solution for the same problem I experienced and at the same time I could easily find my archived script using the search box on my blog.

rem: Some testing batch command in Windows XP

setlocal ENABLEDELAYEDEXPANSION
@echo off

rem ***************** Test 1 **********************
rem about variables

set var1= This is a variable VAR1
set var2=’This is a variable VAR2′
set var3=”This is a variable VAR3″
echo %var1%
echo ‘%var2%’
echo “%var3%”

rem ***********************************************
rem Conclusion from test 1 is that assigning text
rem value to variable doesn’t need to be quoted
rem ***********************************************

rem **************** Test 2 **********************
rem about for loop

set count=0
for %%a in (1 2 3) do (
echo !count!
set /A count=!count!+1
echo %%a
)

for %%A in (1 2) do for %%B in (A B) DO ECHO %%A%%B

rem **********************************************
rem Conclusion from test 2 is that variable in
rem the for loop has to be single character like
rem %%a. %%var will not work
rem you don’t have to have
rem setlocal ENABLEDELAYEDEXPANSION for the count
rem to work but you do have to use !count! instead
rem of %count%
rem **********************************************

rem **************** Test 3 **********************
rem use for loop to list all .txt file names

for /f %%a in (’dir /b transaction*.txt’) do (
echo %%a
)

rem **********************************************
rem I don’t know what the switch /f in the for
rem statement and the /A switch in the set
rem statement mean but it works.
rem Can someone explain the switch? =)
rem **********************************************

rem **************** Test 4 **********************
rem rename all .txt file so that all of them
rem starts with “trans” instead of “transaction”

for /f “tokens=1,2 delims=_” %%a in (’dir /b transaction*.txt’) do (
if “%%a”==”transaction” echo yes
echo %%a
echo %%b
ren %%a_%%b tran_%%b
)

rem **********************************************
rem The above script works fine. It renames all
rem files named in partern transaction_XXXX.txt
rem to trans_XXXXX.txt
rem “tokens=1,2 delims=_” tells it to separate
rem whatever returned in dir command in two parts
rem using the first “_” found and put the first
rem token in %%a, the second in %%b.
rem It is very powerful and interesting!
rem **********************************************

goto :eof


Related Posts:

  • VB Script to Rename All Files in a Folder
  • The Best Way to Upgrade Wordpress
  • How to Trouble Shoot SQL Server Job Error 8198 - Unable to Determine if the Owner of Job Has Server Access
  • Shell Script to Start Tomcat on Reboot Using Non-Root User
  • TDSS Malware, Compalusa.com and Disk Defragmenter Could Not Start


  • 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3 out of 5)
    Loading ... Loading ...

    RSS feed | Trackback URI

    5 Comments »

    2009-02-24 13:51:44
    MyAvatars 0.2

    […] writing previous post about using window batch command to rename all files in a folder I found using VB Script to do the same thing is much […]

     
    Comment by Jeremy
    2009-03-20 04:30:59
    MyAvatars 0.2

    Nice one, many thanks for this. The solutions in here were very handy to me, but just as useful were the methodical tests and explanation of switches (where you knew them!) Now we have the solution we needed, having got close but fallen at the last hurdle before.
    One additional remark: if you decide NOT to set ENABLEDELAYEDEXPANSION, then the !variable! syntax fails. Not sure if that means that %variable% would work in that situation instead, but just thought I’d be explicit about where ENABLEDELAYEDEXPANSION had an effect (though obviously it’s relevant in other parts too)
    Thanks again

     
    Comment by SenHu Subscribed to comments via email
    2009-08-13 09:07:33
    MyAvatars 0.2

    I use biterscripting ( http://www.biterscripting.com ) for file rename. It offers more flexibility and power and is easy to learn. You can try it. I have seen several rename biterscripts posted on the net. I also have posted some although a small number.

    Sen

     
    Comment by Simon
    2011-05-05 07:35:05
    MyAvatars 0.2

    Thank you for the script examples!

    The /f simply indicates the for loop is using a filename set
    The /A means the right hand side of the equal sign is a numeric value

    Got these from /?

     
    Comment by Anonymous
    2011-08-06 16:05:55
    MyAvatars 0.2

    set /A var=…

    Defines an Arithmatic operation.

     
    Name (required)
    E-mail (required - never shown publicly)
    URI
    Subscribe to comments via email
    Your Comment (smaller size | larger size)
    You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.