Windows batch Script to select week number when calling a file - batch-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

Related

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

redirecting .exe output within batch script to txt file

i am trying to redirect output from an exe(commanline exe) which is being called in from batch file to log file. In this script IP addresses of hostnames provided in input.txt are being redirected to result.txt. i am trying to run .exe within same batch script to place those IPs in maintenance mode in my monitoring tool. Script runs fine and performs the action as expected but it fails to capture the output from .exe. please help.
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (input.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo !SERVER_ADDRESS::=!>>%OUTPUT_FILE%
)
start c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"
Output from .exe if i run it directly from command prompt:
PS C:\> .\MaintenanceMode.exe ON C:\result.txt username
Not an IP!!
Reading IPs from File: C:\result.txt
Valid Arguments
System put into MM Host# 10.*.*.* Status# Success
System put into MM Host# 10.*.*.* Status# Success
You are redirecting the output of the START command, but not the exe.
If you want to use START and redirect the output, then you most execute a new CMD.EXE session and escape the redirection so it occurs within the new session:
start cmd /c c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% ^>^> "c:\result2.txt"
But why are you using START? It would be so much simpler if you simply execute your exe directly:
c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"

To create x number of folders and txt files using batch file

Hi I'm new to using batch files and I've been asked to make 3000 folders which include .txt files, all with the name 'Client1', 'Client 2' to 'Client 3000' for the folders and text files but have no idea how to go about this :s
You should use a for loop for this
take a look at this post:
Batch script loop
for a little explanation of who to use such a loop.
Furthermore to create a directory use md with the name you want to use
to create a file a
echo your_text_here > name_of_textfile.txt
should do
the windows solution would thus look like
for /l %x in (1, 1, 10) do (
md client%x
echo hello > client%x\%x.txt
)
this should get you started
under linux i would use
mkdir client{1..3000}
touch client*/textfile{1..3000}.txt
you can use this with cygwin if you just need to create the directories once...
This script creates 5 folders with each folder containing 5 files
echo off
SET /a j=0
:floop
IF %j%==5 GOTO END
md client%j%
cd client%j%
SET /a i=0
:loop
IF %i%==5 GOTO FEND
echo This is iteration %i%.
ver | date | time >> file%i%.txt
SET /a i=%i%+1
GOTO LOOP
:fend
SET /a j=%j%+1
cd ..
GOTO FLOOP
:end
echo That’s it!
pause
Now if you want to change the total number of folders change
IF %j%==5 GOTO END
to
IF %j%==n GOTO END
where n is the number of folders
same way to change number of text files change this line
IF %i%==5 GOTO FEND
to
IF %i%==n GOTO FEND
where n is the number of files

Print Batch results to a text file?

I created a batch file to lookup my external ip.
and it works well .
This is the code.
#echo off
>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo strHTML = objHTTP.ResponseText
>>"%temp%\ip.vbs" echo wscript.echo strHTML
for /f "tokens=7 delims=:<" %%a in ('cscript /nologo "%temp%\ip.vbs"') do set ip=%%a
echo %ip:~1%
pause
What i want is to Print the results to a text file named "IPlog.txt"
and every time i run the bat file it has to do the same thing and print the new results to the next line in the text file. So please can anyone help me with this.
... or change your
echo %ip:~1%
to
echo %ip:~1% >>IPlog.txt
to run your batch without the additional " >>IPlog.txt "
Please remove the pause command from your code and run the batch-file like this
mybatch.bat >> IPlog.txt
This will append the resulting ip address on to the log file IPLog.txt every time you run this batch file.

Batch Program Problem

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 (^)

Resources