Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder containing all types of files. Let's say my folder path is E:/temp.

I would like to create, only for the .avi type files, a txt file and name the txt file as the name of the movie. so if I have 20 clip .avi files in the folder, I want to open 20 txt file, and each txt file will be named after a clip.

Also, inside the txt file I need to write (for all txt file it will be the same) the word "HAPPY"

What will be the script using batch file?

share|improve this question

You need to use FOR to iterate over the elements you want to work with. See How to loop through files matching wildcard in batch file for details.

Once you have that part solved, you'll want to create the filename of your .txt file. The command line documentation of FOR can help us here:

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only

The last entry is what we want. We want to cut off the .avi, so we use ~n in our variable to get only the name. This is where the %%~nf.txt comes from.

We start with %%f, the current filename. Then we cut off the extension with %%~nf and tack on the .txt... Done: %%~nf.txt

The last problem is when you try to solve it all on one line, like:

FOR %%f IN (*.avi) DO ECHO HAPPY > %%~nf.txt

That wouldn't work, because the shell will interpret the > and start outputting to that file instantly, which is not what we want. We want the ECHO to be processed for every single file, so I simply split it into multiple lines.


REM Iterate over all *.avi file in the current directory
FOR %%f IN (*.avi) DO (
    REM Cut off the extension from %%f, tack on .txt and 
    REM use it as the filename for our HAPPY output
    ECHO HAPPY > %%~nf.txt
share|improve this answer
Hi, Thank you sooo much!! it works and I am HAPPY. just one more thing... i am trying to understand how the loop thing is working. the clips that I am interested in are divided into many sub folders. so I have a main folder, inside many more folders, each folder containes another level of folders and the clips are inside the third level. and I want to do the same thing you taught me for all those folders but instead of copying the bat file you gave me and run it for each of the sub folders I want to be able to do it from the outer folder (the main one) automaticly. Thank you again!!!! Shir – shir Nov 12 '12 at 7:14
so I wrote this: FOR /r %%f IN (*.avi) DO ( %%~nf.txt ECHO Philips > %%~nf_Medical_System.txt ) but the txt file are located where the bat file is ...and I want each txt file to be under its movie.. how can I locate it in the subfolder it belongs? Thank you again!!!! Shir – shir Nov 12 '12 at 7:36
and also, are the files must be localy on my computer in order for it to work? – shir Nov 12 '12 at 7:53
@shir Try using %%~dpnf.txt instead. It should then use the Drive Letter + Full Path + Name + .txt. Maybe that will help. Otherwise make sure to let me know :) – Oliver Salzburg Nov 12 '12 at 11:30
You are the best! thank you! it is working perfectly – shir Nov 12 '12 at 11:47

Create a text file named happy.txt containing "HAPPY". Create a batch file named makehappy.cmd containing this:

for %%i in (*.avi) do copy happy.txt "%%i.txt"

Run makehappy. The new text files will have a double extension, but there are ways around that if necessary.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.