I want to create a batch file able to apply some processing on each JPG file in a folder hierarchy. The following script file works very well for that case (here I only echo the name of each file, but this should be replaced by some more complex statements in the real application):
:VERSION 1
#echo off
set "basefolder=C:\Base"
for /r %basefolder% %%f in (*.jpg) do echo %%f
Actually, I don't want to explore all the folder hierarchy under %basefolder%, but only a given list of subfolders. This modified script is able to deal with that case :
:VERSION 2
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
pushd %basefolder%\%%~s"
for /r %%f in (*.jpg) do echo %%f
popd
)
Is there a solution to remove the pushd/popd pair of statements, to get something closer to the initial script. I thought that one of the following scripts would do the job:
:VERSION 3
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
for /r %basefolder%\%%~s" %%f in (*.jpg) do echo %%f
)
or, using delayed expansion:
:VERSION 4
#echo off
setlocal enabledelayedexpansion
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
set "folder=%basefolder%\%%~s"
echo !folder!
for /r !folder! %%f in (*.jpg) do echo %%f
)
but none of them is working. When running the second one, the echo !folder! command in the external loop shows C:\Base\A, C:\Base\B and C:\Base\C as expected, but the inner loop doesn't echo any JPG file, so I guess that the recursive for /r command does not run correctly.
What am I doing wrong ?
Final edit after answers :
Thanks to #aschipfl who provided a link to the answer posted by #jeb on another question, quoted below:
The options of FOR, IF and REM are only parsed up to the special character phase. Or better the commands are detected in the special character phase and a different parser is activated then. Therefore it's neither possible to use delayed expansion nor FOR meta-variables in these options.
In other words, my versions 3 and 4 do not work because when defining the root folder of the FOR /R command, neither the %%~s nor the !folder! are correctly expanded by the expression parser. There is no way to change that, as this is a parser limitation. As I said in a comment below: the root folder option in the FOR /R command is basically only syntactic sugar to avoid the use of pushd/popd before and after the command. As this syntactic sugar is incomplete, we have to stick to the original syntax for some specific use cases, as the one presented here. The alternatives proposed by #Gerhard (using a subroutine CALL) or by #Mofi (parsing the result of a DIR command) are working, but they are neither more readable nor more efficient than the simple pushd/popd version I proposed initially.
My Approach for this would be really straight forward:
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do for /R "%basedir%" %%a in ("%%~i\*.jpg") do echo %%~fa
The double quotes inside of the subfolders variable is important here, it will ensure that folder names with whitespace are not seen as separators for the folder names. For instance:
set "subfolders="Folder A","Folder B","Folder C""
Edit
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do call :work "%%~i"
goto :eof
:work
for /R "%basedir%\%~1" %%a in (*.jpg) do echo %%~fa
It is in general not advisable to assign the value of a loop variable to an environment variable and next use the environment variable unmodified without or with concatenation with other strings being coded in batch file or defined already above the FOR loop within body of a FOR loop. That causes just problems as it requires the usage of delayed expansion which results in files and folders with one or more ! are not correct processed anymore inside body of the FOR loop caused by double parsing of the command line before execution, or command call is used on some command lines, or a subroutine is used called with call which makes the processing of the batch file much slower.
I recommend to use this batch file for the task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "basefolder=C:\Base"
set "subfolders=A B C "Subfolder D" SubfolderE"
for %%I in (%subfolders%) do for /F "delims=" %%J in ('dir "%basefolder%\%%~I\*.jpg" /A-D /B /S 2^>nul') do echo %%J
endlocal
The inner FOR loop starts for each subfolder defined in subfolders in background one more command process with %ComSpec% /c and the DIR command line appended as additional arguments. So executed is with Windows installed to C:\Windows for example for the first subfolder:
C:\Windows\System32\cmd.exe /c dir "C:\Base\A\*.jpg" /A-D /B /S 2>nul
The command DIR searches
in specified directory C:\Base\A and all it subdirectories because of option /S
for files because of option /A-D (attribute not directory) including those with hidden attribute set
matching the pattern *.jpg in long or short file name
and outputs to handle STDOUT of background command process just the matching file names because of option /B (bare format)
with full path because of option /S.
The error message output by DIR on nothing found matching these criteria is redirecting from handle STDERR to device NUL to suppress it.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
The output to handle STDOUT of background command process is captured by FOR respectively the command process which is processing the batch file. FOR processes the captured output line by line after started cmd.exe terminated itself. This is very often very important. The list of files to process is already in memory of command process before processing the first file name. This is not the case on using for /R as this results in accessing file system, getting first file name of a non-hidden file matching the wildcard pattern, run all commands in body of FOR and accessing the file system once again to get next file name. The for /R approach is problematic if the commands in body of FOR change a file to process like deleting, moving, modifying, copying it in same folder, or renaming a found file because of the entries in file system changes while for /R is iterating over these entries. That can easily result in some files are skipped or some files are processed more than once and it could result also an endless running loop, especially on FAT file system like FAT32 or exFAT. It is never good to iterate over a list of files on which the list changes on each iteration.
Command FOR on usage of /F ignores empty lines which do not occur here. A non-empty line is split up into substrings using a normal space and a horizontal tab as string delimiters by default. This line splitting behavior is not wanted here as there could be full qualified file names containing anywhere inside full name one or more spaces. For that reason delims= is used to define an empty list of delimiters which disables the line splitting behavior.
FOR with option /F would also ignore lines on which first substring starts with ; which is the default end of line character. This is no problem here because of command DIR was used with option /S and so each file name is output with full path which makes it impossible that any file name starts with ;. So the default eol=; can be kept.
FOR with option /F assigns by default just first substring to specified loop variable as tokens=1 is the default. This default can be kept here as splitting the lines (full file names) into substrings is disabled already with delims= and so there is always the full file name assigned to the loop variable.
This example uses just echo %%I to output the file names with full path. But it is now safe to replace this single command by a command block which does more with the JPEG files because of the list of JPEG files for each specified subfolder tree in base folder is always already completely in memory of command process processing the batch file.
My problem is that two FOR loops are working separately, but don't want to work one after another.
The goal is:
The first loop creates XML files and only when the creation has already been done the second loop starts and counts the size of created XML files and writes it into .txt file.
#echo off
Setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s C:\Users\NekhayenkoO\test\') do (
echo Verarbeite %%~na
jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
)
for /f %%i in ('dir /b /s C:\Users\NekhayenkoO\outputxml\') do (
echo %%~ni %%~zi >> C:\Users\NekhayenkoO\outputxml\size.txt
)
pause
This question can be answered easily when knowing what jhove is.
So I searched in world wide web for jhove, found very quickly the homepage JHOVE | JSTOR/Harvard Object Validation Environment and downloaded also jhove-1_11.zip from SourceForge project page of JHOVE.
All this was done by me to find out that jhove is a Java application which is executed on Linux and perhaps also on Mac using the shell script jhove and on Windows the batch file jhove.bat for making it easier to use by users.
So Windows command interpreter searches in current directory and next in all directories specified in environment variable PATH for a file matching the file name pattern jhove.* having a file extension listed in environment variable PATHEXT because jhove.bat is specified without file extension and without path in the batch file.
But the execution of a batch file from within a batch file without usage of command CALL results in script execution of current batch file being continued in the other executed batch file without ever returning back to the current batch file.
For that reason Windows command interpreter runs into jhove.bat on first file found in directory C:\Users\NekhayenkoO\test and never comes back.
This behavior can be easily watched by using two simple batch files stored for example in C:\Temp.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do Test2.bat "%%I"
echo %~n0: Leaving %~f0
Test2.bat:
#echo %~n0: Arguments are: %*
#echo %~n0: Leaving %~f0
On running from within a command prompt window C:\Temp\Test1.bat the output is:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
The processing of Test1.bat was continued on Test2.bat without coming back to Test1.bat.
Now Test1.bat is modified to by inserting command CALL after do.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do call Test2.bat "%%I"
echo Leaving %~f0
The output on running Test1.bat from within command prompt window is now:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
Test2: Arguments are: "Test2.bat"
Test2: Leaving C:\Temp\Test2.bat
Test1: Leaving C:\Temp\Test1.bat
Batch file Test1.bat calls now batch file Test2.bat and therefore the FOR loop is really executed on all *.bat files found in directory of the two batch files.
Therefore the solution is using command CALL as suggested already by Squashman:
#echo off
setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s "%USERPROFILE%\test\" 2^>nul') do (
echo Verarbeite %%~na
call jhove.bat -m PDF-hul -h xml -o "%USERPROFILE%\outputxml\%%~na.xml" "%%a"
)
for /f %%i in ('dir /b /s "%USERPROFILE%\outputxml\" 2^>nul') do (
echo %%~ni %%~zi>>"%USERPROFILE%\outputxml\size.txt"
)
pause
endlocal
A reference to environment variable USERPROFILE is used instead of C:\Users\NekhayenkoO.
All file names are enclosed in double quotes in case of any file found in the directory contains a space character or any other special character which requires enclosing in double quotes.
And last 2>nul is added which redirects the error message output to handle STDERR by command DIR on not finding any file to device NUL to suppress it. The redirection operator > must be escaped here with ^ to be interpreted on execution of command DIR and not as wrong placed redirection operator on parsing already the command FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cd /?
dir /?
echo /?
for /?
And read also the Microsoft article Using command redirection operators.
You need to use the START command with the /WAIT flag when you launch an external application.
I believe it would look something like this:
START /WAIT jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
That should cause the batch file to pause and wait for the external application to finish before proceeding.
I have a list of files like these:
icone1.gif
icone2.gif
icone1.gif
icone11.gif
icone12.gif
icone13.gif
icone14.gif
icone15.gif
I want to remove 'e' from it so they will icon1.gif, icon2.gif and so on...
I tried this from the DOS Command Prompt:
ren icone*.gif icon*.gif
Didn't work.
Create a batch file in the folder by typing "notepad go.bat" at the command prompt and hitting enter and drop this in then save and exit notepad:
for %%i in ("*.gif") do (set fname=%%i) & call :rename
goto :eof
:rename
::Cuts off 1st five chars, then appends Icon and the remaining chars
ren "%fname%" "Icon%fname:~5%"
goto :eof
Double click the batch file in windows or from the command prompt type go and press enter
See How does the Windows RENAME (REN) command interpret wildcards? for rules that can explain why your command does not work.
Assuming all your file names have a digit after "icone" (really only care that you never have another "e" following "icone"), then the following one liner will work from the command line.
for /f "tokens=1* delims=eE" %A in ('dir /b icone*.gif') do #ren "%Ae%B" "%A%B"
Double up the percents if you put the command within a batch script.
For a really simple and robust solution, use my JREN.BAT regular expression renaming utility, pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
jren "^(icon)e(.*\.jpg)" "$1$2" /i
So I've been trying to create a batch file for a piece of software called DiscEX the software requires command line use from cmd.exe windows xp or higher the way it's initiated is like this discex (any arguments needed) location of iso file.
Now I can get the software to run using the batch file but I can't seem to figure out how to copy the target location of a file that was dragged onto it to open the batch file up
Here is what the batch file in notepad looks like.
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex
pause
also I need to be able to start in the working directory of a portable hard drive.
All you need to do is
C:\windows\system32\discex "%1"
to get a file path argument passed into the batch
If the batch file is in the working directory already, put
cd /d %~dp0 in the batch after #echo off
If you want to determine what drive is the external usb drive, use
#echo off
setlocal
set wmi='wmic logicaldisk where "volumeserialnumber='32A78F3B'" get caption'
for /f "skip=1 delims=" %%A in (%wmi%) do (
for /f "tokens=1 delims=:" %%B in ("%%A") do (set drive=%%B)
)
echo %drive%
where volumeserialnumber is the output from vol [driveletter of USB drive:] with the - removed.
When you drag a file on a batch file, the full file path is available in the first argument (%1) of the batch file. If you need this argument to be fed to the discex application as its first argument, you can do:
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex %1
pause
Any idea how to access the Windows clipboard using a batch file?
To set the contents of the clipboard, as Chris Thornton, klaatu, and bunches of others have said, use %windir%\system32\clip.exe.
Update 2:
For a quick one-liner, you could do something like this:
powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"
Capture and parse with a for /F loop if needed. This will not execute as quickly as the JScript solution below, but it does have the advantage of simplicity.
Updated solution:
Thanks Jonathan for pointing to the capabilities of the mysterious htmlfile COM object for retrieving the clipboard. It is possible to invoke a batch + JScript hybrid to retrieve the contents of the clipboard. In fact, it only takes one line of JScript, and a cscript line to trigger it, and is much faster than the PowerShell / .NET solution offered earlier.
#if (#CodeSection == #Batch) #then
#echo off
setlocal
set "getclip=cscript /nologo /e:JScript "%~f0""
rem // If you want to process the contents of the clipboard line-by-line, use
rem // something like this to preserve blank lines:
for /f "delims=" %%I in ('%getclip% ^| findstr /n "^"') do (
setlocal enabledelayedexpansion
set "line=%%I" & set "line=!line:*:=!"
echo(!line!
endlocal
)
rem // If all you need is to output the clipboard text to the console without
rem // any processing, then remove the "for /f" loop above and uncomment the
rem // following line:
:: %getclip%
goto :EOF
#end // begin JScript hybrid chimera
WSH.Echo(WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text'));
Old solution:
It is possible to retrieve clipboard text from the Windows console without any 3rd-party applications by using .NET. If you have powershell installed, you can retrieve the clipboard contents by creating an imaginary textbox and pasting into it. (Source)
Add-Type -AssemblyName System.Windows.Forms
$tb = New-Object System.Windows.Forms.TextBox
$tb.Multiline = $true
$tb.Paste()
$tb.Text
If you don't have powershell, you can still compile a simple .NET application to dump the clipboard text to the console. Here's a C# example. (Inspiration)
using System;
using System.Threading;
using System.Windows.Forms;
class dummy {
[STAThread]
public static void Main() {
if (Clipboard.ContainsText()) Console.Write(Clipboard.GetText());
}
}
Here's a batch script that combines both methods. If powershell exists within %PATH%, use it. Otherwise, find the C# compiler / linker and build a temporary .NET application. As you can see in the batch script comments, you can capture the clipboard contents using a for /f loop or simply dump them to the console.
:: clipboard.bat
:: retrieves contents of clipboard
#echo off
setlocal enabledelayedexpansion
:: Does powershell.exe exist within %PATH%?
for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
set getclip=powershell "Add-Type -AssemblyName System.Windows.Forms;$tb=New-Object System.Windows.Forms.TextBox;$tb.Multiline=$true;$tb.Paste();$tb.Text"
) else (
rem :: If not, compose and link C# application to retrieve clipboard text
set getclip=%temp%\getclip.exe
>"%temp%\c.cs" echo using System;using System.Threading;using System.Windows.Forms;class dummy{[STAThread]
>>"%temp%\c.cs" echo public static void Main^(^){if^(Clipboard.ContainsText^(^)^) Console.Write^(Clipboard.GetText^(^)^);}}
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!getclip!" "%%I" /nologo /out:"!getclip!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"
if not exist "!getclip!" (
echo Error: Please install .NET 2.0 or newer, or install PowerShell.
goto :EOF
)
)
:: If you want to process the contents of the clipboard line-by-line, use
:: something like this to preserve blank lines:
for /f "delims=" %%I in ('%getclip% ^| findstr /n "^"') do (
set "line=%%I" & set "line=!line:*:=!"
echo(!line!
)
:: If all you need is to output the clipboard text to the console without
:: any processing, then remove the above "for /f" loop and uncomment the
:: following line:
:: %getclip%
:: Clean up the mess
del "%temp%\getclip.exe" 2>NUL
goto :EOF
Slimming it down (on a new enough version of Windows):
set _getclip=powershell "Add-Type -Assembly PresentationCore;[Windows.Clipboard]::GetText()"
for /f "eol=; tokens=*" %I in ('%_getclip%') do set CLIPBOARD_TEXT=%I
First line declares a powershell commandlet.
Second line runs and captures the console output of this commandlet into the CLIPBOARD_TEXT enviroment variable (cmd.exe's closest way to do bash style backtick ` capture)
Update 2017-12-04:
Thanks to #Saintali for pointing out that PowerShell 5.0 adds Get-Clipboard as a top level cmdlets, so this now works as a one liner:
for /f "eol=; tokens=*" %I in ('powershell Get-Clipboard') do set CLIPBOARD_TEXT=%I
The clip command is good to pipe text to the clipboard, but it can't read from the clipboard. There is a way in vbscript / javascript to read / write the clipboard but it uses automation and an invisible instance if Internet Explorer to do it so its pretty fat.
The best tool I've found for working the clipboard from script is Nirsoft's free NirCmd tool.
http://www.nirsoft.net/utils/nircmd.html
Its like a swiss army knife of batch commands all in one small .exe file. For clipboard commands you would say someting like
nircmd clipboard [Action] [Parameter]
Plus you can directly refer to clipboard contents in any of its commands using its ~$clipboard$ variable as an argument. Nircmd also has commands in it to run other programs or commands from so it is possible to use it to pass the clipboard contents as an argument to other batch commands this way.
Clipboard actions:
set - set the specified text into the clipboard.
readfile - set the content of the specified text file into the clipboard.
clear - clear the clipboard.
writefile - write the content of the clipboard to a file. (text only)
addfile - add the content of the clipboard to a file. (text only)
saveimage - Save the current image in the clipboard into a file.
copyimage - Copy the content of the specified image file to the clipboard.
saveclp - Save the current clipboard data into Windows .clp file.
loadclp - Load Windows .clp file into the clipboard.
Note that most programs will always write a plain text copy to the clipboard even when they are writing a special RTF or HTML copy to the clipboard but those are written as content using a different clipboard format type so you may not be able to access those formats unless your program explicitly requests that type of data from the clipboard.
Best way I know, is by using a standalone tool called WINCLIP .
You can get it from here: Outwit
Usage:
Save clipboard to file: winclip -p file.txt
Copy stdout to clipboard: winclip -c Ex: Sed 's/find/replace/' file | winclip -c
Pipe clipboard to sed: winclip -p | Sed 's/find/replace/'
Use winclip output (clipboard) as an argument of another command:
FOR /F "tokens=* usebackq" %%G in ('winclip -p') Do (YOUR_Command %%G ) Note that if you have multiple lines in your clipboard, this command will parse them one by one.
You might also want to take a look at getclip & putclip tools: CygUtils for Windows but winclip is better in my opinion.
With Vista or higher, it's built in. Just pipe output to the "clip" program.
Here's a writeup (by me):
http://www.clipboardextender.com/general-clipboard-use/command-window-output-to-clipboard-in-vista
The article also contains a link to a free utility (written by Me, I think) called Dos2Clip, which can be used on XP.
EDIT: I see that I've gotten the question backwards, my solution OUTPUTS to the clipboard, doesn't read it. sorry!
Update: Along with Dos2Clip, is Clip2Dos (in the same zip), which will send the clipboard text to stdout. So this should work for you. Pascal source is included in the zip.
To retrive clipboard content from your batch script: there is no "pure" batch solution.
If you want an embed 100% batch solution, you will need to generate the other language file from your batch.
Here is a short and batch embed solution which generate a VB file (Usually installed on most windows)
Since all answers are confusing, here my code without delays or extra windows to open stream link copied from clipboard:
#ECHO OFF
//Name of TEMP TXT Files
SET TXTNAME=CLIP.TXT
//VBA SCRIPT
:: VBS SCRIPT
ECHO.Set Shell = CreateObject("WScript.Shell")>_TEMP.VBS
ECHO.Set HTML = CreateObject("htmlfile")>>_TEMP.VBS
ECHO.TXTPATH = "%TXTNAME%">>_TEMP.VBS
ECHO.Set FileSystem = CreateObject("Scripting.FileSystemObject")>>_TEMP.VBS
ECHO.Set File = FileSystem.OpenTextFile(TXTPATH, 2, true)>>_TEMP.VBS
ECHO.File.WriteLine HTML.ParentWindow.ClipboardData.GetData("text")>>_TEMP.VBS
ECHO.File.Close>>_TEMP.VBS
cscript//nologo _TEMP.VBS
:: VBS CLEAN UP
DEL _TEMP.VBS
SET /p streamURL=<%TXTNAME%
DEL %TXTNAME%
:: 1) The location of Player
SET mvpEXE="D:\Tools\Programs\MVP\mpv.com"
:: Open stream to video player
%mvpEXE% %streamURL%
#ECHO ON
Piping output to the clipboard is provided by clip, as others have said. To read input from the clipboard, use the pclip tool in this bundle. And there's tons of other good stuff in there.
So for example, you're going through an online tutorial and you want to create a file with the contents of the clipboard...
c:\>pclip > MyNewFile.txt
or you want to execute a copied command...
c:\>pclip | cmd
This might not be the exact answer, but it will be helpful for your Quest.
Original post:
Visit https://groups.google.com/d/msg/alt.msdos.batch/0n8icUar5AM/60uEZFn9IfAJ
Asked by Roger Hunt
Answered by William Allen
Much Cleaner Steps:
Step 1) create a 'bat' file named Copy.bat in desktop using any text editors and copy and past below code and save it.
#ECHO OFF
SET FN=%1
IF ()==(%1) SET FN=H:\CLIP.TXT
:: Open a blank new file
REM New file>%FN%
ECHO.set sh=WScript.CreateObject("WScript.Shell")>_TEMP.VBS
ECHO.sh.Run("Notepad.exe %FN%")>>_TEMP.VBS
ECHO.WScript.Sleep(200)>>_TEMP.VBS
ECHO.sh.SendKeys("^+{end}^{v}%%{F4}{enter}{enter}")>>_TEMP.VBS
cscript//nologo _TEMP.VBS
ECHO. The clipboard contents are:
TYPE %FN%
:: Clean up
DEL _TEMP.VBS
SET FN=
Step 2) create a Blank text file (in my case 'CLIP.txt' in H Drive) or anywhere, make sure you update the path in Copy.bat file under 'FN=H:\CLIP.txt' with your destination file path.
That's it.
So, basically when you copy any text from anywhere and run Copy.bat file from desktop, it updates CLIP.txt file with the Clipboard contents in it and saves it.
Uses:
I use it to transfer data from remotely connected machines where copy/paste is disabled between different connections; where shared drive (H:) is common to all Connections.
Multiple lines
The problem is resolved, but disappointment remains.
I was forced to split one command into two.
First of them well understands the text and service characters, but does not understand the backspace.
The second understands backspace, but does not understand many service characters.
Can anyone unite them?
Notepad ++, in the place where it should open, is commented out because sometimes the window does not get access to enter characters and therefore you need to make sure that it is active.
Of course, it is better to enter characters using the PID of the notebook process, but ...
The request from wmic opens for a long time, so do not close the notepad window until the bat file is closed.
#echo off
set "like=Microsoft Visual C++"
set "flag=0"
start /max C:\"Program Files\Notepad++\notepad++.exe" -nosession -multiInst
( set LF=^
%= NEWLINE =%
)
set ^"NL=^^^%LF%%LF%^%LF%%LF%^^"
::------------------
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%i in ( `wmic /node:"papa" product where "Name like '%%%like%%%'" get * ^| findstr /r /v "^$"`) do (
for /f tokens^=1^ delims^=^" %%a in ("%%i") do set str=%%a
if "!flag!"=="0" (
::start /max C:\"Program Files\Notepad++\notepad++.exe" -nosession -multiInst& set "flag=1"
for /f "delims=" %%i in ('mshta "javascript:new ActiveXObject('WScript.Shell').SendKeys('{BS}{BS}{BS}{BS}');close(new ActiveXObject('Scripting.FileSystemObject'));"') do set
set flag=1
)
#set /P "_=%%str%%"<NUL|clip
for /f "delims=" %%i in ('mshta "javascript:new ActiveXObject('WScript.Shell').SendKeys('^v');close(new ActiveXObject('Scripting.FileSystemObject'));"') do set
(echo %%NL%%)|clip
for /f "delims=" %%i in ('mshta "javascript:new ActiveXObject('WScript.Shell').SendKeys('^v');close(new ActiveXObject('Scripting.FileSystemObject'));"') do set
)
setlocal disabledelayedexpansion
Here's one way with mshta:
#echo off
:printClip
for /f "usebackq tokens=* delims=" %%i in (
`mshta "javascript:Code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text'))));"`
) do (
echo cntent of the clipboard:
echo %%i
)
the same thing for accessing it directly from console:
for /f "usebackq tokens=* delims=" %i in (`mshta "javascript:Code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(clipboardData.getData('Text'))));"`) do #echo %i