Page 1 of 1
Batch code to paste from clipboard to a file?
Posted: 12 Mar 2012 08:49
by sneaky
For a .bat file, how can I write whats currently copied from the clipboard to a new file. If the file exists, it will overwrite it, if it doesn't exist, then it will create the file.
So if I were to copy a text, then run the .bat file, it will create the file, then write what you copied, to that file.
Something like
CLIP > "output.txt"
Re: Batch code to paste from clipboard to a file?
Posted: 12 Mar 2012 09:29
by abc0502
Hi here is a vbscript don't ask me how it work i just took part of the code from here
http://community.spiceworks.com/scripts/show/358-paste-clipboard-to-fileand changed it abit and by luck it worked even i don't know the vbscript
Code: Select all
Set objHTML = CreateObject("htmlfile")
ClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text")
path = "D:\1.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(path, 2, true)
objFile.WriteLine ClipboardText
objFile.Close
save it as .vbs file type and change the "D:\1.txt" with your directory
you can run the script using patch or make the patch create the vbscript to a temp then run it so it will be one file
Re: Batch code to paste from clipboard to a file?
Posted: 12 Mar 2012 09:43
by abc0502
OK I made a batch file here:
it make the vbs file in %temp% and run it to copy the clipboard to a text file then delete the vbs script
Code: Select all
@echo off
cls
set "path=D:\clip.txt"
>"%temp%\clipboard.vbs" (
echo.Set objHTML = CreateObject("htmlfile"^)
echo.ClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text"^)
echo.path = "%path%"
echo.Set objFSO = CreateObject("Scripting.FileSystemObject"^)
echo.Set objFile = objFSO.OpenTextFile(path, 2, true^)
echo.objFile.WriteLine ClipboardText
echo.objFile.Close )>>"%temp%\clipboard.vbs"
"%temp%\clipboard.vbs"
del /F /Q %temp%\clipboard.vbs
don't forget o change the path variable
Re: Batch code to paste from clipboard to a file?
Posted: 12 Mar 2012 09:54
by foxidrive
abc0502 - that worked here too and I wrapped it into a batch file. (Edit: you were too quick for me)
If a filename is not specified on the command line then it will use cliptext.txt in the current folder.
Code: Select all
@echo off
if "%~f1"=="" %0 "cliptext.txt"
(
echo Set objHTML = CreateObject("htmlfile"^)
echo ClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text"^)
echo Set objFSO = CreateObject("Scripting.FileSystemObject"^)
echo Set objFile = objFSO.OpenTextFile("%~f1", 2, true^)
echo objFile.WriteLine ClipboardText
echo objFile.Close
) > "%temp%\clip.vbs"
start "" "%temp%\clip.vbs"
Re: Batch code to paste from clipboard to a file?
Posted: 12 Mar 2012 10:07
by abc0502
@foxidrive
it was realy just luck
Re: Batch code to paste from clipboard to a file?
Posted: 12 Mar 2012 11:19
by !k
Re: Batch code to paste from clipboard to a file?
Posted: 12 Mar 2012 14:59
by aGerman
paste.vbs
Code: Select all
Set colNamendArgs = WScript.Arguments.Named
iNamedArgs = colNamendArgs.Count
ret = 1
Do
If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" And iNamedArgs = 0 Then Exit Do
If iNamedArgs = 0 Then
WScript.Echo getString
ret = 0
Exit Do
End If
If colNamendArgs.Exists("?") Then
displayHelp
ret = 0
Exit Do
End If
If Not colNamendArgs.Exists("o") Then Exit Do
If colNamendArgs.Item("o") = "" Then Exit Do
open = 8
If colNamendArgs.Exists("w") Then open = 2
createnew = False
If colNamendArgs.Exists("c") Then createnew = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Err.Clear
On Error Resume Next
Set objFile = objFSO.OpenTextFile(colNamendArgs.Item("o"), open, createnew)
objFile.WriteLine getString
objFile.Close
On Error Goto 0
If Not Err Then ret = 0
Exit Do
Loop
Set colNamendArgs = Nothing
Set objFSO = Nothing
Set objFile = Nothing
WScript.Quit ret
Function getString()
Set objHTML = CreateObject("HTMLFile")
getString = objHTML.ParentWindow.ClipboardData.GetData("text")
Set objHTML = Nothing
End Function
Sub displayHelp
WScript.Echo "Pastes text from clipboard." & vbLf & vbLf & _
"[cscript //nologo] paste.vbs [ /? | /o:FileName [ /w /c ] ]" & vbLf & vbLf & _
"Note: Run in cscript mode if executed without arguments!" & vbLf & _
" /?" & vbTab & vbTab & "Displays this help message." & vbLf & _
" /o" & vbTab & vbTab & "Redirects to the defined file." & vbLf & _
" FileName" & vbTab & " File name or full name where to redirect." & vbLf & _
" /w" & vbTab & vbTab & "Overwrites the defined file. (Appends by default.)" & vbLf & _
" /c" & vbTab & vbTab & "Creates not existing file. (Not created by default.)" & vbLf
End Sub
Usage:
[cscript //nologo] paste.vbs [ /? | /o:FileName [ /w /c ] ]
Note: Run in cscript mode if executed without arguments!
/? Displays this help message.
/o Redirects to the defined file.
FileName File name or full name where to redirect.
/w Overwrites the defined file. (Appends by default.)
/c Creates not existing file. (Not created by default.)
Regards
aGerman
Re: Batch code to paste from clipboard to a file?
Posted: 07 Jan 2018 07:06
by dvakdva
Dear aGerman,
thank you for this code. This is only True for standard Codepage, can you please modify the code to recognize special character?
Best Regards
Re: Batch code to paste from clipboard to a file?
Posted: 07 Jan 2018 16:23
by aGerman
I can't change the behavior of the console window in case you don't use option /o. If you want to write to a file then UTF-16 could be used.
Code: Select all
Set colNamendArgs = WScript.Arguments.Named
iNamedArgs = colNamendArgs.Count
ret = 1
Do
If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" And iNamedArgs = 0 Then Exit Do
If iNamedArgs = 0 Then
WScript.Echo getString
ret = 0
Exit Do
End If
If colNamendArgs.Exists("?") Then
displayHelp
ret = 0
Exit Do
End If
If Not colNamendArgs.Exists("o") Then Exit Do
If colNamendArgs.Item("o") = "" Then Exit Do
If colNamendArgs.Exists("u") Then
Set objADOS = CreateObject("ADODB.Stream")
objADOS.CharSet = "utf-16"
objADOS.LineSeparator = -1
objADOS.Open
If Not colNamendArgs.Exists("w") Then
On Error Resume Next
objADOS.LoadFromFile colNamendArgs.Item("o")
objADOS.Position = objADOS.Size
On Error Goto 0
End If
Err.Clear
Set objHTML = CreateObject("HTMLFile")
objADOS.WriteText objHTML.ParentWindow.ClipboardData.GetData("text"), 1
Set objHTML = Nothing
On Error Resume Next
objADOS.SaveToFile colNamendArgs.Item("o"), 2
If Err.Number = 0 Then ret = 0
On Error Goto 0
objADOS.Close
Set objADOS = Nothing
Exit Do
End If
open = 8
If colNamendArgs.Exists("w") Then open = 2
createnew = False
If colNamendArgs.Exists("c") Then createnew = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Err.Clear
On Error Resume Next
Set objFile = objFSO.OpenTextFile(colNamendArgs.Item("o"), open, createnew)
objFile.WriteLine getString
objFile.Close
If Err.Number = 0 Then ret = 0
On Error Goto 0
Exit Do
Loop
Set colNamendArgs = Nothing
Set objFSO = Nothing
Set objFile = Nothing
WScript.Quit ret
Function getString()
Set objHTML = CreateObject("HTMLFile")
getString = objHTML.ParentWindow.ClipboardData.GetData("text")
Set objHTML = Nothing
End Function
Sub displayHelp
WScript.Echo "Pastes text from clipboard." & vbLf & vbLf & _
"[cscript //nologo] paste.vbs [ /? | /o:FileName [ [ /w /c ] | [ /w /u ] ]" & vbLf & vbLf & _
"Note: Run in cscript mode if executed without arguments!" & vbLf & _
" /?" & vbTab & vbTab & "Displays this help message." & vbLf & _
" /o" & vbTab & vbTab & "Redirects to the defined file." & vbLf & _
" FileName" & vbTab & " File name or full name where to redirect." & vbLf & _
" /w" & vbTab & vbTab & "Overwrites the defined file. (Appends by default.)" & vbLf & _
" /c" & vbTab & vbTab & "Creates not existing file. (Not created by default.)" & vbLf & _
" /u" & vbTab & vbTab & "Writes to the file as UTF-16 with BOM instead of ANSI." & vbLf & _
" " & vbTab & vbTab & "Default behavior changes in a way that a not existing" & vbLf & _
" " & vbTab & vbTab & "file will always be created. (Omitting /c has no effect.)"
End Sub
Usage:
Code: Select all
Pastes text from clipboard.
[cscript //nologo] paste.vbs [ /? | /o:FileName [ [ /w /c ] | [ /w /u ] ]
Note: Run in cscript mode if executed without arguments!
/? Displays this help message.
/o Redirects to the defined file.
FileName File name or full name where to redirect.
/w Overwrites the defined file. (Appends by default.)
/c Creates not existing file. (Not created by default.)
/u Writes to the file as UTF-16 with BOM instead of ANSI.
Default behavior changes in a way that a not existing
file will always be created. (Omitting /c has no effect.)
Steffen
Re: Batch code to paste from clipboard to a file?
Posted: 08 Jan 2018 13:54
by dvakdva
Hello aGerman
Dear Steffen,
i am grateful. It is working perfect.
Your character is honorable and i appreciate this. Thank you.
Best Regards