Batch Program Problem - batch-file

I am currently trying to program a batch script which allows the user to enter the name of a website, and then writes "127.0.0.1 www.website.com" at the bottom of the user's hosts file (essentially blocking that website)
Everything is working except for one line. I need to write the following line into another batch file which will be created by my program:
echo find /v "%url%" < C:\WINDOWS\System32\drivers\etc\hosts > C:\Users\%username%\desktop\temp.txt >> unblock.bat
This line is part of the code which will be able to remove the website from the hosts file if the user wishes to.
The problem appears to be the "<" and ">" signs. The program will not allow me to write these to the new batch file. I have tried to save them as variables and realized that the only way i could do it was by declaring them with inverted commas like this:
set char1="<"
set char2=">"
and then my command looks like this:
echo find /v "%url%" %char1% C:\WINDOWS\System32\drivers\etc\hosts %char2% C:\Users\%username%\desktop\temp.txt >> unblock.bat
The problem with this is that when writing to the new batch file, they both still have "" around them which makes the new batch file useless as the command does not execute properly.
Any ideas on how to fix this?
Here's the whole code for the batch file (incomplete):
#echo off
TITLE Site Blocker
SET /P url=Enter website (e.g. www.facebook.com)-
echo. >> C:\WINDOWS\System32\drivers\etc\hosts
echo 127.0.0.1 %url% >> C:\WINDOWS\System32\drivers\etc\hosts
echo find /v "%url%" < C:\WINDOWS\System32\drivers\etc\hosts > C:\Users\%username%\desktop\temp.txt >> unblock.bat
echo del C:\WINDOWS\System32\drivers\etc\hosts /Q >> unblock.bat
echo ren C:\Users\%username%\desktop\temp.txt hosts >> unblock.bat
echo copy C:\Users\%username%\desktop\hosts C:\WINDOWS\System32\drivers\etc\ >> unblock.bat
echo del C:\Users\%username%\desktop\hosts /Q >> unblock.bat
echo msg * %url% unblocked >> unblock.bat
echo del unblock.bat >> unblock.bat
echo exit >> unblock.bat
exit

Escape the greater than and less than with a caret (^)

Related

How to make a Batch File Move another File?

I am trying to make a batch script that moves a file into a startup folder and I want to make it universal
Currently I have this but I want to make it work on any user's computer (C:\users\USERNAME)
Here is the code
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %name%
move C:%user%\Desktop\Directory 1\dile.txt C:%user%\Desktop\Directory 1\file folder 1
:end
cmd /k
The file is called dile.txt located in a folder called Directory 1 on the desktop and I want to make it move to a folder called file folder 1 inside the Directory 1 folder. Is there a way to do this while making it work on anyone's computer?
%USERNAME% can be used to grab the active user account. Try something like this. Make sure to enclose paths in quotes when folders have spaces in their names.
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %USERNAME%
move "C:\Users\%USERNAME%\Desktop\Directory 1\file.txt" "C:\Users\%USERNAME%\Desktop\Directory 1\file folder 1\"
:end
cmd /k
Just for the sake of providing something a little bit different:
#Echo Off & SetLocal EnableExtensions
Set /P "=Startup . " 0< NUL
For /L %%G In (1,1,5) Do (Set /P "=. " 0< NUL
%SystemRoot%\System32\PATHPING.EXE 127.0.0.1 -n -q 1 -p 500 1> NUL)
Echo(&Echo Your name is %UserName%
%SystemRoot%\System32\Robocopy.exe "%UserProfile%\Desktop\Directory 1" "%UserProfile%\Desktop\Directory 1\file folder 1" "dile.txt" /Mov 1> NUL 2>&1
Pause
This uses some animated text, %UserProfile%, instead of C:\Users\%UserName%, and robocopy instead of Move. Using RoboCopy, allows for the creation of your destination directory, if it does not already exist.

Executing a command and storing its output in a file using batch file

In command prompt...
First execute Batch file
then execute numbers of commands.
so how can i store all that command and its output in a single file,
and file path define in batch file. ?
like this maybe
#echo OFF
(
Echo Ipconfig
Echo.
Ipconfig
Echo.
Echo.
Echo Net User Guest
Echo.
Net user Guest
)>file.txt
Exit
Or you could set the commands as variables and then type it instead of the whole command.
And easy Way to create a batch prompt that saves its output you could do it like This:
#echo off
:top
Cls
Set /p c=
Echo %c% >> output.txt & %c% >> output.txt
%c%
Echo. >> output.txt
Goto top
Of course then run the command and then create a space for the next Line

How to extract error descriptions from a batch file

I have this batch script:
#echo off
set server_list=C:\temp\servers.txt
set cab=E:\PHC\wsusscn2.cab
set sass=E:\PHC\SASS\sass.exe
erase c:\sass\error.log
for /f "tokens=1,2*" %%i in (%server_list%) do (
echo %%i
xcopy %cab% \\%%j\c$\sass /Y || echo %%i: ?error description (invalid drive specification, Access denied etc)? >> c:\sass\error.log
xcopy %sass% \\%%j\c$\sass /Y || echo %%i: ?error description (invalid drive specification, Access denied etc)? >> c:\sass\error.log
wmic /node:%%j PROCESS CALL CREATE "cmd /c c:\sass\sass.exe -i c:\sass\wsusscn2.cab -o c:\sass\%%i.csv" || echo %%i: ?error description (RPC server not available etc)? >> c:\sass\error.log
)
pause`
The script works fine, problem is, I can't get the error descriptions to the error.log file (between the question marks). It would help greatly, if I knew, why exactly the operation was unsuccessful. I tried the following:
echo %%i: cab file copy failed - %ERRORLEVEL%
But it always results in errorlevel 0 (obviously errorlevel of the echo command). I tried several ways to store the errorlevel to a variable, but always unsuccessfully. Furthermore, errorlevel only gives me partially what I want, not the descriptions, but just type of error.
The file Servers.txt contains hostnames of servers in one column, and their IPs in the second column.
How can I extract the error descriptions?
Problem was solved by using STDERR. This is working form of the questioned lines:
xcopy %cab% \\%%j\c$\sass /Y 2>> C:\SASS\error.log
xcopy %sass% \\%%j\c$\sass /Y 2>> C:\SASS\error.log
wmic /node:%%j PROCESS CALL CREATE "cmd /c c:\sass\sass.exe -i c:\sass\wsusscn2.cab -o c:\sass\%%i.csv" 2>> C:\SASS\error.log
This code types precisely what I need to the error.log file. Since this solution does not state the hostname of the server, I had to add these lines to the beginning of the loop:
echo ----------- >> C:\SASS\error.log
echo %%i >> C:\SASS\error.log

Windows batch Script to select week number when calling a file

I have considered to incorporate a batch file in my SSIS package to put a file into an SFTP site via winscp.com and therefore need to write a batch file at the end which can put the needed file into a SFTP site, for which I have written the process below but I need help to call the file which looks like Filename_Week #(eg. Filename_1,file name_2...Filename_34) and is located M:\Test\DATA OUT folder. My feeling is to add an if statement (i.e. if [%v_week%] == [] SET v_week= week #) after set v_week=%1, but I am not sure what that would be since I have never written a batch file before.
SET v_week=%1
echo option batch continue > SFTP_Filename_Put.txt
echo option confirm off >> SFTP_Filename_Put.txt
echo open target >> SFTP_Filename_Put.txt
echo lcd "M:\Test\DATA OUT" >> SFTP_Filename_Put.txt
echo cd / >> SFTP_Filename_Put.txt
echo put "M:\Test\DATA OUT\Filename_%v_week%.txt" /Filename_%v_week%.txt >> SFTP_Filename_Put.txt
echo exit >> SFTP_Filename_Put.txt
M:\temp\apps\WinSCP\winscp.com/script="M:\Development\SFTPBatchFiles\SFTP_Filename_Put.txt"
del SFTP_Filename_Put.txt
Like this it will automatically get the last created .fyi file and get the last 2 chars
and set your V_week variable.
#echo off&cls
setlocal EnableDelayedExpansion
for %%a in (*.fyi) do set $WeekNb=%%~na
set $WeekNb=%$WeekNb:~-2%
set v_week=%$WeekNB%
echo The Week Number Is : %v_week%
pause

Setting a variable from a text file

I have the following:
FOR /f "tokens=*" %%a IN (%input%) do (
dsquery * forestroot -filter "(&(mail=%%a))" | dsget user -UPN >> p.txt 2> tmp.txt
set /p Error=<tmp.txt
echo %%a %Error% >> log.txt
)
The point is to create a list of usernames from a list of emails by querying an AD, which is not a problem. But I also want to send the errors to a log file, and something doesn't seem to be working when I send the temp.txt into the variable (I tried to add "echo %Error%" and all it says is "Echo is off"
You are using %error% in a block statement for ... in (...) do (this is a block)
A block is evaluated in one go, but you are changing the variable inside the block.
To get the changed variable, use delayed expansion:
At the start of your script write
setlocal enabledelayedexpansion
and inside the block use the (changed) value with !error! instead of %error%
Changing
echo %%a %Error% >> log.txt
to
echo %%a !Error! >> log.txt
should solve your problem (together with the setlocal enabledelayedexpansion).

Resources