I have a script which search for file name which is in the format "abc2014.txt" in a particular folder. I am then counting the number of such files which have *2014 in their name. But if that file is not found then on command prompt it gives output as "File not found".
My script is:
#echo off
SetLocal enabledelayedexpansion
for /F "tokens=1" %%a IN ('Dir "C:\Users\BOX\*2014*" /-C/S/A:-D') Do Set q=!n2! & Set n2=%%a
echo %q%
I don't want this "File not found" output. How do I suppress this "File not found" output? If there is no file there, then I want blank output.
How to achieve this?
Just redirect standard error (2) of 'dir' command to nul.
#echo off
SetLocal enabledelayedexpansion
for /F "tokens=1" %%a IN ('Dir "C:\Users\BOX\*2014*" /-C/S/A:-D 2^>nul') Do Set q=!n2! & Set n2=%%a echo %q%
But if you want to count files with certain characters in the name:
#echo off
echo Files number:
Dir "C:\Users\BOX\*2014*" /-C/S/A:-D 2^>nul | find /C /V ""
setlocal enableDelayedExpansion
for /F "tokens=1" %%a IN ('Dir "C:\Users\BOX\*2014*" /-C/S/A:-D 2^>nul') Do (
Set q=!n2!
Set n2=%%a echo %q%
)
endlocal
Related
I am trying to write a batch script that recursively lists all directories and their files with *.js type in the below format:
For example, if I start with the C:\project directory
c:\project
project.js
project_time.js
c:\project\core
core.js
core_render.js
core_application.js
I tried to implement the above logic in code as follows:
#echo off
for /r %%f in (*.js) do (
echo %%f >> names.txt
)
pause
I was not able to print the directory under which the files are listed.
#echo off
setlocal disabledelayedexpansion
set "lastdir="
( for /r %%A in (*.js) do (
set "nextdir=%%~dpA"
setlocal enabledelayedexpansion
if /i not "!lastdir!" == "!nextdir!" (
rem Empty line and directory path.
if defined lastdir #echo(
#echo !nextdir!
)
endlocal
rem Filename.
#echo %%~nxA
set "lastdir=%%~dpA"
)
) > "names.txt"
The lastdir variable is to record the last directory path so it is echoed only once.
If lastdir is different to %%~dpA:
If lastdir is defined, then an empty line will be echoed.
Directory path of found file is echoed.
Filename is always echoed.
for modifiers dp is the drive and path. nx is the name and extension.
setlocal enabledelayedexpansion is used only where needed so paths with ! are not vulnerable.
I am not going to suggest a command line solution as it would be very long. Instead suggest use of tree command if the output format is suitable.
Here's an untested example, (I'm not expecting it to be quick if your base directory is large):
#Echo Off
(
For /F "Delims=" %%G In ('Dir /B /S /A:D "C:\Project" 2^> NUL') Do (
%__AppDir__%where.exe /Q "%%G":*.js 1> NUL 2> NUL
If Not ErrorLevel 1 (
Echo/
Echo %%G
For /F "EOL=| Delims=" %%H In ('%__AppDir__%where.exe "%%G":*.js')Do (
Echo %%~nxH
)
)
)
) 1> "names.txt"
Pause
If you prefer to run something from the Command Prompt, then try this version:
(For /F "Delims=" %G In ('Dir /B/S/AD "C:\Project" 2^>NUL')Do #(%__AppDir__%where.exe /Q "%G":*.js >NUL 2>&1&&(Echo/&Echo %G&For /F "EOL=|Delims=" %H In ('%__AppDir__%where.exe "%G":*.js')Do #Echo %~nxH)))>"names.txt"
Two simple ways to do this:
dir /S *.js
You get the answers, just as you requested.
FORFILES /S /M *.js /C "cmd /c echo #path"
You get complete path for every file.
This question already has answers here:
Assign output of a program to a variable using a MS batch file
(12 answers)
How to set commands output as a variable in a batch file [duplicate]
(9 answers)
Closed 9 months ago.
I'm looking to get the result of a command as a variable in a Windows batch script (see how to get the result of a command in bash for the bash scripting equivalent). A solution that will work in a .bat file is preferred, but other common windows scripting solutions are also welcome.
The humble for command has accumulated some interesting capabilities over the years:
D:\> FOR /F "delims=" %i IN ('date /t') DO set today=%i
D:\> echo %today%
Sat 20/09/2008
Note that "delims=" overwrites the default space and tab delimiters so that the output of the date command gets gobbled all at once.
To capture multi-line output, it can still essentially be a one-liner (using the variable lf as the delimiter in the resulting variable):
REM NB:in a batch file, need to use %%i not %i
setlocal EnableDelayedExpansion
SET lf=-
FOR /F "delims=" %%i IN ('dir \ /b') DO if ("!out!"=="") (set out=%%i) else (set out=!out!%lf%%%i)
ECHO %out%
To capture a piped expression, use ^|:
FOR /F "delims=" %%i IN ('svn info . ^| findstr "Root:"') DO set "URL=%%i"
If you have to capture all the command output you can use a batch like this:
#ECHO OFF
IF NOT "%1"=="" GOTO ADDV
SET VAR=
FOR /F %%I IN ('DIR *.TXT /B /O:D') DO CALL %0 %%I
SET VAR
GOTO END
:ADDV
SET VAR=%VAR%!%1
:END
All output lines are stored in VAR separated with "!".
But if only a single-line console-output is expected, try:
#ECHO off
#SET MY_VAR=
FOR /F %%I IN ('npm prefix') DO #SET "MY_VAR=%%I"
#REM Do something with MY_VAR variable...
#John: is there any practical use for this? I think you should watch PowerShell or any other programming language capable to perform scripting tasks easily (Python, Perl, PHP, Ruby)
To get the current directory, you can use this:
CD > tmpFile
SET /p myvar= < tmpFile
DEL tmpFile
echo test: %myvar%
It's using a temp-file though, so it's not the most pretty, but it certainly works! 'CD' puts the current directory in 'tmpFile', 'SET' loads the content of tmpFile.
Here is a solution for multiple lines with "array's":
#echo off
rem ---------
rem Obtain line numbers from the file
rem ---------
rem This is the file that is being read: You can replace this with %1 for dynamic behaviour or replace it with some command like the first example i gave with the 'CD' command.
set _readfile=test.txt
for /f "usebackq tokens=2 delims=:" %%a in (`find /c /v "" %_readfile%`) do set _max=%%a
set /a _max+=1
set _i=0
set _filename=temp.dat
rem ---------
rem Make the list
rem ---------
:makeList
find /n /v "" %_readfile% >%_filename%
rem ---------
rem Read the list
rem ---------
:readList
if %_i%==%_max% goto printList
rem ---------
rem Read the lines into the array
rem ---------
for /f "usebackq delims=] tokens=2" %%a in (`findstr /r "\[%_i%]" %_filename%`) do set _data%_i%=%%a
set /a _i+=1
goto readList
:printList
del %_filename%
set _i=1
:printMore
if %_i%==%_max% goto finished
set _data%_i%
set /a _i+=1
goto printMore
:finished
But you might want to consider moving to another more powerful shell or create an application for this stuff. It's stretching the possibilities of the batch files quite a bit.
you need to use the SET command with parameter /P and direct your output to it.
For example see http://www.ss64.com/nt/set.html. Will work for CMD, not sure about .BAT files
From a comment to this post:
That link has the command "Set /P
_MyVar=<MyFilename.txt" which says it will set _MyVar to the first line
from MyFilename.txt. This could be
used as "myCmd > tmp.txt" with "set
/P myVar=<tmp.txt". But it will only
get the first line of the output, not
all the output
Example to set in the "V" environment variable the most recent file
FOR /F %I IN ('DIR *.* /O:D /B') DO SET V=%I
in a batch file you have to use double prefix in the loop variable:
FOR /F %%I IN ('DIR *.* /O:D /B') DO SET V=%%I
I would like to add a remark to the above solutions:
All these syntaxes work perfectly well IF YOUR COMMAND IS FOUND WITHIN THE PATH or IF THE COMMAND IS A cmdpath WITHOUT SPACES OR SPECIAL CHARACTERS.
But if you try to use an executable command located in a folder which path contains special characters then you would need to enclose your command path into double quotes (") and then the FOR /F syntax does not work.
Examples:
$ for /f "tokens=* USEBACKQ" %f in (
`""F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe"" Hello '"F:\GLW7\Distrib\System\Shells and scripting"'`
) do echo %f
The filename, directory name, or volume label syntax is incorrect.
or
$ for /f "tokens=* USEBACKQ" %f in (
`"F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe" "Hello World" "F:\GLW7\Distrib\System\Shells and scripting"`
) do echo %f
'F:\GLW7\Distrib\System\Shells' is not recognized as an internal or external command, operable program or batch file.
or
`$ for /f "tokens=* USEBACKQ" %f in (
`""F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe"" "Hello World" "F:\GLW7\Distrib\System\Shells and scripting"`
) do echo %f
'"F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe"" "Hello' is not recognized as an internal or external command, operable program or batch file.
In that case, the only solution I found to use a command and store its result in a variable is to set (temporarily) the default directory to the one of command itself :
pushd "%~d0%~p0"
FOR /F "tokens=* USEBACKQ" %%F IN (
`FOLDERBROWSE "Hello world!" "F:\GLW7\Distrib\System\Layouts (print,display...)"`
) DO (SET MyFolder=%%F)
popd
echo My selected folder: %MyFolder%
The result is then correct:
My selected folder: F:\GLW7\Distrib\System\OS install, recovery, VM\
Press any key to continue . . .
Of course in the above example, I assume that my batch script is located in the same folder as the one of my executable command so that I can use the "%~d0%~p0" syntax. If this is not your case, then you have to find a way to locate your command path and change the default directory to its path.
NB: For those who wonder, the sample command used here (to select a folder) is FOLDERBROWSE.EXE. I found it on the web site f2ko.de (http://f2ko.de/en/cmd.php).
If anyone has a better solution for that kind of commands accessible through a complex path, I will be very glad to hear of it.
Gilles
Just use the result from the FOR command. For example (inside a batch file):
for /F "delims=" %%I in ('dir /b /a-d /od FILESA*') do (echo %%I)
You can use the %%I as the value you want. Just like this: %%I.
And in advance the %%I does not have any spaces or CR characters and can be used for comparisons!!
If you're looking for the solution provided in Using the result of a command as an argument in bash?
then here is the code:
#echo off
if not "%1"=="" goto get_basename_pwd
for /f "delims=X" %%i in ('cd') do call %0 %%i
for /f "delims=X" %%i in ('dir /o:d /b') do echo %%i>>%filename%.txt
goto end
:get_basename_pwd
set filename=%~n1
:end
This will call itself with the result of the CD command, same as pwd.
String extraction on parameters will return the filename/folder.
Get the contents of this folder and append to the filename.txt
[Credits]: Thanks to all the other answers and some digging on the Windows XP commands page.
#echo off
ver | find "6.1." > nul
if %ERRORLEVEL% == 0 (
echo Win7
for /f "delims=" %%a in ('DIR "C:\Program Files\Microsoft Office\*Outlook.EXE" /B /P /S') do call set findoutlook=%%a
%findoutlook%
)
ver | find "5.1." > nul
if %ERRORLEVEL% == 0 (
echo WinXP
for /f "delims=" %%a in ('DIR "C:\Program Files\Microsoft Office\*Outlook.EXE" /B /P /S') do call set findoutlook=%%a
%findoutlook%
)
echo Outlook dir: %findoutlook%
"%findoutlook%"
You can capture all output in one variable, but the lines will be separated by a character of your choice (# in the example below) instead of an actual CR-LF.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /b') do (
if "!DIR!"=="" (set DIR=%%i) else (set DIR=!DIR!#%%i)
)
echo directory contains:
echo %DIR%
Second version, if you need to print the contents out line-by-line. This takes advanted of the fact that there won't be duplicate lines of output from "dir /b", so it may not work in the general case.
#echo off
setlocal EnableDelayedExpansion
set count=0
for /f "delims=" %%i in ('dir /b') do (
if "!DIR!"=="" (set DIR=%%i) else (set DIR=!DIR!#%%i)
set /a count = !count! + 1
)
echo directory contains:
echo %DIR%
for /l %%c in (1,1,%count%) do (
for /f "delims=#" %%i in ("!DIR!") do (
echo %%i
set DIR=!DIR:%%i=!
)
)
#echo off
setlocal EnableDelayedExpansion
FOR /F "tokens=1 delims= " %%i IN ('echo hola') DO (
set TXT=%%i
)
echo 'TXT: %TXT%'
the result is 'TXT: hola'
You should use the for command, here is an example:
#echo off
rem Commands go here
exit /b
:output
for /f "tokens=* useback" %%a in (`%~1`) do set "output=%%a"
and you can use call :output "Command goes here" then the output will be in the %output% variable.
Note: If you have a command output that is multiline, this tool will set the output to the last line of your multiline command.
Please refer to this http://technet.microsoft.com/en-us/library/bb490982.aspx which explains what you can do with command output.
I try to improve an overview of my search and find memories for batch files in Windows XP command prompt environment.
In order to my previous sentence I am not happy with my search possibilities and have to post a question.
I try to compare the names of some text files and have written words in a text file that are by reading the same. With such a start environment I wrote following batch script to get an echo output.
The aim is
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & echo !var!
rem The output is the name of the files without extension. Now my question:
rem Is it possible to compare the above file names with some input
rem from a text file, for example like:
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & for /f %%a in (Textfile.txt) do (if !var!==%%a echo good else echo search)
rem That returns no output. I would like to know if there are possibilities
rem to do that? And if it is possible, how to revise this batch file?
endlocal disabledelayedexpansion
pause
Have a nice day, wishes
Stefan
This should work with Latin characters - some foreign characters may not work:
#echo off
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder\*.*" ') do find /i "%%~nb" < "textfile.txt" >nul && (echo "%%~nb" found) || (echo "%%~nb" not found)
pause
proper formatting you code increases readabilty:
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder"') do (
for /f %%a in (textfile.txt) do (
if "%%~nb"=="%%a" ( echo good ) else ( echo search )
)
)
I added a /b to the dircommand (show name only, no date/time/attributes) and a /a-d to exclude directorynames.
You don't need to use a variable (!var!) here (but you can, it works fine).
I am new to batch files and have some basic understanding and can edit batches if I see them in front of me.
I wonder if you could help me with the following requirement for my batch file.
Is there a file in a directory with a modified date of today
If yes
Is there a certain text string of ‘test’ in the file
If yes
echo ‘test string found’
Else
echo ‘test string NOT found’
Else
Echo ‘no file today’
Do not put the batch in the searched folder (the batch file contains the test string!):
#echo off &setlocal
:: first modify the search path here!
set "searchpath=."
set "text=test"
dir /a-d | find "%date%" >nul || (echo no file today&goto :eof)
for /f "tokens=3*" %%i in ('dir /a-d %searchpath% ^| find "%date%"') do (
for /f %%k in ('findstr /mc:"%text%" "%searchpath%\%%j"') do (
if not "%%k"=="" set "found=true"
)
)
if not defined found (echo test string NOT found) else echo test string found
endlocal
Edit: this version is for European date format, e.g. dd.mm.yyyy.
With each of the commands I list here, you can do commandname /? for more information.
The 2nd token of the %date% environment variable and the date /t command display today's date in the same format as a dir listing.
The dir command has an available /a switch which allows displaying only files with (or without) a specific attribute. To exclude directories (such as . and ..) use dir /a:-d.
You can capture the output of commands using for /f.
Finally, you can test for the existence of a string using either the find or the findstr command. findstr lets you search using regular expressions for a little more flexibility. But if you just want to search for a literal string, find will work just fine.
Put all that together:
#echo off
rem "setlocal" prevents any variables you set within your batch script
rem from remaining as environment orphans after the script completes.
setlocal
rem set variable %today% as the second token of %date%
for /f "tokens=2" %%I in ("%date%") do set today=%%I
rem dir list, skip directories, skip this batch script, include only files with a date matching %today%
for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%"') do (
rem record success for later
set found=1
rem search file %%I for "string" (case-insensitive).
find /i "string" "%%I">NUL
rem Was last command successful?
if %ERRORLEVEL%==0 (
echo Test string found
) else (
echo Test string NOT found
)
)
rem if success was not recorded
if not defined found echo No file today
Then when you start to see your coding more as a means of artistic expression than a means to an end, you can perform more advanced trickery to perform the same task with fewer lines of code.
#echo off
setlocal
for /f "tokens=2" %%I in ("%date%") do set today=%%I
for /f "tokens=4*" %%H in ('dir /a-d ^| findstr /v /i "%~nx0$" ^| find "%today%" ^|^| echo No file today 1^>^&2') do (
(find /i "string" "%%I" >NUL && (
echo Test string found.
)) || echo Test string not found.
)
#ECHO OFF
SETLOCAL
:: Point to the target directory. establish target file and string required
SET target=.
SET targfile=example.txt
SET targtxt=FIND me
::
SET targfile="%target%\%targfile%"
IF NOT EXIST %targfile% ECHO No file today& GOTO :EOF
:dfloop
SET filename=q%random%.q$q
SET fullname="%target%\%filename%"
IF EXIST %fullname% GOTO dfloop
:: create a dummy file dated today
COPY nul: %fullname% >nul
FOR /f %%i IN ('dir %fullname% ^|find /i "%filename%"') DO SET today=%%i
DEL %fullname%
FOR /f %%i IN ('dir %targfile% ') DO IF %today%==%%i (SET today=)
IF DEFINED today ECHO No file today&GOTO :eof
FIND /i "%targtxt%" %targfile% >NUL
IF ERRORLEVEL 1 (ECHO test string NOT found) ELSE (ECHO test string found)
I've set the target directory to . - the current directory - for testing. Replace the . with your target directory name.
Looks for a string in the target file 'example.txt'
Now - that assumes that you are looking for a specific filename that may appear/be modified in a directory such as ...\myappdir\myapp.log
If you mean "any file [matching some filemask] that was created/modified today" is to be searched for the string then a slightly modified routine:
#ECHO OFF
SETLOCAL
:: Point to the target directory. establish target filemask and string required
SET target=\destdir
SET targfile=examp*.txt
SET targtxt=FIND me
::
SET targfile="%target%\%targfile%"
IF NOT EXIST %targfile% ECHO No file today& GOTO :EOF
:dfloop
SET filename=q%random%.q$q
SET fullname="%target%\%filename%"
IF EXIST %fullname% GOTO dfloop
:: create a dummy file dated today
COPY nul: %fullname% >nul
FOR /f %%i IN ('dir %fullname% ^|find /i "%filename%"') DO SET today=%%i
DEL %fullname%
SET count=0
FOR /f %%i IN ('dir %targfile% ') DO IF %today%==%%i (SET /a count+=1)
IF %count% equ 0 ECHO No file today&GOTO :eof
FOR /f "tokens=1*delims=:" %%i IN (
'dir /b/a-d/o-d %targfile%^|findstr /n /v "*" '
) DO IF %%i leq %count% (
FIND /i "%targtxt%" "%target%\%%j" >NUL
IF NOT ERRORLEVEL 1 (SET today=)
)
IF DEFINED today (ECHO test string NOT found) ELSE (ECHO test string found)
Here, any file matching examp*.txt that has been created/modified today in \destdir will be examined for the target string.
This question already has answers here:
Assign output of a program to a variable using a MS batch file
(12 answers)
How to set commands output as a variable in a batch file [duplicate]
(9 answers)
Closed 9 months ago.
I'm looking to get the result of a command as a variable in a Windows batch script (see how to get the result of a command in bash for the bash scripting equivalent). A solution that will work in a .bat file is preferred, but other common windows scripting solutions are also welcome.
The humble for command has accumulated some interesting capabilities over the years:
D:\> FOR /F "delims=" %i IN ('date /t') DO set today=%i
D:\> echo %today%
Sat 20/09/2008
Note that "delims=" overwrites the default space and tab delimiters so that the output of the date command gets gobbled all at once.
To capture multi-line output, it can still essentially be a one-liner (using the variable lf as the delimiter in the resulting variable):
REM NB:in a batch file, need to use %%i not %i
setlocal EnableDelayedExpansion
SET lf=-
FOR /F "delims=" %%i IN ('dir \ /b') DO if ("!out!"=="") (set out=%%i) else (set out=!out!%lf%%%i)
ECHO %out%
To capture a piped expression, use ^|:
FOR /F "delims=" %%i IN ('svn info . ^| findstr "Root:"') DO set "URL=%%i"
If you have to capture all the command output you can use a batch like this:
#ECHO OFF
IF NOT "%1"=="" GOTO ADDV
SET VAR=
FOR /F %%I IN ('DIR *.TXT /B /O:D') DO CALL %0 %%I
SET VAR
GOTO END
:ADDV
SET VAR=%VAR%!%1
:END
All output lines are stored in VAR separated with "!".
But if only a single-line console-output is expected, try:
#ECHO off
#SET MY_VAR=
FOR /F %%I IN ('npm prefix') DO #SET "MY_VAR=%%I"
#REM Do something with MY_VAR variable...
#John: is there any practical use for this? I think you should watch PowerShell or any other programming language capable to perform scripting tasks easily (Python, Perl, PHP, Ruby)
To get the current directory, you can use this:
CD > tmpFile
SET /p myvar= < tmpFile
DEL tmpFile
echo test: %myvar%
It's using a temp-file though, so it's not the most pretty, but it certainly works! 'CD' puts the current directory in 'tmpFile', 'SET' loads the content of tmpFile.
Here is a solution for multiple lines with "array's":
#echo off
rem ---------
rem Obtain line numbers from the file
rem ---------
rem This is the file that is being read: You can replace this with %1 for dynamic behaviour or replace it with some command like the first example i gave with the 'CD' command.
set _readfile=test.txt
for /f "usebackq tokens=2 delims=:" %%a in (`find /c /v "" %_readfile%`) do set _max=%%a
set /a _max+=1
set _i=0
set _filename=temp.dat
rem ---------
rem Make the list
rem ---------
:makeList
find /n /v "" %_readfile% >%_filename%
rem ---------
rem Read the list
rem ---------
:readList
if %_i%==%_max% goto printList
rem ---------
rem Read the lines into the array
rem ---------
for /f "usebackq delims=] tokens=2" %%a in (`findstr /r "\[%_i%]" %_filename%`) do set _data%_i%=%%a
set /a _i+=1
goto readList
:printList
del %_filename%
set _i=1
:printMore
if %_i%==%_max% goto finished
set _data%_i%
set /a _i+=1
goto printMore
:finished
But you might want to consider moving to another more powerful shell or create an application for this stuff. It's stretching the possibilities of the batch files quite a bit.
you need to use the SET command with parameter /P and direct your output to it.
For example see http://www.ss64.com/nt/set.html. Will work for CMD, not sure about .BAT files
From a comment to this post:
That link has the command "Set /P
_MyVar=<MyFilename.txt" which says it will set _MyVar to the first line
from MyFilename.txt. This could be
used as "myCmd > tmp.txt" with "set
/P myVar=<tmp.txt". But it will only
get the first line of the output, not
all the output
Example to set in the "V" environment variable the most recent file
FOR /F %I IN ('DIR *.* /O:D /B') DO SET V=%I
in a batch file you have to use double prefix in the loop variable:
FOR /F %%I IN ('DIR *.* /O:D /B') DO SET V=%%I
I would like to add a remark to the above solutions:
All these syntaxes work perfectly well IF YOUR COMMAND IS FOUND WITHIN THE PATH or IF THE COMMAND IS A cmdpath WITHOUT SPACES OR SPECIAL CHARACTERS.
But if you try to use an executable command located in a folder which path contains special characters then you would need to enclose your command path into double quotes (") and then the FOR /F syntax does not work.
Examples:
$ for /f "tokens=* USEBACKQ" %f in (
`""F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe"" Hello '"F:\GLW7\Distrib\System\Shells and scripting"'`
) do echo %f
The filename, directory name, or volume label syntax is incorrect.
or
$ for /f "tokens=* USEBACKQ" %f in (
`"F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe" "Hello World" "F:\GLW7\Distrib\System\Shells and scripting"`
) do echo %f
'F:\GLW7\Distrib\System\Shells' is not recognized as an internal or external command, operable program or batch file.
or
`$ for /f "tokens=* USEBACKQ" %f in (
`""F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe"" "Hello World" "F:\GLW7\Distrib\System\Shells and scripting"`
) do echo %f
'"F:\GLW7\Distrib\System\Shells and scripting\f2ko.de\folderbrowse.exe"" "Hello' is not recognized as an internal or external command, operable program or batch file.
In that case, the only solution I found to use a command and store its result in a variable is to set (temporarily) the default directory to the one of command itself :
pushd "%~d0%~p0"
FOR /F "tokens=* USEBACKQ" %%F IN (
`FOLDERBROWSE "Hello world!" "F:\GLW7\Distrib\System\Layouts (print,display...)"`
) DO (SET MyFolder=%%F)
popd
echo My selected folder: %MyFolder%
The result is then correct:
My selected folder: F:\GLW7\Distrib\System\OS install, recovery, VM\
Press any key to continue . . .
Of course in the above example, I assume that my batch script is located in the same folder as the one of my executable command so that I can use the "%~d0%~p0" syntax. If this is not your case, then you have to find a way to locate your command path and change the default directory to its path.
NB: For those who wonder, the sample command used here (to select a folder) is FOLDERBROWSE.EXE. I found it on the web site f2ko.de (http://f2ko.de/en/cmd.php).
If anyone has a better solution for that kind of commands accessible through a complex path, I will be very glad to hear of it.
Gilles
Just use the result from the FOR command. For example (inside a batch file):
for /F "delims=" %%I in ('dir /b /a-d /od FILESA*') do (echo %%I)
You can use the %%I as the value you want. Just like this: %%I.
And in advance the %%I does not have any spaces or CR characters and can be used for comparisons!!
If you're looking for the solution provided in Using the result of a command as an argument in bash?
then here is the code:
#echo off
if not "%1"=="" goto get_basename_pwd
for /f "delims=X" %%i in ('cd') do call %0 %%i
for /f "delims=X" %%i in ('dir /o:d /b') do echo %%i>>%filename%.txt
goto end
:get_basename_pwd
set filename=%~n1
:end
This will call itself with the result of the CD command, same as pwd.
String extraction on parameters will return the filename/folder.
Get the contents of this folder and append to the filename.txt
[Credits]: Thanks to all the other answers and some digging on the Windows XP commands page.
#echo off
ver | find "6.1." > nul
if %ERRORLEVEL% == 0 (
echo Win7
for /f "delims=" %%a in ('DIR "C:\Program Files\Microsoft Office\*Outlook.EXE" /B /P /S') do call set findoutlook=%%a
%findoutlook%
)
ver | find "5.1." > nul
if %ERRORLEVEL% == 0 (
echo WinXP
for /f "delims=" %%a in ('DIR "C:\Program Files\Microsoft Office\*Outlook.EXE" /B /P /S') do call set findoutlook=%%a
%findoutlook%
)
echo Outlook dir: %findoutlook%
"%findoutlook%"
You can capture all output in one variable, but the lines will be separated by a character of your choice (# in the example below) instead of an actual CR-LF.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /b') do (
if "!DIR!"=="" (set DIR=%%i) else (set DIR=!DIR!#%%i)
)
echo directory contains:
echo %DIR%
Second version, if you need to print the contents out line-by-line. This takes advanted of the fact that there won't be duplicate lines of output from "dir /b", so it may not work in the general case.
#echo off
setlocal EnableDelayedExpansion
set count=0
for /f "delims=" %%i in ('dir /b') do (
if "!DIR!"=="" (set DIR=%%i) else (set DIR=!DIR!#%%i)
set /a count = !count! + 1
)
echo directory contains:
echo %DIR%
for /l %%c in (1,1,%count%) do (
for /f "delims=#" %%i in ("!DIR!") do (
echo %%i
set DIR=!DIR:%%i=!
)
)
#echo off
setlocal EnableDelayedExpansion
FOR /F "tokens=1 delims= " %%i IN ('echo hola') DO (
set TXT=%%i
)
echo 'TXT: %TXT%'
the result is 'TXT: hola'
You should use the for command, here is an example:
#echo off
rem Commands go here
exit /b
:output
for /f "tokens=* useback" %%a in (`%~1`) do set "output=%%a"
and you can use call :output "Command goes here" then the output will be in the %output% variable.
Note: If you have a command output that is multiline, this tool will set the output to the last line of your multiline command.
Please refer to this http://technet.microsoft.com/en-us/library/bb490982.aspx which explains what you can do with command output.