- #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?
#!/bin/sh
for FILE in $HOME/*.txt
do
echo $FILE >> $FILE
done
FOR %y IN (.\*.txt) DO @ECHO %y >> %y
thanks that's cool! But how can I get rid of suffix .txt?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.
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.feynman1 said:who can help me get rid of inputting '.txt' into the .txt 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.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 )
Thanks but how to do it in anaconda/jupyter in windows?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.
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: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.
Or if you are more familiar with another scripting language (e.g. Node.js or even PHP) then this would be the easiest route.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.
Using thefeynman1 said:Thanks but how to do it in anaconda/jupyter in windows?
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).import glob
filelist = glob.glob(r'c:\myDocuments\*.txt')
import re
re.sub('\.txt$', '', filelist[0])
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.
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.
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.
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.
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.