Windows Batch - read result from cmd command and save to a variable - windows

I would like to read the hostname of my device and generate a file with such name. However, I am new in Windows Batch, I don't even understand how to use of those variable, read file......
Here is what I want to do:
CD C:\WINDOWS\SYSTEM32\
CMD.EXE hostname -> to a HostName variable e.g. called abc ::I hope it will save my computer name to a variable string
echo It success to function >> C:\%abc%.txt :: I hope it can generate a file with the sting in variable
PAUSE
I have thought that if it is not function with cmd command, so I tried the alternative method:
CD C:\WINDOWS\SYSTEM32\
CMD.EXE hostname >> C:\HostName.tmp
-AND THEN READ BACK the C:\HostName.tmp Here
-And then save to a variable again e.g.
echo It success to function >> C:\HostName_variable.txt ::It generate a file with the sting in variable
PAUSE
It seems not successful =[.
Added:
I would like to create a file with the name of hostname. e.g. if my hostname is "abc", it will output a file as "abc.csv" something like this.

You can get it two ways:
Environment variables are a few pre-configured variables containing general system state, and one of them is COMPUTERNAME, which you can send straight to a file like:
echo %COMPUTERNAME% > hostname_file.txt
Or you can store it in another variable, first, then echo the content of that variable to a file:
set myHostName=%HOSTNAME%
echo %myHostName% > hostname_file.txt
The other way is running the hostname.exe program which prints the current computer name, and redirect the output to a file:
hostname > hostname_file.txt
It's harder to run hostname.exe and store the result in a variable, you need to abuse a for loop to do that:
for /f %f in ('hostname') do set myHostName=%f
echo %myHostName% > hostname_file.txt
NB. What you are doing isn't quite how you describe it - a file isn't the same thing as a variable:
You write:
CD C:\WINDOWS\SYSTEM32\
CMD.EXE hostname >> HostName_variable ::It will save my computer name to a variable string
echo It success to function >> C:\HostName_variable.txt ::It generate a file with the string in variable
PAUSE
cmd.exe is unnecessary because you're already in a command prompt when you run it, you don't need another one to run. You don't need to change to c:\windows\system32 first, because Windows always looks there for programs to run. >> is adding to the end of a file if it exists, it won't always create a new file, > will always create a new file and if one already exists, will overwrite it. Once it has sent the output to a file, it's not in a variable.

Related

How to save cmd output as file name

I'm running a command line script on multiple PC's and i'm trying to save username as a file name so i can see who's information i'm viewing later on.
In the command line script i run Whoami and i'd like to save it as "user"."file type". I'm trying to do this in a command line script because I always do it manually in command line and am trying to automate this process so I can do it faster.
If you know how to do it in a better way do share.
whoami > test.txt
tells it to go to a file and "test.txt" will be generated wherever your CMD CurDir is.
You may use Windows Environment variables %USERNAME% and possibly %USERDOMAIN% if the domain is needed.
%USERNAME% does not return the domain by itself.
Full list of standard environment variables: How-to: Windows Environment Variables
Use these in the command as needed. For example:
dir > %USERNAME%.txt
If you need the domain in there:
dir > %USERDOMAIN%_%USERNAME%.txt
(using _ to separate domain and username instead of \ since filename cannot contain \)
Remember to use >> instead of > if you don't want the file to be overwritten each time the command is run.
You may want to direct errors and standard output as needed: Redirecting error messages from Command Prompt: STDERR/STDOUT

Batch file variable in an environment variable

I have a program named go.exe, and I pass it a file name example so that it can make a log file named example_golog.txt.
The problem here is that I want to only call the executable without explicitly giving a filename or making a user have to give a file name.
My solution to this was to name a system variable called GO whose value is go.exe %~n0. What's wrong with this is instead of the %~n0 part getting the file name of the batch file that called it, my go.exe file just makes a text file called %~n0_golog.txt.
Is there any way around this so that %~n0 will do it's magic in the batch files that call it in go.exe %~n0?
EDIT:
My system variable:
Name: GO
Value: go %~n0
My test.bat file:
#echo on
%GO%
pause
When I run test.bat, the %~n0 does not expand out.
So instead of test_golog.txt being made, %~n0_golog.txt is made
In your batch file, you can call it to dereference the variable.
echo %go% will report go %~n0.
call echo %go% will report go test (from a batch file named test).
You can use whatever other sets you need from there.

Make System information Batch file

How to create a batch file about system info and ip address and domain server etc. _ which will generate a output as txt file
You can put whatever Windows commands you want in a batch file and then redirect the output to a text file using >. E.g., you could create a batch file called test.bat and put the following commands in it:
#echo off
systeminfo
ipconfig /all
Then you could run test.bat from a command prompt as follows:
C:>test >outfile.txt
The output of the batch file, which would consist of the output from the commands you placed in it, would be redirected to a file named outfile.txt by the redirect operator, >. Note: it can sometimes take awhile for the output of the systeminfo command to complete, so, depending on your system, you may need to give it a minute to complete.
Alternatively, you could add the >outfile.txt at the end of commands within the batch file itself, though for every instance after the first one you need to use double greater than signs, i.e., >>, to append the output rather than wiping out what was in outfile.txt previously and creating a new version of that text file. E.g., you could have the following in test.bat:
#echo off
systeminfo >outfile.txt
ipconfig /all >>outfile.txt
You would then just use the following at a command prompt:
C:>test

Windows batch file, save contents of file dowloaded by wget to a variable

Currently, I have a batch file that uses wget to read a file from the server. Is there any way for wget to save the contents of that file to a variable and then for the batch file to take a certain action based on the value of the variable?
The peseduo-code would probably look something like this. I am very new to batch files and am still learning the semantics:
SAVE RESULT OF wget http://www.theserver.com/instruction TO VARIABLE: the_variable
IF %the_variable% == 'restart' <DO SOME ACTION HERE>
I will base this answer on the assumption that your downloaded file contains text strings.
If this is the case then it is possibile to use the FOR command in this way:
for /F %I IN (instruction.txt) DO if %I==restart #echo RESTART FOUND
This command opens the file "instruction.txt" and parse it assigning each word to the variable %I
Then for each value of variable %I executes the command specified after the keyword DO.
In this case I have compared the variabile %I to the string "restart" and if the result is true the batch execs the command #echo RESTART FOUND
You could use the GOTO: function in your batch file. So that if the variable is equal to a certain value/string it jumps to a particular section in the batch file and carries out the code in that section.
Almost like using methids in Object Orientated Programming.
CHeck this link out :-
http://www.robvanderwoude.com/goto.php

Is it possible to pass a variable into a Windows FTP script file?

I have a batch script that dynamically creates some files and generates four files with somewhat random filenames based on the time, date, etc. It then needs to upload that file to a server via FTP.
As of right now, my .bat file has a line like "ftp -s:ftp.txt". Ftp.txt contains some fairly straightforward FTP script stuff: something like this--
open ftp.myserver.com
username
password
put filename1.dat
put filename2.dat
put filename3.dat
put filename4.dat
What I'd like to do is pass in the filenames that need to be uploaded and then replace the "put filename1.dat" with "put %file1%"--where %file1% is the filename variable being passed in.
Is this possible? Anybody know how to do it? Or is my whole approach wrong?
You could generate the ftp.txt file on the fly with your bat file. Simply do something like :
echo ftp.myserver.com>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo put filename1.dat>>ftp.txt
echo put filename2.dat>>ftp.txt
echo put filename3.dat>>ftp.txt
echo put filename4.dat>>ftp.txt
ftp -s:ftp.txt
Of course now that you are in the bat file you can use environment variables and other stuff in place of "filenameX.dat"
For example :
echo put %file1% >>ftp.txt
To update the answer, here's an updated batch file. Note the exclusion of the space after the username and password (as it passes a space to the ftp site). This will also utilize an argument passed to the batch file.
echo open ftp.myserver.com>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo put %1>>ftp.txt
echo quit>>ftp.txt
ftp -s:ftp.txt
I don't think the command line FTP client in windows is smart enough to do environment/shell variable interpolation on the commands. But you could have the controlling .bat file generate the ftp script dynamically, and do the variable stuff while you're still at the shell level.

Resources