CMD Set only reading first letter in text file - variables

Using the following command Set /p out=<out.txt I do not get the text in the file, what is returned is just a symbol and the first letter in the file.
I've tried using Set out=more out.txt but that ends up just placing the command "more out.txt" into the script instead of what's in the text file.
https://i.stack.imgur.com/yU9A1.jpg

It looks that the out.txt is saved using UTF-16 encoding with BOM. Unlike type command, the < redirection operator does not understand this. The following cmd command should do the trick:
set out=&for /f "delims=" %G in ('type out.txt') do #if not defined out set out=%G
To use the FOR command in a batch script (.bat or .cmd extension), specify %%G instead
of %G as follows:
#ECHO OFF
set "out="
for /f "delims=" %%G in ('type out.txt') do if not defined out set out=%%G

Related

Saving command into variable

I've question about this variables.How can I store result of a command into a variable? For example: saving drive serial into location variable.
thanks.
E.g:
#ECHO OFF
SET location=vol
ECHO We're working with %location%
for /f "delims=" %%x in ('your command and parameters') do set "var=%%x"
where the 'single quotes' are required around the command from which you wish to save output.
Edit - now we know which command.
Comment - one variable can contain up to ~8,000 characters.
Here's a routine that will set the values in $1...$whatever and the entire set in $all with each field enclosed in angle-brackets.
#ECHO OFF
SETLOCAL
SET "$all="
FOR /f "tokens=1*delims=:" %%a IN (
'systeminfo 2^>nul^|findstr /n /r "$"'
) DO (
SET "$%%a=%%b"
FOR /f "tokens=*" %%c IN ("%%b") DO CALL SET "$all=%%$all%%<%%c>"
SET /a max=%%a
)
SET $
pause
FOR /l %%z IN (1,1,%max%) DO CALL ECHO %%$%%z%%
GOTO :EOF
thank you...please see my batch file contents:
for /f "delims=" %%x in ('systeminfo') do set "var=%%x"
command_line.exe %var%
only last line of 'systeminfo' saved in var variable and it is:
"data execution perevention available: yes"
I want to store all contetnt of systeminfo into variable!
thank you.I'm too basic
do you mean result of systeminfo is into all variable? at the end of your code can I add this command:
start command_line.exe $%all%
is this true?
can you put complete code for my batch file?

Batch file variable value not available until outside of loop

I am trying to write a batch file that will search through a directory for *.pdf file extensions then convert them to *.tif file extensions with ImageMagic. I am able to do this if there is one PDF file in the directory, but if there are more than one I can't figure out how to convert them with the correct name. The problem is that within the loop, the fName variable doesn't appear to be assigned, but outside the loop, it has a value...
Here is the code that works for a single PDF file and works for multiple, but the name containts ".pdf":
echo off
set dSource=C:\Users\Nick\Documents\Research\Journal Article\Figures
set fType=*.pdf
for /f "delims=" %%f in ('dir /a-d /b "%dSource%\%fType%"') do (
rem remove extension from file name, set value to variable:
set fName=%%~nf
rem call ImageMagic to convert to TIFF
rem convert -compose copy -density 300 -alpha off "%%f" "%%f.tif"
rem above line (when uncommented) lets multiple TIFF images to be produced, but they are *.pdf.tif
rem convert -compose copy -density 300 -alpha off "%fName%.pdf" "%fName%.tif"
rem above line (when uncommented) does not work because fName has no value...
rem variable value does not appear to be assigned within loop:
echo.file name within loop: %fName%
)
echo.file name after loop: %fName%
rem outside loop, variable value is now available...
rem convert -compose copy -density 300 -alpha off "%fName%.pdf" "%fName%.tif"
rem above line of code works, but only for the last file name with *.pdf discovered in directory
pause
Enable delayed expansion if you need to set a variable and use it in the same loop, and then refer to the variable in the loop using ! instead of %:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set dSource=C:\Users\Nick\Documents\Research\Journal Article\Figures
set fType=*.pdf
for /f "delims=" %%f in ('dir /a-d /b "%dSource%\%fType%"') do (
set fName=%%~nf
echo fName in the loop: !fName!
)
echo fName out of the loop: %fName%
Google "batch delayed expansion" for details.
In your case a solution is to replace this:
convert -compose copy -density 300 -alpha off "%%f" "%%f.tif"
with this:
convert -compose copy -density 300 -alpha off "%%f" "%%~dpnf.tif"
rem or "%%~nf.tif" to save them all in the current folder.
The drawback with delayed expansion is that a ! character becomes more difficult to process in a filename or path or string.

Insert Input into a variable

I'm working on a batch script for fun and learning.
I've set up multiple choises, e.g Fint IP address, MAC address...and so on.
The problem is that i can't find out how i can insert the output of those two lines into variables.
for /f "usebackq skip=1" %%f in (`wmic COMPUTERSYSTEM get name`) do ???
for /f "usebackq skip=1" %%f in (`wmic COMPUTERSYSTEM get domain`) do ???
So I can use the output in a sentence like;
Your DNS-name is %dns_name_output% and your domain is %domain_name_output%.
try this:
for /f %%f in ("%computername%") do set "name=%%f"
for /f %%f in ("%userdomain%") do set "domain=%%f"
echo %name% %domain%
To capture wmic get output, use the /value flag like this
for /f "tokens=2 delims==" %%A in ('wmic computersystem get name /value') do set "Name=%%A"
for /f "tokens=2 delims==" %%A in ('wmic computersystem get domain /value') do set "Domain=%%A"
The problem you are seeing arises from the fact that WMIC output is in unicode. By some mechanism that I do not fully understand (a bug?), the FOR /F command transforms the unicode command output into ASCII, but mysteriously appends an extra carriage return (<CR>) at the end of each line.
FOR /F does not return empty lines, but the mysterious and seemingly blank lines are not really blank - they contain a <CR>.
Even if the extra lines are properly ignored, the last value in the list will include an unwanted <CR> that is included when assigning the value to an environment variable. The <CR> will not be apparent if the variable is later expanded normally using %VAR% because the command parser automatically strips all <CR> characters. But the <CR> is preserved and can cause problems if delayed expansion !VAR! is used.
The FOR /F command strips the last character from each line if it happens to be a <CR>. So passing the value through an extra FOR /F will eliminate the problem. David Ruhman's suggestion to use the /value switch is a good one, and can be improved upon. Multiple values may be requested in one loop, and the property name can be used as the variable name. Having only one name/value pair per line eliminates potential parsing problems with spaces and or commas in values.
The commas in the WMIC command must either be escaped or quoted when used within FOR /F. In this case, quoting the entire command seems easiest. The following will properly define two environment variables - Domain, and Name:
for /f "delims=" %%A in ('"wmic computersystem get domain, name /value"') do (
for /f "tokens=1* delims==" %%B in ("%%A") do set "%%B=%%C"
)
echo Your host name is %name% and your domain is %domain%
This works using wmic without any odd spaces or CRs embedded.
#echo off
for /f "tokens=2 delims=<>" %%a in ('wmic COMPUTERSYSTEM get name /format:htable^|find "hidden"') do set "name=%%a"
for /f "tokens=2 delims=<>" %%a in ('wmic COMPUTERSYSTEM get domain /format:htable^|find "hidden"') do set "dom=%%a"
echo "%name%,%dom%"
pause
You can define a variable, and assign it's value to the result of a command using back-tick enclosure.
Try this:
a=`ls`
echo $a

How to Get Substring of #FNAME in forfiles command

I want to rename a bunch of files in a directory by stripping their file names down to the first 16 characters but I cannot work out how to get a substring of #FNAME when using the forfiles command in a batch file.
Help would be greatly appreciated.
Thanks
Roland
Using forfiles to do this is most likely going to be quite tricky, as I don't think you can set and use variables in the same batch, because you spawn a new cmd for each file ("cmd /c command").
However, the same functionality can easily be achieved using a simple for loop.
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir C:\yourdir /s /b') do (
set file=%%~na
set ext=%%~xa
set new=!file:~0,16!
ren %%a !new!!ext!
)
That will recurse through all subdirectories as well. If you just want the folder you choose, remove the /s from the dir command.
Also, because you are stripping just the file name, I have made it so it will append the file extension back on after, otherwise the characters in the extension would count towards the 16 character limit.
You can use a second bat file and pass #fname like %1 and make the substring here.
Forfiles /m *.* /c "cmd /c call second.bat #fname"
In second.bat
Set v=%1
Set substring=%v:~1,5%
Echo %substring%

Findstr, how to read part of string/line

I have file with a list of names and extensions, formatted such as below each on it's own line:
JoeBloggs=102
JohnSmith=109
What I want to do is use findstr but read the number after the equals sign. So I am using the following command:
#echo off
for /F "delims=" %%a in ('findstr /p %username% extensions.txt') do set ext=%%a
If a user logs on as JoeBloggs it will capture JoeBloggs=102, what I want it to do is only capture 102. So essentially only the numbers after the equals sign.
#echo off
for /f "tokens=1,2 delims==" %%a in (names.txt) do (
if "%%a"=="%username%" set ext=%%b
)
echo %ext%
pause >nul
This will read each line of your text file and split it when it comes across an = sign.
I have specified to use tokens 1 and 2, 1 being before the split, and 2 being after so we can compare the first and if it's what you want, use the second.