How to break the FOR Loop Inside the batch file? - asp.net

Here is my code :
#echo off
for /f "tokens=2*" %%a in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\NCR\APTRA\Aggregate
Installer\Inventory\Aggregate\APTRA Self-Service Support" /f 06.04.01') do set "AppPath=%%~b"
echo %AppPath%
cmd /k
So when we run this batch file command prompt will open, so my requirement is when i click on Enter we need to break the FOR LOOP as the command prompt is not getting closed, Could any one please help me out ?

try with goto:
#echo off
for /f "tokens=2*" %%a in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\NCR\APTRA\Aggregate
Installer\Inventory\Aggregate\APTRA Self-Service Support" /f 06.04.01') do (
set "AppPath=%%~b"
goto :break
)
:break
echo %AppPath%
cmd /k

Related

How to create a shortcut for task to run for local services in windows 10?

I have Bluetooth headphone, sometimes windows 10 fails to connect with Bluetooth device so I need to go to task manager -> Services - Open Services -> Find Bluetooth Support Service and restart it.
I was curious if I can make any shortcut or batch file operation or kind of script and I can just click on it and it will restart Bluetooth Support Service.
First, find your service name by typing this command:
powershell -Command "sc.exe query | Select-String -Pattern Bluetooth -Context 1,0"
You'll find your service's name with the string SERVICE_NAME just above the DISPLAY_NAME you searched for. It can be something like UmRdpService or RasMan, whatever.
Then, in an ELEVATED command prompt, type these two commands:
sc stop YourServiceNameFoundAbove
sc start YourServiceNameFoundAbove
Your service is now restarted.
You can use the following batch, it will ask automatically for elevation if needed.
Just modify the line set SRV=... and save it where it suits you, then you simply need to create a shortcut manually (done only once) on your desktop.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set SRV=YourServiceNameFoundPreviously
REM Check admin mode, auto-elevate if required.
openfiles > NUL 2>&1 || (
REM Not elevated. Do it.
echo createObject^("Shell.Application"^).shellExecute "%~dpnx0", "%*", "", "runas">"%TEMP%\%~n0.vbs"
cscript /nologo "%TEMP%\%~n0.vbs"
goto :eof
)
del /s /q "%TEMP%\%~n0.vbs" > NUL 2>&1
sc stop !SRV! || (
echo ERROR: Couldn't stop service !SRV!.
pause
goto :eof
)
sc start !SRV! || (
echo ERROR: Couldn't start service !SRV!.
pause
goto :eof
)
goto :eof

Encrypt multiple files GNUPG

I'm new on stackoverflow, and i need your help)
I'll try to describe the situation.
On a windows file-server, I have a task (running every 10 minutes) that decrypt gpg files received from an external trusted source (which encrypt files using my public key). The task run using a domain account, and decrypt files using my private key of course. Here is the script that I use to decrypt multiple files:
#echo off
FOR /R L:\IN\ %%f in (*.gpg) DO (call :subroutine %%f)
GOTO :eof
:subroutine
set filename=%1
echo %date% %time% - %filename% >> input_lst.log
gpg2 --q --batch --yes --passphrase-file "pwd.ini" -o %filename:~0,-4% -d %filename%
IF %ERRORLEVEL% EQU 0 (DEL /Q /F "%filename%") ELSE GOTO :error
GOTO :eof
:error
echo %date% %time% - %filename% >> error.log
:eof
echo %date% %time% - %filename% >> processed.log
Now, the main task is to create a batch script (no need to specify any recipient, etc) that will encrypt files in L:\OUT using the public key from external trusted source. Can someone help me? Thank you.
If you want to do something, do it by yourself))
#echo off
FOR /R L:\OUT\ %%f in (*.txt) DO (call :subroutine %%f)
GOTO :eof
:subroutine
set filename=%1
echo %date% %time% - %filename% >> encr__input_lst.log
gpg2 -r trusted#domain.com --trust-model always -o %filename%.gpg --q --batch --yes -e %filename%
IF %ERRORLEVEL% EQU 0 (DEL /Q /F "%filename%") ELSE GOTO :error
GOTO :eof
:error
echo %date% %time% - %filename% >> encr_error.log
:eof
echo %date% %time% - %filename% >> encr_processed.log

Copying files across network from a specific folder of different network hosts to a common network destination using batch file

I'm trying to automate a manual work of copying a specific file from many network client machines.
All clients will have a file that have to be backed up from time to time.
for that i have a list of client hostnames. and the file to be backed up is in a shared folder of each host.
Now I want a batch file that creates a new folder with "dd-mm-yyyy"format and copies all the files in to a masterHost.
for that i have wrote
#echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%dd%-%mm%-%yyyy%
echo New folder name %date%
MKDIR \\MasterHost\d$\%date%
copy \\client1\c$\data\data.txt \\MasterHost\d$\%date%\client1data.txt
copy \\client2\c$\data\data.txt \\MasterHost\d$\%date%\client2data.txt
copy \\client3\c$\data\data.txt \\MasterHost\d$\%date%\client3data.txt
copy \\client4\c$\data\data.txt \\MasterHost\d$\%date%\client4data.txt
copy \\client5\c$\data\data.txt \\MasterHost\d$\%date%\client5data.txt
now can anyone help me getting the client name from a file that has list of client hostnames say hostList.txt because i dont want to manually enter the host names each time...
the hostList.txt will look like this
client1
client2
client3
client4
client5
.
.
.
etc
Even a small help will be appreciated because i'm new to batch scripting. Thanks
You can try something like this:
for /F %x in ('type hostList.txt') do (
copy \\%x\c$\data\data.txt \\MasterHost\d$\%date%\client1data.txt
)
instead of all the copy... lines.

shell script help - checking for file exists

I'm not sure why this code isn't working. Its not going to the copy command.
I successfully run this manually on the command line (without the check)
I don't think i'm performing a correct file check? Is there a better, cleaner way to write this?
I just want to make sure the file exists, if so, copy it over. Thanks.
#!/bin/bash
if [ $# != 1 ]; then
echo "Usage: getcnf.sh <remote-host>" 2>&1
exit 1
fi
#Declare variables
HOURDATE=`date '+%Y%m%d%H%M'`
STAMP=`date '+%Y%m%d-%H:%M'`
REMOTE_MYCNF=/var/log/mysoft/mysoft.log
BACKUP_DIR=/home/mysql/dev/logs/
export REMOTE_MYCNF HOURDATE STAMP
#Copy file over
echo "Checking for mysoft.log file $REMOTE_MYCNF $STAMP" 2>&1
if [ -f $REMOTE_MYCNF ]; then
echo "File exists lets bring a copy over...." 2>&1
/usr/bin/scp $1:$REMOTE_MYCNF $BACKUP_DIR$1.mysoft.log
echo "END CP" 2>&1
exit 0
else
echo "Unable to get file" 2>&1
exit 0
fi
your checking existing file on remote computer seems like:
you should do:
ssh $host "test -f $file"
if [ $? = 0 ]; then
use sh -x script.sh to see what is happening.
You are testing for the existence of a remote file
$1:$REMOTE_MYCNF
using the local name $REMOTE_MYCNF. The if test is never satisfied.
You don't check that $1 is set.
Your file check runs on the local machine - not on the remote.
Change your if to:
if[! -f $REMOTE_MYCNF -o ! -d $REMOTE_MYCNF];

Batch/CMD: Finding out every single folder file meta.xml

i have a directory which contains the following:
-directory
-another directory
-meta.xml
-yet another directory
-meta.xml
...
So it needs to find in every directory 'meta.xml', all by itself (so that i dont have to type in every directory at the code) And i want that with EVERY meta.xml file a command is done.
Is there ANY possible way to do this, and could this be quiet done because the command i was talking about gives a text line from that meta.xml file, and i would like to not see whats its doing, that you get something like this:
text from meta file 1
text from meta file 2
text from meta file 3
and not
meta.xml found
text from meta file 1
meta.xml found
text from meta file 2
meta.xml found
text from meta file 3
If thats not clear, just think that i would just to like it run quietly, ok. (/Q maybe?)
this is the command i was talking about:
findstr "<name>" meta.xml >temp1.lis
FOR /F "tokens=2 delims=>" %%i in (temp1.lis) do #echo %%i > temp2.lis
FOR /F "tokens=1 delims=<" %%i in (temp2.lis) do #echo %%i > temp3.lis
it needs to do this for every single meta.xml file and giving the output so you get a total list when you do
type temp3.lis
Thanks!
Ok, as far as I can tell, this should work:
findmeta.cmd
#echo off
for /f "delims=" %%f in ('dir /b /s meta.xml') do (
FOR /F "tokens=2 delims=>" %%i in ('findstr "<name>" %%f') do #echo %%i > temp2.lis
FOR /F "tokens=1 delims=<" %%i in (temp2.lis) do #echo %%i >> temp3.lis
)
I modified your script part so answers are added to temp3.lis, instead of replacing it, and removed the use of the first temp file.
If, in the end, what you're looking for is what ever is between the tags, like
<name>blahblah</name> (the blahblah part)
this should do even faster, no temp files needed and one less for loop (broke into multiple lines for ease of reading, it all hold on a single line if needed.
for /f "delims=" %%f in ('dir /b /s meta.xml') do (
FOR /F "tokens=2 delims=><" %%i in ('findstr "<name>" %%f') do (
#echo %%i >> temp3.lis
)
)
Single line version:
for /f "delims=" %%f in ('dir /b /s meta.xml') do FOR /F "tokens=2 delims=><" %%i in ('findstr "<name>" %%f') do #echo %%i >> temp3.lis
Not sure which you prefer, IF this is what you're looking for.
EDIT:
Ok I think I got it, but I'm not sure it's gonna fix everything. Basically you want to change the tokens=2 to tokens=3 like this:
for /f "delims=" %%f in ('dir /b /s meta.xml') do FOR /F "tokens=3 delims=><" %%i in ('findstr "<name>" %%f') do #echo %%i >> temp3.lis
Now see if this works.
findstr /r /s ".*" meta.xml
will give you every line from every meta.xml file in the current directory and all its subdirectories, in the form:
dir1\dir2\dir2\meta.xml: line is here.
If you need anything more complex, I'd suggest installing Cygwin or GNU tools and using the bash shell or UNIX-like utilities from that, something like:
find . -name meta.xml -exec cat {} ';'
To find all meta.xml files:
dir meta.xml /s
Then you can pipe the output to some other command like
dir meta.xml /s | someCommand
I am not quite sure if I understood what you want. You want to find all files named meta.xml, but then do you want to search for something inside that file or do you want to see the complete content from that file? And you want to filter out any output regarding the name of the files found, you are only interested in the content of the files, right?
I am not sure if this is possible to do with cmd, but if you install cygwin you can easily do it the following way:
prompt>find top_level_directory -name meta.xml -print0 | xargs -0 grep -h "your search pattern"
for searching or
prompt>find top_level_directory -name meta.xml -print0 | xargs -0 cat
for outputting the complete file.

Resources