I already googled and tried several solutions but without success...
My batch script looks the following, it opens four files but then cmd.exe window stays open and cannot be closed anymore. Even Task Manager asks for admin rights if I want to close. I am no admin, only normal user, so to force closing the cmd window I can only logoff and re-login to windows.
I already tried several options ("/b" option on start command, "exit" command at the end, also "exit 0" ... without any difference)
start "" /b "file1.xlsx"
#ping -n 3 localhost> nul
start "" /b "file2.xlsx"
#ping -n 1 localhost> nul
start "" /b "file3.xlsx"
#ping -n 1 localhost> nul
start "" /b "file4.xlsx"
I am using Windows 10 Enterprise 21H2.
At the bottom add:
:end
exit
Then most probably you can solve this 😊
#phuclv Thank you! I changed the pause from ping command to timeout command as you proposed: and this solved the problem for me.
Great! Thank you!
start "" /b "file1.xlsx"
timeout 2
start "" /b "file2.xlsx"
timeout 1
start "" /b "file3.xlsx"
timeout 1
start "" /b "file4.xlsx"
Related
I have one single Batch file with 2 functions in it:
1.) MAIN
2.) ASYNC
Now i try to call the ASYNC function out of the MAIN function without waiting for the completion of the ASYNCfunction. Is that possible?
Here is my Batch file:
#echo off
:MAIN
setlocal
echo Here i am in the Main Thread
start /b :ASYNC
echo Here i am in the Main Thread again
endlocal
exit /b 0
:ASYNC
setlocal
echo I am in the ASYNC Thread
timeout /T 100
endlocal
exit /b 0
Is there any possibility to run a function in a single batchfile asynchron from another function in the same batchfile without separating the functions to multiple files?
Greets & Thank you :D
Yes, there is a possibility: use start to let the script restart itself, provide the label of the sub-routine as an argument and use goto rather than call to continue execution at that point:
#echo off
if not "%~1"=="" goto %~1
:MAIN
setlocal
echo Here I am in the MAIN thread
start "" /B cmd /D /C "%~f0" :ASYNC
echo Here I am in the MAIN thread again
endlocal
exit /b 0
:ASYNC
setlocal
echo Now I am in the ASYNC thread
timeout /T 10
endlocal
exit /b 0
When you run the script and want to first execute the main section, you must not provide an argument. If you do, it will be interpreted as a jump label. To avoid that, you could check whether the (first) argument begins with a : and skip the goto if not. Replace the line if not "%~1"=="" goto %~1 by the following to achieve that:
set "ARG1=%~1" & if not defined ARG1 shift /1 & goto :MAIN
if "%ARG1:~,1%"==":" goto %ARG1%
This is not absolutely safe against any odd arguments (like "&"&), but it will serve well in most situations. However, regard that the argument positions may not be as expected, because of the additional label argument (check out the argument string %*, which is even not going to be affected by shift).
Note, that the sub-routine then becomes executed in a new cmd.exe instance, so any environment changes will not reach the main instance.
There is even a better way to accomplish what you want, without affecting any potential arguments, namely to embed the label of the sub-routine in the script path itself when restarting the script (technique courtesy of user jeb, see his answer to How to pipe the final result of a windows batch for loop into another command):
#echo off
for /F "tokens=3 delims=:" %%Z in ("%~0") do goto :%%Z
:MAIN
setlocal
echo Here I am in the MAIN thread
start "" /B cmd /D /C "%~d0\:ASYNC:\..%~pnx0" %*
echo Here I am in the MAIN thread again
endlocal
exit /b 0
:ASYNC
setlocal
echo Now I am in the ASYNC thread
timeout /T 10
endlocal
exit /b 0
The key portion is the augmented path string %~d0\:ASYNC:\..%~pnx0, which is going to be resolved to %~d0%~pnx0 (that is the full script path %~0).
The sub-routine becomes again executed in a new cmd.exe instance.
I have a SQL request in my vba code
Set WSJ = VBA.CreateObject(“WScript.Shell)
WSJ.run “cmd.exe /C chcp 0000 & cd C:\oracle... “ & myPath & “.sql && exit”, 1,1
it worked perfect but recently stopped closing cmd window and not continue the process (it doesn't happen all the time, 1 or 2 times for day). There is no error, the last line is «disconnected from the server», but the window does not close and the code does not continue to work.
is it possible to write in the code a check for closing cmd window after a minute or something like that? or configure sql to reach this line and close
Thanks
please try with below code
Sub closwWindow()
Call Shell("cmd.exe /S /c" & "cd /d C:\UTAS-SA && del /f/s/q BJSFM > nul", vbNormalFocus)
End Sub
As the return value of WScript.Shell.Run is not very useful, I've tried to do it in another way, but I only found a way to get this done from command prompt itself:
Using Start, I can start a command prompt with the title I'm giving it, like:
start "this is an interesting window" cmd.exe
As a result, a command prompt is started, having "this is an interesting window" as the title. You can then use Tasklist in order to find this, something like:
tasklist /V | findstr "this_is_an_interesting_window"
cmd.exe 8240 Console 6 3 636 K Running DESKTOP-D0GIHSU\plop 0:00:00 this_is_an_interesting_window
From there you can retrieve the process ID (8240) in this case, and you can use TaskKill in order to kill it:
taskkill /FI "PID eq 8240"
SUCCESS: Sent termination signal to the process with PID 8240.
Good luck
This question already has answers here:
FTP commands in a batch script does not working properly
(2 answers)
Closed 5 years ago.
#echo off
set datetoday=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
set /a batchdate=%datetoday%-1
cd c:\batchreports\DAYENDREPORTS\PREDAYEND\%batchdate%
ftp 192.168.18.188
username
password
cd autoemail
send DEALS_ENTERED_TODAY_ALL_2OM_UP_20170813.xls
bye
exit
When I try to run the code line by line in cmd prompt it works properly but if I run the bat file itself, it just prompts the login screen, which means my code stops working at the line of username.. what should I do so that the code will proceed?
To securely get yesterdays date you can't simply subtract one from todays date if this will swap the month as Compo already pointed out.
#echo off
For /f %%Y in (
'powershell -NoP -C "(get-date).AddDays(-1).ToString(\"yyyyMMdd\")"'
) Do Set Yesterday=%%Y
Set "Folder=c:\batchreports\DAYENDREPORTS\PREDAYEND\%yesterday%"
Pushd "%folder%"||(Echo Can't locate %folder% &Pause&Exit /B 1)
> ftpscript.ftp (
echo host 192.168.18.188
echo username
echo password
echo cd autoemail
echo send DEALS_ENTERED_TODAY_ALL_2OM_UP_%Yesterday%.xls
echo bye
)
(ftp -i -s:ftpscript.ftp 2>&1 >ftpscript.log) && (Del ftpscript.*) || (
Echo An error occured, view log:
more < ftpscript.log
pause
Exit /B 1
)
Exit /B 0
In a folder named "drops" I have 3 .dll files. I am looking for a piece of code that can echo the number 0 to each .dll file without having to echo them individually. Basically, I don't want to have to open up each file and change the number to 0... laziness ensues!
for /f %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO echo 0
I have tried using for. Is my code wrong? Thanks in advance.
Edit: Title didn't have anything to do with my question :D
Easy
forfiles /p "C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops" /m "*.dll" /c "cmd.exe /c echo 0 > #path"
That will do it. Also, in ou code, add > %%G Thats the only mistake I found.
Mona
Drop /F, use
FOR %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO ECHO 0
/F will try to open file *.dll.
This is something that i searched for very long and never even found another that asked the same !
Is it possible to have a variable input with text under it !
Because, in the code, the text beneath is shown only on ending of the input.
Basicly, i'd like to make a thing like this :
######################
INPUT (the set /p cmd)
######################
The thing i want is to make the line under the set command visible
No!
It's not possible with pure batch.
There is a simple reason,
as there is no way to go up with the cursor, it's only possible to go back in the same line (with the CR or BACKSPACE characters).
Only CLS can bring the cursor up, but then the screen is empty again.
I know two commands with the possibility to go lines up, but I don't know a way to use them from a batch file.
set and cmd in a double threaded command window.
#echo off
if "%~1"=="intern" (
prompt %2$G
call %2
exit
)
start /b "" "%~f0" intern :thread2
:thread1
prompt :thread1$G
call :cls
echo Use ESC to go up lines
for /L %%n in (1 1 10) DO (
ping localhost -n 2 > nul
echo( %%n
)
exit /b
:thread2
set /p var=Press ESC ... NOW!
exit /b
But there exists many external tools to set the cursor position (CursorPos.exe from Aacini)