What I have done so far (displays both date and time on a WIN2K3 5.2.3790 machine with a leading space) :
SET DATETIME=(FOR /F %%C IN ('TIME/T') DO SET /P NOW=%%C : )&(FOR /F "TOKENS=2 DELIMS= " %%A IN ('DATE/T') DO SET /P NOW=%NOW% %%A <NUL)
%DATETIME%
I want to remove the leading space before the date that the set /p adds and add parameter for text but can't find a solution. I would also like to add a parameter for text as described here http://ss64.com/nt/syntax-macros.html once the set /p gets solved.
What I would like to get as a result (but with a batch macro instead of a call) :
#ECHO OFF
CALL :DATETIME "HELLO WORLD "
PAUSE>NUL
:DATETIME
FOR /F "TOKENS=*" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET NOW=%%B
FOR /F %%C IN ('TIME/T') DO SET /P NOW=%NOW% %%C %~1< NUL
Thank you,
I get the impression that the task is to echo a date and time and text on the same line:
echo %date% # %time% - "Hello World"
As you referenced the macros at ss64.com you should try to use the syntax shown there.
It's not necessary but a good way to use the %\n% formatting.
#ECHO OFF
set LF=^
::Above 2 blank lines are required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
SET DATETIME=( %\n%
FOR /F "TOKENS=*" %%A IN ('DATE/T') DO ( %\n%
FOR /F %%C IN ('TIME/T') DO SET /P "NOW=%%A%%C "^<NUL%\n%
)%\n%
)
set datet
echo ------
%DATETIME%
echo Hello World
Related
In batch I'm trying to split a string into substrings preferably based on a delimiter.
This example works but is there a better way to do this:
for /f "tokens=1-20 delims=:" %%a in (string.txt) do (
echo %%a& echo.%%b& echo.%%c& echo.%%d& echo.%%e& echo.%%f& echo.%%g& echo.%%h& echo.%%i& echo.%%j
echo.%%k& echo.%%l& echo.%%m& echo.%%n& echo.%%o& echo.%%p& echo.%%q& echo.%%r& echo.%%s& echo.%%t
) >substrings.txt
I have tried this code from Magoo which works for default delimiters but fails using colon:
#echo off
setLocal
for /f "delims=:" %%a in (string.txt) do (
for %%i in (%%a) do echo %%i >>substrings.txt
)
I have also tried this code from Aacini but it seems to fail using special characters.
#echo off
setlocal EnableDelayedExpansion
set i=1
set "x=%string%"
set "x!i!=%x::=" & set /A i+=1 & set "x!i!=%"
set x >substrings.txt
An example %string% var or string.txt is:
Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select
I am trying to output %string% var or string.txt to substring.txt as follows:
Select project
/option
report name="Sustainability"
option value="201"
Huonville
/option
/report
report name="Energy and Water Management"
option value="102"
Cygnet
/option
/report
report name="Tree Management"
option value="101"
Cygnet
/option
/report
option disabled="disabled"
/option
/select
Any help would be greatly appreciated. Thanks
When you use for /f, you need to know how many tokens there are (and there is a limit for max tokens).
You need a plain for loop, but you can't choose, which delimiter should be used.
So replace every delimiter with a space. Be sure to have quotes around each "token" to take care of original spaces. To do this, put quotes around the whole string and replace the delimter : with " ":
#echo off
setlocal enabledelayedexpansion
set "string=Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select""
set "string=%string: =#%"
set "string="%string::=" "%"
(for %%a in (%string%) do (
set "out=%%~a"
echo !out:#= !
))>out.txt
echo take a look at the modified string:
echo %string%
echo/
type out.txt
Another approach (just for academic reasons):
#echo off
setlocal
set "string=Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select"
call :recur %string%
goto :eof
:recur
rem echo loop with %*
for /f "tokens=1,* delims=:" %%a in ("%*") do (
echo %%a
if not "%%~b" == "" call :recur %%b
goto :eof
)
echo.
Note: there is a recursion limit (due to stack-size). It works with the given string, but may fail when the string contains more "tokens".
See #dbenham's AMAZING CERTUTIL string replacement technique. It works for any characters, even exclams!
#echo off
====SETLOCAL EnableDelayedExpansion EnableExtensions
set "B=^!"
set "C=^"
set ^"L=^
%===Line Feed===%
"
for /F "eol=N" %%C in ('wmic os get Name /value') do set "R=%%C" Carriage Return
set ^"E=!R!!L!" CRLF
>nul 2>&1 certutil -f -encodehex String.txt "%temp%\hex.txt" 4
pushd "%temp%"
>expand.txt (FOR /F "delims=" %%A in (hex.txt) do FOR %%B in (%%A) do (
set "char=%%B"
REM ! --> !B! ###
set "char=!char:21=21 42 21!"
REM ^ --> !C! ###
set "char=!char:5e=21 43 21!"
REM <CR> --> !R! ###
set "char=!char:0a=21 4c 21!"
REM <LF> --> !L! ###
set "char=!char:0d=21 52 21!"
REM : --> !R!!L! ###
set "char=!char:3a=21 45 21!"
echo(!char!
))
>nul 2>&1 certutil -f -decodehex expand.txt rawContent.txt
for /f delims^=^ eol^= %%A in (rawContent.txt) do set "modified=%%A"
del hex.txt expand.txt rawContent.txt
popd
>substring.txt (
echo(!modified!
)
This is a summary of what Stephan came up with.
#echo off
setlocal enabledelayedexpansion
set /p string=<"input.txt"
set "string=%string: =#%"
set "string="%string::=" "%"
(for %%a in (%string%) do (
set "output=%%~a"
echo !output:#= !
))>output.txt
Set string or input text file (modify line 3).
set /p string=<"input.txt"
The delimiter in this example is colon [:] which follows after [string:] (modify line 5).
set "string="%string::=" "%"
If string or input.txt contains quotes %string% may need to be double quoted (modify line 3).
for /f "delims=" %%a in (input.txt) do (set string="%%a")
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (input.txt) do (set string="%%a")
set "string=%string: =#%"
set "string="%string::=" "%"
(for %%a in (%string%) do (
set "output=%%~a"
echo !output:#= !
))>output.txt
This is code from a call above it. I extract line 5 from a file with this code which leaves it in %%c. I then want to take each word in that line (there can be from 1 to nth words) and create a markdown link like
[word](word.html)
and append those to a txt file. This is what I have so far. If I have 10 words do I really have to add all the tokens in the command like %%d, %%e, etc.? Can I use something like tokens=1-*. * meaning the last token in the line? Using tokens=* uses the whole line of words as one token I believe.
setlocal enabledelayedexpansion
set "lineNr=5"
set /a lineNr-=1
for /f "usebackq delims=" %%c in (`more +!lineNr! "%~1"`) DO (
for /f "tokens=1-2 delims= " %%d in ("%%c") do (
echo [%%d]^(tags/%%d.html^) [%%e]^(tags/%%e.html^) ^<br^> >> index.txt
)
goto :eof
)
Here is an example of how you can do that. Note I just used some parts as an example as I do not have time to format escape characters now, but you'll get the idea :)
#echo off
set "lineNr=5"
set /a lineNr-=1
for /f "usebackq delims=" %%c in (`more +%lineNr% "%~1"`) DO set "line=%%c"
for %%i in (%line%) do echo| set /p =[%%i](tags/%%i.html)>>index.txt
OK, I got it working by doing this -
set "lineNr=5"
set /a lineNr-=1
for /f "usebackq delims=" %%c in (`more +%lineNr% "overview.md"`) DO (
set "line=%%c"
goto :next
)
:next
for %%i in (%line%) do echo| set /p =[%%i](tags/%%i.html) >> index.txt
Have to break out of the loop line listing after the first line wanted(line 5).
I have the possibility to Highlight every letter of a Word with the space key.
The delimeter is a for /f Loop Option. The Output of the variablenmae Word, do not show empty spaces. For example the varaiblenames Word, content is THEWORDISPAT is shown with the used code. Until I try to Output A THEWORDISPAT. In such a case the Output is fused ATHEWORDISPAT. I can not Point to search for what I Need. A non english native user.
#echo off
setlocal enabledelayedexpansion
set logic=13
for /l %%a in (1,1,%logic%) do (call :grapefruit %%a)
goto :eof
goto :main
:grapefruit
set count=0
for %%z in (%1) do (
set /a count+=10%3
set var[!count!]=%%z)
:main
set token= THEWORDIZPAT
for /f "tokens=* delims=" %%b in ("%token%") do (set word=%%b)
set alien=!word:~%var[10]%,1!
for %%y in (%alien%) do (echo|set /p =%%y)
pause > nul
endlocal disabledelayedexpasion
Layouts for screen display is seperated in mainly two cases. First case languages and second case to define possible changes.
The solution below is possible but it is not with an upgraded example to the main example.
#echo off
setlocal enabledelayedexpansion
for /f %%A in ('echo prompt $H ^| cmd') do set BS=%%A
set "string=XA THEWORDIZPAT"
for /L %%A in (0,1,14) do (
for /f "tokens=* delims=X" %%b in ("!string:~%%A,1!") do (
set /p=%BS%%%b < nul)
pause > nul
)
I've been trying to make a batch file that makes folders named after the people on a .txt file list, and then gives them full access to modifying their own personal folder.
The problem is that I keep getting a ' delims= " was unexpected at this time' error.
Here's my code here, I was wondering if you guys would be able to find out what I did wrong, thanks ^-^
(Btw I haven't added the permissions part yet, I just need to get this part sorted out first)
CODE: http://pastebin.com/XLi11nZa
NAMES LIST: http://pastebin.com/xbh3WTSv
#echo off
color A
echo What is the name of list file? (Do not include format)
SET /P list=
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" %list%.txt | find /C ":""
for /f %%a in ('!cmd!') do set m=%%a
SET c=0
echo !m! folders to be created. Continue? (Y/N)
SET /P ANSWER=
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
exit
:yes
echo Now creating %m% folders.....
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
SET /a c = !c! + 1
mkdir "%%i"
echo !c!/%m% folders created [%%i]
)
endlocal
echo Now adding permissions to %m% folders.....
pause
setlocal enabledelayedexpansion
SET c1=0
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
SET /a c1 = !c1! + 1
SET word=1
SET /a showme=c1-1
SET showme=skip=%showme%
IF !c1! equ 1 set "showme= "
FOR /F "tokens=%word% %showme% delims= " %%F IN ("%list%") DO if defined
showme set showme=%%F
SET first=%showme:~0,1%
SET word=2
SET /a showme1=c1-1
SET showme1=skip=%showme1%
IF %c1% equ 1 set "showme1= "
FOR /F "tokens=%word% %showme1% delims= " %%L IN ("%list%") DO if
defined showme1 set showme1=%%L
set B=%showme1%%first%
set _STRING=%B%
set "_UCASE=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_LCASE=abcdefghijklmnopqrstuvwxyz"
for /l %%a in (0,1,25) do (
call set "_FROM=%%_UCASE:~%%a,1%%
call set "_TO=%%_LCASE:~%%a,1%%
call set "_STRING=%%_STRING:!_FROM!=!_TO!%%
)
set _STRING
echo %_STRING%
echo %_STRING%>>testing.txt
endlocal
pause
)
names list
Loralee Stucky
Tomas Silberman
Marleen Rosell
Phyllis Steier
Elmo Jetter
Kristyn Spruell
Willetta Vandermeer
Hazel Alsobrook
Naida Nixon
Nadia Godfrey
Lavonna Antunez
Mac Castile
Tamela Stover
Piedad Heidrick
Hien Welsh
Carolin Gularte
Mariko Tolentino
Alia Graddy
Deadra Rehkop
Donella Pittman
Replace
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
by
for /f "usebackq eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
twice.
Without the usebackq option, a double-quoted set of for /F is interpreted as a literal string rather than a file. Removing the double-quotes could work but could also lead to problems with files that have spaces in their names.
Another thing: you are dynamically creating the skip=# option for for /F, where # stands for a number. you have to make sure that this number is always positive, so 0 is not understood by for /F which could also lead to your error message.
So you could add an if check whether the number is greater than zero and do not add the skip option otherwise (by clearing your showme variables).
And last but not least: the delayed expansion is not always used properly: sometimes in the block startung at the for command in line #26 of your code and reaching to the end, you are not consistently using !! for expansion of variables showme, showme1 and c1, which are the ones that are modified within that code block.
The folders will get created proper if you remove the double quotes from the filename variable for /f "eol=; tokens=1 delims=," %%i in (%list%.txt) do ( change that line and you'll get past this part.
You may want to do the same in the permissions section.
I have a text file with text on each line. I would like to be able to put each line in one long line along with a space. So, if the text file has:
Bob
Jack
Sam
I want the result to be
Bob Jack Sam
Below are two methods that I am working on but I am stuck. Anything in brackets [] means that I know the syntax is completely wrong; I only put it there to show my thought process. The commented sections are just me experimenting and I have left them in case anyone wants to comment on what they would do / why they do what they do.
Method 1:
#echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
for %%a in (!count!) do (
!var%%a! = !var%%a! & " "
echo !var%%a!
)
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe !var%%a!-1
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
Method 2:
#echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
call echo %%var!count!%%
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe ^
[i = 1]
[for i to !count! do (]
call echo %%var!count!%% & " " & " "^
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
If you only have three values, you can directly retrieve them from the file
< input.txt (
set /p "line1="
set /p "line2="
set /p "line3="
)
set "var=%line1% %line2% %line3%"
echo("%var%"
If you don't know the number of values, use a for /f command to read the lines and concatenate the contents into a variable.
#echo off
setlocal enableextensions enabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do set "var=!var!%%a "
echo("%var%"
But if the data can contain exclamation signs, the delayed expansion state will remove them (and the text surounded by them). To avoid it, this can be used
#echo off
setlocal enableextensions disabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do (
setlocal enabledelayedexpansion
for /f "tokens=* delims=¬" %%b in ("¬!var!") do endlocal & set "var=%%b%%a "
)
echo("%var%"
where the setlocal/endlocal and the inner for are used to avoid problems with ! character
Or you can try something like this
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims=" %%a in ('
"<nul cmd /q /c "for /f "usebackq delims=" %%z in ("input.txt"^) do (set /p ".=%%z "^)""
') do set "var=%%a"
echo("%var%"
It runs a cmd instance to output the input lines as only one output line (<nul set /p is used to ouput the data without line feeds, so all data is in the same line). This is wrapped in a for to retrieve the output inside a variable
Would this work for you?
#echo off
SET var=
SETLOCAL EnableDelayedExpansion
FOR /f %%i in (afile.txt) DO (
SET var=!var!%%i
)
echo !var!
ENDLOCAL
Notice there is a space after the SET var=!var!%%i[Space Here] to separate each word in each row
EDIT:
If you want to display the current value in the loop just print %%i with NO concatenation. After the FOR loop finishes it will print the last value assigned to the variable
#echo off
SET var=
SETLOCAL EnableDelayedExpansion
for /f %%i in (input.txt) do (
SET var=%%i
echo !var!
)
echo %var%
Will print:
Bob
Jack
Sam
Sam
Try this: Post Edited.
#Echo off
Set "File=Recent_Log.txt"
Set "Output=My_Log.txt"
(
For /F %%F in (%File%) Do (
Set /p ".=%%~F "
)
) > "%Output%" < Nul
Goto :Eof
Because this is the first link after a Google search, I post my solution(Windows 7/10 .bat):
for %%f in (h:\blaba\*.*) do (type "%%f" & echo.) >> h:\sfsdf\dd.txt
NOTE: When your directory/filename contains spaces use double quotes.