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

  • Thread starter Thread starter feynman1
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around how to append a file name to the contents of .txt files using Windows command line (cmd) and explores various scripting approaches, including Batch files and potential alternatives like Python and Linux tools.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant asks how to append the file name into the contents of a single .txt file and for all .txt files in a directory.
  • Another participant suggests a Unix shell script as a solution, noting that they have not tested it.
  • A participant provides a Batch file script for Windows, but acknowledges their rudimentary skills in DOS batch scripting.
  • There is a request for a method to append the file name without the .txt suffix.
  • One participant mentions the lack of an inline regex routine in Windows cmd, suggesting that PowerShell or VBS might be necessary but could require policy edits.
  • A proposed solution involves using a FOR loop in a Batch file to rename files and append the file name, but it is noted to be complicated.
  • Another participant suggests that working in a directory with only .txt files is advisable to avoid issues with extraneous files during renaming.
  • There are suggestions to use Linux tools or Python with Anaconda for easier handling of such tasks, highlighting the availability of regex in Python.
  • Participants discuss the challenges of accessing the Windows filesystem from a virtual machine and suggest using Windows Subsystem for Linux (WSL) as a more straightforward option.
  • One participant provides a Python example using glob and regex to manipulate file names, aiming to guide others in the right direction.

Areas of Agreement / Disagreement

Participants express various methods and approaches without reaching a consensus on a single solution. Multiple competing views remain regarding the best way to achieve the task using different tools and scripting languages.

Contextual Notes

Limitations include the complexity of DOS batch scripting, the need for policy edits to run PowerShell, and potential user permission issues when using Jupyter notebooks in Linux.

Who May Find This Useful

This discussion may be useful for users looking to manipulate .txt files in Windows using command line tools, as well as those interested in exploring alternative scripting solutions in Python or Linux environments.

feynman1
Messages
435
Reaction score
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
Operating system?

It's trivial in Unix, something like:

[CODE lang="bash" title="For Loop"]#!/bin/sh

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

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

[CODE lang="aspnet" title="Windows Batch File Loop"]FOR %y IN (.\*.txt) DO @ECHO %y >> %y[/CODE]

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   Reactions: feynman1
Melbourne Guy said:
You can write a similar Batch file script, but of course DOS uses different commands:

[CODE lang="aspnet" title="Windows Batch File Loop"]FOR %y IN (.\*.txt) DO @ECHO %y >> %y[/CODE]

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?
 
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   Reactions: feynman1
who can help me get rid of inputting '.txt' into the .txt file
 
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.

[CODE title="DOS BATCH FILE"]rename *.txt *.
FOR %%f IN (*.) DO (
ECHO %%f >> %%f
RENAME "%%f" "%%f".txt
)[/CODE]
 
  • Like
Likes   Reactions: feynman1
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.

[CODE title="DOS BATCH FILE"]rename *.txt *.
FOR %%f IN (*.) DO (
ECHO %%f >> %%f
RENAME "%%f" "%%f".txt
)[/CODE]
oh that works, thanks! so I just need to clear all non txt files.
 
  • Like
Likes   Reactions: 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   Reactions: 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

Similar threads

  • · Replies 20 ·
Replies
20
Views
4K
Replies
35
Views
7K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 32 ·
2
Replies
32
Views
4K
Replies
50
Views
7K
Replies
48
Views
5K
  • · Replies 16 ·
Replies
16
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K