I want to echo the result of dir comamnd in the command prompt.
How do I do that?
I've already tried:
echo dir
echo $dir
an example of this shown in another stackoverflow post is.
FOR /F "delims=" %i IN ('date /t') DO set today=%i
echo %today%
Displays todays current day.
u could replace that for dir.
Related
on linux i can make looping result from select mariadb like this.
data=$(mysql -uroot -pdemo -sNe "select name from employee;");
for datas on $data
do
printf "${datas}";
done
how to do that on windows (.bat or powershell). I have experience looping but result from dir like this.
set mysqlDataDir="D:\wamp\bin\mysql\mysql5.7.14\data"
pushd "%mysqlDataDir%"
for /d %%f in (*) do (
echo processing folder "%%f"
)
popd
i got answer from this question
Set a return value as variable in window batch
#echo off
setlocal
set "sql=c:\xampp\mysql\bin\mysql.exe"
for /f "usebackq" %%a in (`%sql% -u root -ss -e "select COUNT^(*^) FROM browser" form_generator`) do set TESTVAR=%%a
if %TESTVAR% LSS 1 echo whatever
pause
Apologies as I am new and may be asking a obvious question.
echo #off
echo starting load of West One Data Extract v1g
FOR /F %%I IN ('DIR \\<server name>\Data\Servicing\Data\abc*.* /B /O:D') DO SET NEWEST_FILE_1=%%I
echo Uploading "%NEWEST_FILE_1%"
As file name is "abc 123.xls" output echo is "uploading abc"
If I run DIR \\<server name>\Data\Servicing\Data\abc*.* /B /O:D it works fine and results in "abc 123.xls"
Can you please help as to how to change the code to accept space in file names.
By default the FOR /F command will parse the output of a command and split it up into tokens based on the default delimiters. This is all explained in the help file for the FOR command. So you need to use the DELIMS option to tell it not to use any delimiters.
FOR /F "delims=" %%I IN ('DIR \\<server name>\Data\Servicing\Data\abc*.* /B /O:D') DO SET "NEWEST_FILE_1=%%I"
If you have a variable inside this for /f command, it doesn't get printed out to file.
setlocal EnableDelayedExpansion
for /F %%i in ('dir /B') do (
set duration=asdf
echo %duration% > file.txt
)
Contents of file.txt: Echo is ON.
If you move set duration=asdf out of for /f wrap and put it below setlocal line it works correct and prints asdf into file.txt
Why? How do I do this with the set variable inside the for command?
This is due to delayed expansion, which stops variables from being set and used, in the same command.
You are getting Echo is ON. in file.txt, because to the command interpreter it's reading the echo command as:
echo > file.txt
Which as documented by echo /?
Type ECHO without parameters to display the current echo setting.
The simplest fix is to replace the % with !
Updated script:
setlocal EnableDelayedExpansion
for /F %%i in ('dir /B') do (
set duration=asdf
echo !duration! > file.txt
)
Good Day, I'm still new in DOS Commands, I want to if still possible. I have a .bat that I want to call a value from a text file. What I mean I'm doing a .bat file (generic) that will use by normal user by changing only the value from the text file.
Example:
I have this command in my bat file: (It's just an example)
#ECHO OFF
echo First Name:
echo Kobe
echo LastName:
echo Bryant
pause
Instead of editing the .bat file or setting the variable on .bat file. I want to create a set.txt that will hold the variables.
Example:
set.txt
FirstName=Kobe
LastName=Bryant
So the .bat file will be generic/flexible. No need to change.
run.bat
echo First Name:
echo $FirstName
echo Last Name:
echo $LastName
Any tips on how I will do this?
Thanks.
The following code would make the output fully dynamic, in case you don't even know the names of the variables.
#echo off
setlocal disableDelayedExpansion
for /f "delims== tokens=1*" %%A in (set.txt) do (
set "%%A=%%B"
echo %%A:
echo %%B
)
If your file name includes spaces or poison characters like & or ^, then you would want to quote the filename, in which case you would need to add the usebackq option.
for /f "usebackq delims== tokens=1*" %%A in ("name with space.txt") do ...
Be sure in the set.txt there are no spaces surrounding the = sign:
#echo off
for /f "usebackq tokens=* delims=" %%# in ("set.txt") do (
set "%%#"
)
echo First Name:
echo %FirstName%
echo Last name:
echo %LastName%
For /f "tokens=1,2 delims==" %%A in (file.txt) do Echo %A %B
Is how to. See for /?.
I have the batch file below and wish to convert it to a single line cmd /c command.
#ECHO OFF
SETLOCAL
FOR /F "TOKENS=2 DELIMS=:." %%I IN ('chcp') DO SET _codepage_=%%I
SET _codepage_=Cp%_codepage_: =%
ECHO %_codepage_%
ENDLOCAL
I tried:
cmd /c FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO #SET _codepage_=%I && #SET _codepage_=Cp%_codepage_: =% && #ECHO %_codepage_%
also tried:
cmd /v:on /c FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO #SET _codepage_=%I && #SET _codepage_=Cp%_codepage_: =% && #ECHO !_codepage_!
but neither approach works! Can anyone help me out here?
cmd /q/c"for /f "tokens=2delims=:." %a in ('chcp')do for %b in (%a)do echo(Cp%b"
What did the original code do? split the output of chcp, remove the ending dot (I didn't knew it, thank you), storing the value inside a variable to remove a space from it and prefix it with Cp and then echo the variable
What does this code do? split the output of chcp, remove the ending dot, but instead of using a variable to remove the space, an aditional for loop is used over the value from the first loop, and, instead of prefixing the variable, the Cp is included in the data echoed.
I would use the MC ND answer. But just for yucks, I created another solution.
cmd /v:on /q /c "(for /f "delims=." %a in ('chcp') do for %b in (%a) do set cp=Cp%b) & echo !cp!"
Note that for all solutions, you might consider adding the /D option to prevent autorun commands from running.
It seems I was missing double quotes around the cmd command. This is how I finally did this:
cmd /v:on /q /c "FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO SET _codepage_=%I && SET _codepage_=Cp!_codepage_: =! && ECHO !_codepage_!"
also correct as MC ND pointed out above:
cmd /q /c "FOR /F "TOKENS=2 DELIMS=:." %a IN ('chcp') DO FOR %b IN (%a) DO ECHO Cp%b"
I use this single command in Java with a ProcessBuilder in order to get the console's codepage and pass it to the process's stdout and stderr streams like this:
String encoding = getConsoleEncoding(); // use the above cmd command
BufferedReader stdout = new BufferedReader( new InputStreamReader(p.getErrorStream(),encoding) );
So the result of system commands like e.g. date /t will display correctly in Java.
You can save above into a file, name it as my.bat. Then use:
cmd.exe /c "call my.bat"
For In one command line, not use batch file, you can:
FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO (SET _codepage1_=%I && SET _codepage_=Cp%_codepage1_% && ECHO %_codepage_%)