Can You Append a File Name to .txt Files with Windows cmd?

  • Thread starter feynman1
  • Start date
In summary: Accessing the windows filesystem from inside a VM is not trivial so I wouldn't recommend this. Alternatively, you can install anaconda in windows and run similar commands from e.g. a jupyter notebook.
  • #1
feynman1
435
29
For 1 given .txt file, how to append its file name into the main contents of this .txt file? For all .txt files in a directory, how to append the file name into the main contents of each .txt file?
 
Technology news on Phys.org
  • #2
Operating system?

It's trivial in Unix, something like:

For Loop:
#!/bin/sh

for FILE in $HOME/*.txt
do
   echo $FILE >> $FILE
done

Should do the job, noting that I've not actually fired up my Linux laptop to test this :cool:
 
  • Like
Likes feynman1
  • #3
thanks, but how about in windows
 
  • #4
You can write a similar Batch file script, but of course DOS uses different commands:

Windows Batch File Loop:
FOR %y IN (.\*.txt) DO @ECHO %y >> %y

With an even bigger note that my DOS batch file skills are rudimentary but hopefully this points you in the right direction, @feynman1.
 
  • Like
Likes feynman1
  • #5
Melbourne Guy said:
You can write a similar Batch file script, but of course DOS uses different commands:

Windows Batch File Loop:
FOR %y IN (.\*.txt) DO @ECHO %y >> %y

With an even bigger note that my DOS batch file skills are rudimentary but hopefully this points you in the right direction, @feynman1.
thanks that's cool! But how can I get rid of suffix .txt?
 
  • #6
In Windows? Sorry, @feynman1, there isn't an inline regex routine to do that I'm aware of outside of PowerShell or within VBS and that's not straightforward. For example, your PC might need policy edits to run PowerShell.

In Unix, you'd just pipe it through sed, and there are a lot of equivalents to sed (and complete Unix-like wrappers), but they require that you download packages to your computer...and you need to know how to use Unix commands.

Within a DOS batch file, the FOR loop could be used to try:

1. Set a variable to the current filename in the FOR loop
2. RENAME to change the filename and strip off the .txt extension
3. ECHO the filename
4. RENAME the file to the original .txt filename using the variable you set in Step #1

It's pretty complicated, though, and my DOS BATCH skills are really rusty :frown:
 
  • Like
Likes feynman1
  • #7
who can help me get rid of inputting '.txt' into the .txt file
 
  • #8
feynman1 said:
who can help me get rid of inputting '.txt' into the .txt file
This can work as a DOS BATCH file, though it helps to run it in a directory with only the .txt files you want to edit, otherwise the second RENAME will pick up extraneous files.

DOS BATCH FILE:
rename *.txt *.
FOR %%f IN (*.) DO (
ECHO %%f >> %%f
RENAME "%%f" "%%f".txt
)
 
  • Like
Likes feynman1
  • #9
Melbourne Guy said:
This can work as a DOS BATCH file, though it helps to run it in a directory with only the .txt files you want to edit, otherwise the second RENAME will pick up extraneous files.

DOS BATCH FILE:
rename *.txt *.
FOR %%f IN (*.) DO (
ECHO %%f >> %%f
RENAME "%%f" "%%f".txt
)
oh that works, thanks! so I just need to clear all non txt files.
 
  • Like
Likes Melbourne Guy
  • #10
If you do this kind of work more often, it is worthwhile to have linux and its tools available. You could install it in a virtual box and run it in a window inside MS windows if you do not want to bother with a dual boot system.

You also have regex available in python, so alternatively you can install anaconda in windows and run similar commands from e.g. a jupyter notebook. This is maybe the easiest approach.
 
  • Like
Likes feynman1
  • #11
bigfooted said:
If you do this kind of work more often, it is worthwhile to have linux and its tools available. You could install it in a virtual box and run it in a window inside MS windows if you do not want to bother with a dual boot system.

You also have regex available in python, so alternatively you can install anaconda in windows and run similar commands from e.g. a jupyter notebook. This is maybe the easiest approach.
Thanks but how to do it in anaconda/jupyter in windows?
 
  • #12
bigfooted said:
If you do this kind of work more often, it is worthwhile to have linux and its tools available. You could install it in a virtual box and run it in a window inside MS windows if you do not want to bother with a dual boot system.
Accessing the windows filesystem from inside a VM is not trivial so I wouldn't recommend this. It's easier from a Windows Subsystem for Linux (WSL) installation though. However neither of these are as easy as your next suggestion, which would be my recommendation.
bigfooted said:
You also have regex available in python, so alternatively you can install anaconda in windows and run similar commands from e.g. a jupyter notebook. This is maybe the easiest approach.
Or if you are more familiar with another scripting language (e.g. Node.js or even PHP) then this would be the easiest route.
feynman1 said:
Thanks but how to do it in anaconda/jupyter in windows?
Using the file methods. It is usually easier in Windows because you don't have to worry so much about user permissions (in Linux the Jupyter kernel may not be running with the right permissions to modify the files you want).
 
  • #13
Install anaconda:
https://www.anaconda.com/products/individual
Anaconda is a data science platform that offers jupyter notebook (or jupyter labs). You can then use that to do some python programming:
https://www.dataquest.io/blog/jupyter-notebook-tutorial/
and then read the introduction to python:
https://www.w3schools.com/python/
And then you are ready for some regex:
https://www.w3schools.com/python/python_regex.asp

you can do something like this to get a list of all .txt files:
Python:
import glob
filelist = glob.glob(r'c:\myDocuments\*.txt')

You then have a list of filenames. There are many options to strip the extension, but here is one to strip the extension of the first entry in the list using regex:
Python:
import re
re.sub('\.txt$', '', filelist[0])

I hope this will get you started in the right direction.
 
  • #14

FAQ: Can You Append a File Name to .txt Files with Windows cmd?

How do I add a filename to a .txt file?

In order to add a filename to a .txt file, you can simply type the name of the file at the top of the document or use the "Save As" function to rename the file. This will automatically add the filename to the .txt file.

Can I add a filename to an existing .txt file?

Yes, you can add a filename to an existing .txt file by using the "Save As" function and renaming the file, or by simply typing the name of the file at the top of the document. Just make sure to save the changes afterwards.

Is it possible to add a filename to a .txt file without changing the content?

Yes, it is possible to add a filename to a .txt file without changing the content. You can either type the name of the file at the top of the document or use the "Save As" function to rename the file. This will not alter the content of the file.

How can I add a specific filename to a .txt file?

To add a specific filename to a .txt file, you can use the "Save As" function and type in the desired filename, or you can manually type the filename at the top of the document. Make sure to save the changes afterwards.

What should I do if the filename does not appear in the .txt file after adding it?

If the filename does not appear in the .txt file after adding it, make sure to save the changes before closing the document. If the issue persists, try closing and reopening the document or double check that the filename was properly typed or saved.

Similar threads

Replies
20
Views
2K
Replies
41
Views
4K
Replies
32
Views
3K
Replies
48
Views
4K
Replies
16
Views
5K
Back
Top