I need to make a batch file for my groupmates (actually, I need to make a sequence of the program, but for simple to ask, I use batch file).
The batch's job is copy a specific file in their computer to the batch's folder. But the problem is I don't know the path to that file of all my groupmates.
Here are the things I need:
Help my groupmates choose their path to that file. (Maybe just auto-find that file in their computers).
Copy that file and paste it into the batch file's folder (which include my other programs).
After all my other programs finished their job, copy and replace that file to its original folder.
Do you have any script that might help?
You can start with this batch code :
#echo off
Title Search for a file by name (Wildcard accepted) by Hackoo 2014
mode con cols=90 lines=5 & color 9B
echo(
Set /p "FileName=Please Enter the name of the file to find (Wildcard accepted) : "
echo(
Set Tmp=Tmp.txt
Set SearchResult=SearchResult.txt
Call :BrowseFolder "Select the Source folder" "C:\Program"
Set LocationFolder=%MyFolder%
echo You chose to looking into "%LocationFolder%" for this file "%FileName%"
echo( & cls & Color 0A
echo( & echo Please Wait for moment .... Searching for "%FileName%" on "%LocationFolder%"
Where /r "%LocationFolder%" "%FileName%" > %Tmp%
Cmd /U /C Type %Tmp% > %SearchResult%
Del %Tmp%
Start %SearchResult%
::******************************************************************************
:BrowseFolder
set MyFolder=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
>%vbs% echo set WshShell=WScript.CreateObject("WScript.Shell")
>>%vbs% echo set shell=WScript.CreateObject("Shell.Application")
>>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2)
>>%vbs% echo if typename(f)="Nothing" Then
>>%vbs% echo wscript.echo "set MyFolder=Dialog Cancelled"
>>%vbs% echo WScript.Quit(1)
>>%vbs% echo end if
>>%vbs% echo set fs=f.Items():set fi=fs.Item()
>>%vbs% echo p=fi.Path:wscript.echo "set MyFolder=" ^& p
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
::******************************************************************************
EDIT : 28/06/2015 at 07:00
#echo off
Title Search for a file by name and copy it (Wildcard accepted) by Hackoo 2015
mode con cols=90 lines=5 & color 9B
Set /p "FileName=Please Enter the name of the file to find (Wildcard accepted) : "
Set SearchResult=SearchResult.txt
Call :BrowseFolder "Select the Source folder" "C:\Program"
Set LocationFolder=%MyFolder%
echo You chose to looking into "%LocationFolder%" for this file "%FileName%"
echo( & cls & Color 0A
echo( & echo Please Wait for moment .... Searching for "%FileName%" on "%LocationFolder%"
where /r "%LocationFolder%" "%FileName%" > %SearchResult%
Goto:CopyMyFile
::******************************************************************************
:BrowseFolder
set MyFolder=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
>%vbs% echo set WshShell=WScript.CreateObject("WScript.Shell")
>>%vbs% echo set shell=WScript.CreateObject("Shell.Application")
>>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2)
>>%vbs% echo if typename(f)="Nothing" Then
>>%vbs% echo wscript.echo "set MyFolder=Dialog Cancelled"
>>%vbs% echo WScript.Quit(1)
>>%vbs% echo end if
>>%vbs% echo set fs=f.Items():set fi=fs.Item()
>>%vbs% echo p=fi.Path:wscript.echo "set MyFolder=" ^& p
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
::******************************************************************************
:CopyMyFile
Cls
for /f "delims=*" %%a in (%SearchResult%) do (echo Copy "%%a" "%~dp0" & Copy "%%a" "%~dp0")
Pause
dir c:\nameoffiletosearch /s /b > %temp%\results.txt will create a list with full path of all occurences of your file in drive c.
You could even wrap this in a loop that searches multiple drives.
Then get the path from the results.txt and use it with your copy command.
Related
I'm trying to loop through all .lss files in a folder, and grab a string that exists between two tags, save that value and rename the file using that string.
Example:
42982934829.lss -> contains string:
<surveyls_title><![CDATA[J.3200-1118 - Project Title]]></surveyls_title>
Rename to `J.3200-1118 - Project Title.lss`
Here is what I have so far, but I fear my syntax is badly incorrect..
#Echo off
Set Folder=X:\RenameTest
Set Files=*.lss
PushD %Folder%
For %%A in (%Files%) Do For /f %%B IN (
'findstr "<surveyls_title>.*</surveyls_title>" "%ProjectTitle%"'
) Do Call :Rename ..
PopD
Goto :Eof
:Rename
Echo Ren %1 "%ProjectTitle%"
I suppose this is what you want.
#echo off
pushd "X:\RenameTest"
for %%a in (*.lss) do for /f "tokens=3*delims=[]" %%i in ('type "%%a" ^| find "</surveyls_title>"') do echo ren "%%~a" "%%~i%%~xa"
popd
Just remove echo from the line once you are happy with the printed results.
You can also extract the title using Regex : Demo Here
#echo off & color 0A
Title Extract Title using Regex
Set "InputFile=42982934829.lss"
Call :Extract_Title "%InputFile%" Title
Echo %Title%
Pause & Exit
::----------------------------------------------------------------------------------------
:Extract_Title <InputFile> <Title to be Set>
(
echo WScript.StdOut.WriteLine Extract_Title(Data^)
echo Function Extract_Title(Data^)
echo Data = WScript.StdIn.ReadAll
echo Set re = New RegExp
echo re.Global = True
echo re.IgnoreCase = True
echo re.Pattern = "\[CDATA\[(.*?)\]\]"
echo For Each Match in re.Execute(Data^)
echo Title = Match.SubMatches(0^)
echo Next
echo Extract_Title = Title
echo End Function
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%A in ('cscript /nologo "%tmp%\%~n0.vbs" ^< "%~1"') do set "%2=%%A"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------------
I want to make autorun .bat program that automatically performs netsh cmd and save the result in .txt file by the just simple click of a button.
below is what I wrote in notepad and saved as getkey.bat
echo netsh wlan show profile name=wifi_name key=clear >Desktop/savedpasskey.txt
exit
but it is not working
To show the password of your WIFI SSID , you must execute this batch file with admin rights :
#echo off & setlocal enabledelayedexpansion
Set "Copyright=by Hackoo 2017"
Title %~n0 %Copyright%
Mode con cols=75 lines=8
cls & color 0A & echo.
echo ***********************************************
echo %~n0 %Copyright%
echo ***********************************************
echo(
if _%1_==_Main_ goto :Main
Set Count=0
Set L=0
:getadmin
echo %~nx0 : self elevating
set vbs=%temp%\getadmin.vbs
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
::*************************************************************************************
:Main
Call :init
Call :CountLines
Set "PasswordLog=%~dp0Wifi_Passwords_on_%ComputerName%.txt"
%Mod%
echo(
echo ***********************************************
echo %~n0 %Copyright%
echo ***********************************************
echo(
Call :Color 0E " [N][SSID] ================ Password" 1
echo(
(
echo ***********************************************
echo %~n0 %Copyright%
echo ***********************************************
echo(
echo [N][SSID] ==============^> "Password"
echo(
)>"%PasswordLog%"
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
if not "%%a"=="" (
set "ssid=%%a"
set "ssid=!ssid:~1!"
call :Getpassword "!ssid!"
)
)
echo(
echo Done
If exist "%PasswordLog%" start "" "%PasswordLog%"
pause>nul
exit
::*************************************************************************************
:Getpassword
set "name=%1"
set "name=!name:"=!"
Set "passwd="
for /f "delims=: tokens=2" %%a in ('netsh wlan show profiles %1 key^=clear ^|find /I "Cont"') do (
set "passwd=%%a"
Set /a Count+=1
)
If defined passwd (
set passwd=!passwd:~1!
echo [!Count!][!name!] ====^> "!passwd!"
echo [!Count!][!name!] ====^> "!passwd!" >> "%PasswordLog%"
) else (
Set /a Count+=1
call :color 0C " [!Count!][!name!] The Password is empty" 1
echo [!Count!][!name!] The Password is empty >> "%PasswordLog%"
)
exit /b
::*************************************************************************************
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
::*************************************************************************************
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
<nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
::*************************************************************************************
:CountLines
for /f "skip=2 delims=: tokens=2" %%a in ('netsh wlan show profiles') do (
if not "%%a"=="" (
set /a L+=1
)
)
set /a L=!L! + 10
Set Mod=Mode con cols=75 Lines=!L!
exit /b
::*************************************************************************************
This works. You can change the TEMPFILE to wherever you want the file to be created.
SET "TEMPFILE=%USERPROFILE%\Desktop\savedpasskey.txt"
netsh wlan show profile name=wifi_name key=clear >"%TEMPFILE%"
TYPE "%TEMPFILE%"
It was not working because the path you specified to save your output text file was not getting recognized. The below code should work properly.
#echo off
netsh wlan show profile name=wifi_name key=clear >%USERPROFILE%\Desktop\savedpasskey.txt
#pause
It's always better to specify fully qualified path for the files. In this case it will be C:\Users\<User_Name>\Desktop\savedpasskey.txt where %USERPROFILE% will been replaced with C:\Users\<User_Name>.
Remove #pause if you don't want the command prompt to stay on screen after command is executed.
With reference to increment folder name link, I want to ask how to change it from creating folder to creating text file?
Here's the edited version of the code:
#echo off
#For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
)
#echo off
setlocal enableDelayedExpansion
set "baseName=testing-%Year%%Month%%Day%-"
set "n=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%baseName%*."^|findstr /xri "%baseName%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%baseName%=!"
if !name! gtr !n! set "n=!name!"
)
set /a n+=1
md "%baseName%%n%"
So it works like this. "Hello-20140716-1", "Hello-20140716-2" etc folder can be created by double clicking this batch file. But how can I change it from folder to text file?
I tried changing md "%baseName%%n%" to Echo "Hello" > %baseName%%n%.txt and Echo "Bye" > %baseName%%n%".txt
But this does not work as it will only output " Bye" into hello-20140716-1.txt without creating "Hello" to hello-20140716-1.txt and "Bye" to hello-20140716-2.txt
Test this:
#echo off
For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
)
set "baseName=testing-%Year%%Month%%Day%-"
set "n=0"
:loop
set /a n+=1
if exist "%baseName%%n%.txt" goto :loop
type nul > "%baseName%%n%.txt"
echo "%baseName%%n%.txt" was created
pause
Question title is about 'Increment text filename using batch script', so I dropped the timestamp in filename and just increment the name.
Here is my script:
#echo off
echo.
setlocal
set N=0
set FILENAME=baseFileName.%N%.anyExtension
:loop
set /a N+=1
set FILENAME=baseFileName.%N%.anyExtension
if exist %FILENAME% goto :loop
echo You can safely use this name %FILENAME% to create a new file
endlocal
Credit:
I created my own flavor based on #foxidrive's answer, meaning 99% credit goes to her/him
this batch script to search file better
using "find" its just ordinary...and need to type exactly the file name "file 1.txt" "file2.txt" "file letter.txt"
and exactly folder location...
any other way?
just try type "file"
its show all file with name "file"
"file 1.txt"
"file2.txt"
"file letter.txt"
and have number
1 . "file 1.txt"
2 . "file2.txt"
3 . "file letter.txt"
and enter the number to select the file what we want...
and it will be open.
#echo off
setlocal EnableDelayedExpansion
if exist log del log
set "token=%~1"
if not defined token (
echo Search for what string?
set /p token=^>
echo.
)
set a=0
for /f "delims=" %%A in ('dir /b ^| find "%token%"') do (
set /a a+=1
echo !a!. %%A
echo !a!. %%A >>log
)
echo.
echo Enter number of file to open.
set /p op=^>
for /f "tokens=1,2 delims=." %%A in (log) do (
if %%A EQU %op% start %%B
)
del log
the problem is
when enter the number...batch otomatic close...file wont open...
any suggestion?
you might try this:
#echo off &setlocal
set /p "spat=Enter search pattern: "
for /f "tokens=1*delims=:" %%a in ('dir /b /s /a-d \%spat%*.txt 2^>nul^|findstr /ri "%spat%[^a-z]"^|findstr /rn $') do set "$%%a=%%~b"&set /a fcnt+=1
if not defined fcnt (echo Error! Not such file "%spat%".&goto:eof)
:loop
for /f "tokens=1*delims==$" %%a in ('set "$" 2^>nul') do echo %%a.%%~b
set /p "fno=Enter file number to run: "
echo %fno%|findstr "[1-9][0-9]*" >nul || (echo Error! Try again.&goto:loop)
if %fno% gtr %fcnt% (echo Error! Enter number between 1-%fcnt%. Try again.&goto:loop)
for /f "tokens=1*delims==$" %%a in ('set "$%fno%"') do "%%~b"
I'm new to batch files scripting.
All I want is creating a batch file that calling SQL file and store results in text file .
Can anyone help me, your help is highly appreciated,
This is the first time I need to create such files.
using a batch file:
save and run this:
#echo off
sqlplus -s -l user/pass#yourdb #yoursql.sql>your_log.log
p.s. be sure to have the last line of your sql script as exit; or the batch file will hang.
sqlcmd -S sqlservername -i yoursqlfile.sql -U username -P password -o outputfile.txt
I created a more advanced launcher, try it out (you can find the latest version at http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle-sql-linux-sh-scripts-available-here.html)
Here's the code anyway:
launcher.cmd
#ECHO OFF
rem Script Launcher by Fr3dY v1.4
rem ##############################
rem Version History:
rem 1.4 - Misc. fixes
rem 1.3 - Merged with 'server launcher', now accepts both SQL and SHELL SCRIPTS
rem 1.2 - Interactive prompt to show the file on screen
rem 1.1 - No need to add 'quit;' or 'exit;' in the .sql file anymore
rem Fixed sqlplus waiting for username/password if the first attempt was unsuccessful
rem Log file is generated automatically, including date and time in name
rem Misc. fixes
rem 1.0 - Initial Version
:MAIN
::Path of PLINK
set PLINK="C:\Program Files (x86)\PuTTY\plink.exe"
::List with TNS NAMES
set dbservers=launcher-databases.txt
::List with LINUX SERVERS
set linuxservers=launcher-servers.txt
set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set dt=%dt: =0%
echo Choose launcher mode:
echo 1) Database scripts (.sql files)
echo 2) Shell scripts (.sh files)
set /p launchermode="Insert value: "
echo.
if %launchermode%==1 (
set extension=sql
set servers=%dbservers%
set mode=DB
) else (
if %launchermode%==2 (
set extension=sh
set servers=%linuxservers%
set mode=OS
) else (echo "Incorrect value, exiting..." & goto :END)
)
if exist %servers% (
goto :LISTFILES
) else echo FILE %servers% NOT FOUND, ABORTING & goto :END
:LISTFILES
echo Listing *.%extension% files...
echo.
dir /b *.%extension%
echo.
set /p file=Name of the file to be launched (without extension)?
if exist %file%.%extension% (
goto :CONFIRMSHOW
) else echo FILE %file%.%extension% NOT FOUND, ABORTING & goto :END
:CONFIRMSHOW
echo.
set /p confirm=Show the script %file%.%extension% on screen now?
if %confirm%==y (
goto :SHOWSCRIPT
) else goto :CONFIRMEXEC
echo.
:SHOWSCRIPT
echo.
echo Content of %file%.%extension%
echo ######################
type %file%.%extension%
echo.
echo ######################
echo.
:CONFIRMEXEC
set /p confirm=Are you sure you want to execute this script?
if %confirm%==y (
set /p user=%mode% username?
goto :HInput
) else echo ABORTED & goto :END
echo.
echo Output saved to %file%_%dt%.log
echo.
Goto :END
:HInput
::Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI
::Version 3.0
SetLocal DisableDelayedExpansion
echo.
Echo Enter password:
Set "Line="
Rem Save 0x08 character in BS variable
For /F %%# In (
'"Prompt;$H&For %%# in (1) Do Rem"'
) Do Set "BS=%%#"
:HILoop
Set "Key="
For /F "delims=" %%# In (
'Xcopy /L /W "%~f0" "%~f0" 2^>Nul'
) Do If Not Defined Key Set "Key=%%#"
Set "Key=%Key:~-1%"
SetLocal EnableDelayedExpansion
If Not Defined Key echo. & Goto :HIEnd
If %BS%==^%Key% (Set /P "=%BS% %BS%" <Nul
Set "Key="
If Defined Line Set "Line=!Line:~0,-1!"
) Else Set /P "=*" <Nul
If Not Defined Line (EndLocal &Set "Line=%Key%"
) Else For /F delims^=^ eol^= %%# In (
"!Line!") Do EndLocal &Set "Line=%%#%Key%"
Goto :HILoop
:HIEnd
if %launchermode%==1 (
goto :EXECDB
) else goto :EXECLINUX
:EXECDB
FOR /f %%A IN (%servers%) DO CALL ECHO DATABASE: %%A & ECHO DATABASE: %%A >> %file%_%dt%.log & sqlplus -S -L %user%/!Line!#%%A < %file%.%extension% >> %file%_%dt%.log
goto :END
:EXECLINUX
FOR /f %%A IN (%servers%) DO CALL ECHO SERVER: %%A & ECHO SERVER: %%A >> %file%_%dt%.log & echo y | %PLINK% %user%#%%A -pw !Line! "exit" & %PLINK% %user%#%%A -pw !Line! -batch -m %file%.%extension% >> %file%_%dt%.log & echo. >> %file%_%dt%.log
goto :END
:END
pause