How to put a FOR loop command on the button bar?

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

How to put a FOR loop command on the button bar?

Post by *sorcar »

The following simple script, if executed from a command prompt/TC Command line, appends filenames at the bottom of all txt file in the directory.

Code: Select all

FOR %a IN (*.txt) do ECHO %~nxa >> %a
How to have it work only on only selected files in TC? Thanks.
Last edited by sorcar on 2016-04-11, 19:53 UTC, edited 1 time in total.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

DOS Batch: only process selected files

Post by *Stefan2 »

 

COMMAND: cmd /c
PARAMETER: FOR /F "tokens=*" %%A IN ('TYPE "%F"') do @ECHO %%~nxA >> _ALL.txt




- For info about %F, press F1 key while in button dialog.
%F points to a temporary file, which contains the selected file names.

- tokens=* will take the whole line from that temp file list, even if they contain blank spaces.

- As in batch files, you have to double the percent signs of the variables.





 
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

Silly me :D


COMMAND: cmd /c
PARAMETER: COPY "%F" _ALL2.txt







  :P
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

Silly Me!
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:

Code: Select all

FOR /F "tokens=*" %%A IN ('TYPE "%F"') do @ECHO %%~nxA >> "%O.%E"
This works only on one file at a time. But if multiple files are selected, all selected filenames get appended to the last file. How to fix this?
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

Oops! :oops:
I could fix this by parameter:

Code: Select all

FOR /F "tokens=*" %%A IN ('TYPE "%F"') do @ECHO %%~nxA >> %%A
Last edited by sorcar on 2016-04-11, 20:28 UTC, edited 1 time in total.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

Ah I see! Makes sense, glad you worked it out ;-)





 
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

But I couldn't ECHO a newline character before the appended text. Can it be done?

And thanks for explaining how FOR loop works in TC. I couldn't have figured it out.

Regards.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

DOS Batch: send empty new line to file

Post by *Stefan2 »

I know that is coming :D

Just double the command and concatenate them by an ampersand sign '&'
To send an empty line, utilize ECHO+Dot: ECHO.

FOR /F "tokens=*" %%A IN ('TYPE "%F"') do @ECHO.  >> "%%A" & ECHO %%~nxA >> "%%A"


But that works only if there is already a trailing line break in your files.
If your files end with the last line of text (and no line break), there will be no empty line added.
So maybe double that empty line command?:
FOR /F "tokens=*" %%A IN ('TYPE "%F"') do @ECHO. >> "%%A" & ECHO. >> "%%A" & ECHO %%~nxA >> "%%A"


And as Dalai always says: use quotes, just in case your file names contains spaces: "%%A" ....instead of.... %%A




If you need something more reliable, use VBScript or PowerShell

 
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

If your files end with the last line of text (and no line break), there will be no empty line added.
I got stuck here. You could almost see where I would stumble.
And as Dalai always says: use quotes, just in case your file names contains spaces: "%%A" ....instead of.... %%A
You saved so much of my upcoming headache.
If you need something more reliable, use VBScript or PowerShell
Don't have the courage to start learning one and would be reluctant to ask someone to write it for me.

All the best for being so helpful, so promptly, and with thoroughness.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

>>>would be reluctant to ask someone

Just ask. Some guys here need such challenge for there launch break.
Usually you will get many different solutions.....

And if you get no answer at all, you have not many lost.



Just my $0,02. 
User avatar
ZoSTeR
Power Member
Power Member
Posts: 1052
Joined: 2004-07-29, 11:00 UTC

Post by *ZoSTeR »

As expected in PowerShell :wink: :

Create a button with
Command

Code: Select all

powershell.exe
Parameters

Code: Select all

-NoProfile -ExecutionPolicy remotesigned -Command "&{&'C:\PathTo\AddFileName.ps1' -FileList '%L'}"
Check "Run minimized"

Adjust the path to the script in the parameter and save the following as "AddFileName.ps1":

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"
        }
    }
}
The script will check the last byte of the files to see if it has a linefeed / x0A character and will add a new line accordingly.
If you need an extra line before the filename just add an extra "`r`n" (CRLF) to the -Values.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

PowerShell:

Post by *Stefan2 »

Little late for launch break, isn't it? :D

Good work.






Would that more simple code also work?:

$LastLine = (gc $filePath )[-1]
IF( $LastLine.Trim() -match "^$" ){"empty line"}else{"text line"}



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"
        }




I hope you don't mind :oops:


 
User avatar
ZoSTeR
Power Member
Power Member
Posts: 1052
Joined: 2004-07-29, 11:00 UTC

Post by *ZoSTeR »

@Stefan:
That was my first approach too but that only works if you have:
lastLineCRLF
CRLF

I'm checking for:
lastLineCRLF
-or
lastLineNOTHING
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

It is amazing to see how spontaneous the members of this forum is. It makes visiting a joy. Both the scripts work fine, and I have started using them in my daily work.

Thanks Stephan2, Thanks ZoSTeR.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

Yes I see.

But I am thinking it works pretty fine:


FROM file content:

Code: Select all

------------------------------
Test
------------------------------
TestCRLF
------------------------------
TestCRLF
CRLF
------------------------------
TestCRLF
  CRLF
------------------------------

TO file content:

Code: Select all

------------------------------
Test
CRLF <<added
filename <<added
------------------------------
TestCRLF
CRLF <<added
CRLF <<added <<<< bug
filename <<added
------------------------------
TestCRLF
CRLF
filename <<added
------------------------------
TestCRLF
  CRLF
filename <<added
------------------------------

PoSh with TC-syntax (tripled quotes and doubled percent signs):

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



Not perfect at all. But not bad for an PoSh One-Liner   :P

But your external script have more potential for improvement and error-handling.

 
Post Reply