batch file that opens cmd prompt types string as command line - command-line

I got my batch file to open cmd prompt and change directory successfully, however what I cannot get to work is after the directory has changed I want the batch to then to enter a string as a command.
I've tried cmd /k string but that didn't seem to work, the cmd just sits at the changed directory. I've also tried:
set Opvar= echo string
%Opvar%
Again it just sits at the last changed directory. The batchisp line that's commented out is what the string is that I want to act as if I typed the whole string and pressed enter at the current directory prompt.
Here's the whole thing:
#Echo OFF
FOR /F "Tokens=*" %%# IN ('Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" ^| FIND "-1000"') DO (
FOR /F "Tokens=2,*" %%A IN ('Reg Query "%%#" /v "ProfileImagePath" ^| FIND /V "%%#"') DO (
set drive=%%B
Echo Admin SID: %%~n#
Echo Admin Folder: %%B
)
)
:Ask
echo What are you flashing? Please select one option(1,2, or 3):
echo 1. Main Board w/ 6-Station OpCon.
echo 2. Main Board w/ 12-Station Opcon.
echo 3. OpCon Board.
set /P INPUT=Type input: %=%
If "%INPUT%"=="1" goto Option1
If "%INPUT%"=="2" goto Option2
If "%INPUT%"=="3" goto Option3
::::::::::::
:Option1
echo Starting 6-Station DFU flash...
echo xcopy "%~dp0M1k_MainPCB\6-STATION\*.*" "%drive%\Desktop\MainPCB6\" /d /s /h /v /c /f /k /y
echo cmd /k "cd /d %drive%\Desktop\MainPCB6\6-STATION\Debug\"
goto End
::::::::::::
:Option2
echo Starting 12-Station DFU flash...
echo xcopy "%~dp0M1k_MainPCB\12-STATION\*.*" "%drive%\Desktop\MainPCB12\" /d /s /h /v /c /f /k /y
echo cmd /k "cd /d %drive%\Desktop\MainPCB12\12-STATION\Debug\"
goto End
::::::::::::
:Option3
echo Starting OpCon DFU flash...
xcopy "%~dp0M1k_SWPCB\SWPCB\*.*" "%drive%\Desktop\SWPCB\" /d /s /h /v /c /f /k /y
cmd /k "cd /d %drive%\Desktop\SWPCB\SWPCB\Debug\"
::batchisp -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0
goto End
::::::::::::
:End
echo ******
echo *****
echo ****
echo ***
echo **
echo *
echo Flash Completed! Press any key to exit...
Pause>NUL&Exit
Any help would be much appreciated.
Thank you

You are changing the working directory in a new instance of CMD (CMD /K), use PUSHD to change the working directory in your current CMD instance and POPD to return to the last used directory:
#Echo OFF
PUSHD %WINDIR%"
Echo I'm on "%CD%" Directory!
POPD
Echo I'm on "%CD%" Directory!
Pause&Exit
Change your code to this:
:Option3
echo Starting OpCon DFU flash...
xcopy "%~dp0M1k_SWPCB\SWPCB\*.*" "%drive%\Desktop\SWPCB\" /d /s /h /v /c /f /k /y
PUSHD "%drive%\Desktop\SWPCB\SWPCB\Debug"
batchisp -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0
POPD
goto :End
...Or if you want to start directly the command in a new instance of CMD change it to this:
CMD /k "Start /W """" "%drive%\Desktop\SWPCB\SWPCB\Debug\batchisp" -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0"
PS: Remember that you don't need to change the working dir to start an app, you can write the path followed by the process name like in my last example.

Related

Psexec batch file foor loop error

I have problem. All working on local machine but when use psexec i get error.
This is scrap of my .bat file
psexec.exe \%pc% cmd.exe /k for /f "tokens=2 delims=:" %%g in ('certutil -verifystore My ^| findstr /i "serial"') do echo %%g
On local cmd all its work but on remote i get error:
"At this point | was unexpected"
Could you help me?
Correct part of code
(...) in ('"certutil -verifystore My | findstr /i serial"') do (...)

how do i search for a file and when found set the file path to a variable in a batch script

I am trying to create a batch file that will run and open a Powershell script to then run.
this is what i have so far
#echo off
for /r C:\folder %%a in (*) do if "%%~nxa"=="2WNRN4VMS2.txt" set p=%%~dpnxa
if defined p (
echo %p%
) else (
echo File not found
Pause
)
Powershell.exe -Command "& '%p%'"
exit
That is very simple using command DIR for searching for the file recursively in folder C:\folder and all its subfolders and command FOR for assigning the drive and path of found file to an environment variable:
#echo off
for /F "delims=" %%I in ('dir /A-D /B /S "C:\folder\2WNRN4VMS2.txt" 2^>nul') do set "FilePath=%%~dpI" & goto FoundFile
echo File not found
pause
goto :EOF
:FoundFile
Powershell.exe -Command "& '%FilePath%'"
Please note that the string assigned to environment variable FilePath ends with a backslash. Use in PowerShell command line %FilePath:~0,-1% if the path of the file should be passed without a backslash at end.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
for /?
goto /?
pause /?
set /?

Batch Script - (Stop services, move log files to other directory, append date once move)

I'm a newbie in batch scripting. How can i add a currentdate on the filename once i move the files to other directory? Can you please check my code below? Thanks!
This is how it works:
-- I need to copy some files to other directory in order to kill the running processes which are (dsst)
-- once the dsst is not running, it should stop the GESWCPAServer and delete the copied files in c:temp
-- Next, copy the logfiles from C:\LOGFILES to C:\LOGFILES\Archive.
-- after the files has been copied. it will now start the services.
Thanks!
-------------------------------------------------------------------
#ECHO off
copy D:\fp_swenv\cg_fp\config\*.magik c:\temp
sc stop GESWCPAServer
:loop
echo checking for task list
tasklist /NH /FI "IMAGENAME eq dsst_writer_acp.exe" | find /I "dsst_writer_acp.exe"
rem tasklist /NH /FI "IMAGENAME eq textpad.exe" | find /I "textpad.exe"
if %ERRORLEVEL% == 0 goto sleeploop
goto finish_up
:sleeploop
echo Sleeping 10secs
sleep 10
goto loop
:finish_up
del c:\temp\*.magik
sc stop GESWDisptcher51
sc stop GESWCPAClient
sleep 10
set logpath1="C:\LOGFILES"
set arcpath1="C:\LOGFILES\Archive"
c:
cd %logpath1%
FORFILES /D -1 /M *.log /C "cmd /c move #path %arcpath1%"
cd /D %arcpath1%
sc start GESWCPAServer
sleep 10
echo checking for task list
tasklist /NH /FI "IMAGENAME eq dsst_writer_acp.exe" | find /I "dsst_writer_acp.exe"
rem tasklist /NH /FI "IMAGENAME eq textpad.exe" | find /I "textpad.exe"
if %ERRORLEVEL% == 1 goto sleeploop
sc start GESWDisptcher51
sc start GESWCPAClient
#ECHO ON
---------------------------------------------------------------------------
Please check if this works -
Replace your below code with my code.
set logpath1="C:\LOGFILES"
set arcpath1="C:\LOGFILES\Archive"
c:
cd %logpath1%
FORFILES /D -1 /M *.log /C "cmd /c move #path %arcpath1%"
My Code -
EDIT - Added the zip option as requested by OP in the comment.
#echo OFF
set logpath1=c:\Logfiles
set arcpath1=c:\Logfiles\archive
cd /d %logpath1%
for /f %%x in ('wmic os get localdatetime ^| findstr /b [0-9]') do set TS=%%x
set yyyy=%TS:~0,4%
set mm=%TS:~4,2%
set dd=%TS:~6,2%
set hh=%TS:~8,2%
set min=%TS:~10,2%
set timestamp=%dd%-%mm%-%yyyy%_%hh%-%min%
for /f %%i in ('dir /b *.log') do call :moveandrename "%%i"
goto :jump
:moveandrename
set filename=%~n1
set fileextn=%~x1
move /y %filename%%fileextn% %arcpath1%\%filename%-%timestamp%%fileextn% >nul 2>&1
goto :eof
:jump
cd %arcpath1%
C:\Program Files\WinZip\wzzip.exe -a Archive_%timestamp%.zip *.log
if not %errorlevel% EQU 0 echo.Zip operation failed on %timestamp% >>zipresult.txt & goto :eof
del *.log
:eof
Cheers, G

Batch file to look for file from today and run another .bat otherwise run vbs and exit

I have a batch file I would like to search a folder for any xlsm file updated today.
If it finds an xlsm file from today, it then runs another bat file. If there is no updated file it should run a .vbs file (to send an email) and then exit.
Currently I have the following:
#echo off
set var=%date:~-10%
dir "*.xlsm"|find "%var%">nul&&CALL Update.bat||EXIT
I think I probably need to include some sort of IF/ELSE instead of the current method, but my skills are lacking..
EDIT:
The actual solution I've gone with, based on the answer from #Monacraft is:
#echo off
forfiles /p C:\ /m *.xlsm /d 0 /c "cmd /c call Update.bat"
if ERRORLEVEL 1 start FailEmail.vbs
If using windows 7 you could do forfiles:
#echo off
set found=FALSE
forfiles /p "C:\...[path to folder]" /m *.xlsm /d +1 /c "cmd /c Echo Updating...&CALL Update.bat&set found=TRUE"
if /i %found%==False (
start email.vbs
) else (
Exit
)
And that should work fine
Mona
This worked for me:
#echo off
forfiles /p C:\ /m *.xlsm /d 0 /c "cmd /c call Update.bat"
if ERRORLEVEL 1 start FailEmail.vbs
I think your code should work - some small changes here.
It will just echo the commands to the screen, for you to test.
One point to consider is that the DIR command will also match *.xls files in the short name.
#echo off
set "var=%date:~-10%"
dir "*.xlsm"|find "%var%">nul && (echo CALL Update.bat) || (echo start failemail.vbs)
pause

Batch File XCopy Command

I have a batch file which loops through a content of a text file and copies a specific file using xcopy command.
here's the snippet.
for /f %%a in (FilesToCopy.txt) do (
xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f
xcopy Common\%%a ..\..\Common\%%a /i /d /C /v /s /y /f
)
%%a contains values like
Images\image1.jpg
Images\image2.jpg
so when xcopy is executed it would look like
xcopy ..\..\Common\Images\image1.jpg Common\Images\image1.jpg /i /d /c /v /s /y
upon execute it would then prompt this message
Does Common\Images\image1.png specify a file name
or directory name on the target
(F = file, D = directory)?
it seems that the /i command doesn' work or i am missing something here to suppress the message above.
Well, you left out the second statement the help gives about /I:
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
You are only ever copying one file at a time, so /I doesn't apply.
You can probably hack-solving this by piping F into the command and suppressing output:
echo F|xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f >nul
(Won't work on non-English versions of Windows; but probably that's the least of your problems, given that the batch already fails for file names with spaces :-))
You could try building a single long list of file names to copy:
setlocal enabledelayedexpansion enableextensions
set LIST=
for /f %%a in (FilesToCopy.txt) do set LIST=!LIST! "..\..\Common\%%a"
xcopy %LIST% Common /i /d /c /v /s /y /f
This requires two passes over the initial file, though. And it fails when the list of file names gets longer than 8190 characters.
The destination should be a path, then it won't ask:
xcopy ..\..\Common\Images\image1.jpg Common\Images\ /i /d /c /v /s /y
In your case, you can use path extraction with %~p on the destination since you may want to preserve that:
xcopy ..\..\Common\%%a Common\%%~pa /i /d /c /v /s /y