Code: Select all
FOR %a IN (*.txt) do ECHO %~nxa >> %a
Moderators: Hacker, petermad, Stefan2, white
Code: Select all
FOR %a IN (*.txt) do ECHO %~nxa >> %a
Stephan2 Dear!: I was looking for the very solution that you gave me by being "silly". I wanted the filenames to be inserted at the bottom of each selected file (not to a file named ALL.txt, sorry for not being clear). I could (kind of) fix this by changing the parameter to:Silly Me!
Code: Select all
FOR /F "tokens=*" %%A IN ('TYPE "%F"') do @ECHO %%~nxA >> "%O.%E"
Code: Select all
FOR /F "tokens=*" %%A IN ('TYPE "%F"') do @ECHO %%~nxA >> %%A
I got stuck here. You could almost see where I would stumble.If your files end with the last line of text (and no line break), there will be no empty line added.
You saved so much of my upcoming headache.And as Dalai always says: use quotes, just in case your file names contains spaces: "%%A" ....instead of.... %%A
Don't have the courage to start learning one and would be reluctant to ask someone to write it for me.If you need something more reliable, use VBScript or PowerShell
Code: Select all
powershell.exe
Code: Select all
-NoProfile -ExecutionPolicy remotesigned -Command "&{&'C:\PathTo\AddFileName.ps1' -FileList '%L'}"
Code: Select all
param(
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]$FileList
)
$filePaths = Get-Content -Path $FileList
foreach ($filePath in $filePaths)
{
if (Test-Path -Path $filePath -PathType Leaf)
{
$fileName = Split-Path -Path $filePath -Leaf
# Get the last byte of the file and check if it's a 10/LF
$fs = [IO.File]::OpenRead($filePath)
$fs.Seek(-1, 'End') | Out-Null
$bytelast = $fs.ReadByte()
$fs.close()
if ($bytelast -eq 10)
{
Add-Content -Path $filePath -Value "$fileName"
}
else
{
Add-Content -Path $filePath -Value "`r`n$fileName"
}
}
}
Code: Select all
if ( (gc $filePath )[-1].Trim() -match "^$" )
{
#"empty line"
Add-Content -Path $filePath -Value "`r`n$fileName"
}
else
{
#"text line"
Add-Content -Path $filePath -Value "`r`n`r`n$fileName"
}
Code: Select all
------------------------------
Test
------------------------------
TestCRLF
------------------------------
TestCRLF
CRLF
------------------------------
TestCRLF
CRLF
------------------------------
Code: Select all
------------------------------
Test
CRLF <<added
filename <<added
------------------------------
TestCRLF
CRLF <<added
CRLF <<added <<<< bug
filename <<added
------------------------------
TestCRLF
CRLF
filename <<added
------------------------------
TestCRLF
CRLF
filename <<added
------------------------------
Code: Select all
TOTALCMD#BAR#DATA
PowerShell -NoProfile
TYPE '%F' | %%{IF( @(gc $_ )[-1] -NotMatch """^\s*$"""){Add-Content $_ """`r`n"""};Add-Content $_ $_ }
PowerShell
Append FILENAME to File content
-1