Text file not appearing in correct path location when executing batch script - windows

It's my first time writing scripts for a college course I'm taking. I'm trying to write a simple batch script that creates a new folder, saves all the system IP configurations to a text file INSIDE that folder, and then pings the network and appends the text file with the results. any reason why this code
#echo off
md tempinfo
ipconfig /all > C:\Users\MyUser\tempinfo\output.txt
ping (IP address) >> C:\Users\MyUser\tempinfo\output.txt
is creating a tempinfo folder with nothing in it? I know the ipconfig and ping commands are working but I cannot get the text file to show up inside the tempinfo folder.

You can write something like that :
#echo off
If not exist "%UserProfile%\tempinfo" MD "%UserProfile%\tempinfo"
(
ipconfig.exe /all
ping.exe 127.0.0.1
)>"%UserProfile%\tempinfo\output.txt"
if exist "%UserProfile%\tempinfo\output.txt" Start "" /MAX "%UserProfile%\tempinfo\output.txt"

Related

Automating connection to VNC with batch file

I'm trying to create a batch file to automate vnc connections, this is what i came up with:
#echo off
:Begin
set "PASS=123"
set /p IP=Enter IP Address:
echo Connecting...
start /d "C:\Program Files (x86)\UltraVNC\" VNCVIEWER.EXE %IP%
goto Begin
problem is that the program comes up with a second pop up display for the password which is always 123 but i don't know how to make that automatic too, once the process is open how do i make the batch file enter the password as well automatically ?

Windows batch file using start not placing multiple programs in background

I am trying to get data from a sql server and ldap server for multiple clients. I need to get the sql data first and then the ldap data. In Unix shell it was straight forward to make a loop around a sub process with both retrievals going on for each client and then wait for it to complete. As a windows batch file however it happens sequentially. I.e. until I retrieve the data for one client, it won't go to the next. How can I get each client's data simultaneously? Here's what I have:
REM Get DB and client info. from config file
for /F "tokens=1,2,3,4,5,6 delims=| eol=#" %%G in (%cfg%\%env%.data) do (
REM Mark file as being in process of receiving data
type nul > %%K.tmp
REM Get data and remove tmp file to indicate completion
start cmd /C sqlcmd .... -Q "A long query" ^> %%K.dat1 && "c:\Program Files\Softerra\LDAP Administrator 4\laimex.exe" ... /sql "Another query" > %%K.dat2 && del %%K.tmp
)
For some reason, I need to do the first redirect escaped as ^> while the later one doesn't need that. At this point I am assuming that everything will be retrieved in the background and that I will need to check afterwards for when the processes are complete which I would do by checking the existence of the zero byte temp files I create. What happens though is that each iteration through the loop only starts when the prior one completes rather than occurring straight away by being placed in the background. Can anyone suggest how I can fix this?
You need to escape the && as well (^&^&), otherwise it executes everything after it as soon as start is fired. Example:
1 gets executed in a new shell correctly, while 2 takes over the main window (not what you want).
start cmd /C ping 127.0.0.1 && ping 127.0.0.2
Both get executed one after the other in a new window.
start cmd /C ping 127.0.0.1 ^&^& ping 127.0.0.2
Same as above, another way to do it.
start cmd /C "ping 127.0.0.1 && ping 127.0.0.2"
Also escape the other >'s, this might work:
start cmd /C sqlcmd .... -Q "A long query" ^> %%K.dat1 ^&^& "c:\Program Files\Softerra\LDAP Administrator 4\laimex.exe" ... /sql "Another query" ^> %%K.dat2 ^&^& del %%K.tmp

A way to Pass a Variable to a Networked path Batch file and Execute

Going to lay this out the best I can and see if someone here can help me out a bit.
Here is my Code .bat on the Remote Server.
echo off
title SystemPlatzAll
set /p input=
findstr %input% SysPlatzAll.log >> Result.txt
%SystemRoot%\explorer.exe "Result.txt"
pause
What im trying to achieve is sending the %input% across the network to this batch file then excute and in return have the file save to the computer it is on. To which then the user will get the file opened from a shared folder i have on the drive.
can I use PsExec to send this over or is there another way?
I can get the .bat to execute with the following.
psexec \\HIFRP010.ad.foo.com -u hoem\hoemfooprod -p !foounit123 -e -h -accepteula -i 0 -d F:\Public\Logfiles\Systemplatz\foo\SystemPlatzBackup1.1\Final\NextTest.bat
pause
The above code will execute the Program.
But I want to know how or if it is possible to instead send %input% from one .bat to another.
Thank you in advance.
It is possible. I am not familiar with psexec, but you may be able to encapsulate the last parameter in doublequotes as is common on windows and pass input params directly on the command line. I do this using runas fairly often.
"F:\Public\Logfiles\Systemplatz\foo\SystemPlatzBackup1.1\Final\NextTest.bat paramFoo paramBar"

Why is the following multiple DOS commands in one line not working correctly? (Passing Values)

I am trying to create a text file via DOS commands. The commands asks one for the name of the file prior to creating it. I have looked here and here and elsewhere to get me started.
I would like the code to work in one line. So, I should be able to type the entire code in Windows Start > Run box.
This is what I have:
cmd /k #ECHO OFF & SET /P filename=What File name: & copy NUL %filename%.txt & :End
This however ignores the name of the file I gave when asked, and creates %filename%.txt.
I have tried changing the operator before the word copy to |, &&, and & but these don't even ask me for a file name and simply create %filename%.txt
Also, the cmd box stays open after the text file is created.
P.S. I know I can use /q before /k for echo off.
I look forward to your help.
This works here: delayed expansion is used in another cmd process
cmd /c #ECHO OFF & SET /P filename=What File name: & cmd /v /c copy NUL !filename!.txt & exit
The command line has no path defined for the directory and when using the RUN box it will probably be created in the c:\windows\system32 folder, except you will not have write access to that folder.

How to display my PC's IP Address on Windows (7) Startup?

I am on a work computer with dynamic IP address (Ipv4), which usually changes when I restart it. Since I sometimes need to use this PC through remote desktop connection at home I like to keep its IP address handy. But I sometimes forget to check the IP at restart, so is there a batch file or some code which can start up the cmd and display the IPv4 address everytime the computer restarts ?
thanks.
Simple, just create a batch file that runs ipconfig and displays the output:
start cmd /k ipconfig
exit
You'll see a console window appear on your screen each time you execute this batch file that contains the output of the ipconfig command. Among the information displayed will be the IP address for each of your computer's network adapters.
You can configure the command as appropriate, adding switches to ipconfig as desired. For example, adding the /all switch will cause additional information to be displayed.
If you wanted, you could parse the output of ipconfig, extract the IP address assigned to a particular network adapter, and display just that on the screen. That might reduce the cognitive overload. But any good Windows sysadmin can scan the output of ipconfig rather easily.
You can set a batch file to run upon startup.
The batch file should contain:
ipconfig
pause
I used this in a .bat file I created some days ago and it works fine:
#echo off
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo %ip%
This way the IP is defined as a variable named "ip", so you can use this to do other things with your current IP.
I hope it helped somehow.

Resources