Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
18 votes
Accepted

call batch file and run using its own path/directory

You are a bit unclear where app1.exe is located. If it shares the folder with run1.bat change run1.bat to either @Echo off Pushd "%~dp0" app1.exe popd or @Echo off "%~dp0app1.exe" %0 refers to ...
LotPings's user avatar
  • 1,035
17 votes

How to sleep in a batch file?

You can sleep in Batch using powershell: Milliseconds powershell -nop -c "& {sleep -m Milliseconds}" Seconds powershell -nop -c "& {sleep seconds}"
Wasif's user avatar
  • 331
11 votes

Check for file by wildcard and store as variable in batch file

Doesn't directly answer your question about a batch file but this would work easily in a PowerShell script: $validFile = Test-Path "C:\path\to\your\file" if ($validFile -eq 'True') { $path = C:\...
Vinny's user avatar
  • 407
10 votes

Rename files to add date modified to filename with Windows CMD or simple .TXT

In Powershell you can do this with a One-Liner: Get-ChildItem |Foreach-Object { Rename-Item $_ -NewName ("{0}-{1}{2}" -f $_.BaseName,$_.LastWriteTime.ToString('yyyyMMdd'),$_.Extension) } ...
Gerald Schneider's user avatar
10 votes

Check for file by wildcard and store as variable in batch file

setlocal enabledelayedexpansion enableextensions set LIST= for %%x in (%baseDir%\*) do set LIST=!LIST! %%x set LIST=%LIST:~1%
James Connigan's user avatar
8 votes

Change current directory to the batch file directory

You can do pushd "%~dp0" to go to the directory of a batch file -- even if it's on another drive. Additionally, that allows you to popd to go back to where you came from.
idbrii's user avatar
  • 183
6 votes

Is it safe to write batch files with Unix line endings?

It seems that batch labels are skipped when LF (Unix line-ending) is used in a .bat file.
go2null's user avatar
  • 275
6 votes

Is it safe to write batch files with Unix line endings?

For batch files there is no big difference between unix line endings and windows line endings. Currently, there is only a glitch known for the behavior of GOTO and CALL, while searching for a label. ...
jeb's user avatar
  • 365
6 votes

Change directory to "This PC" / "Computer" from .BAT batch file

This works on windows 10, I am not sure if it is version specific. explorer = Also: explorer.exe /e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D} explorer /root,
Anthony Fornito's user avatar
5 votes

Is it safe to write batch files with Unix line endings?

Kinda, but... You will have to be a paranoid and always duplicate your label lines! never use colons outside of label definitions! Eg.: @echo off goto main <- No use of colon ... :main :main <...
Simon Streicher's user avatar
5 votes
Accepted

How to get all processes under a session of a particular process that is using a port in Windows cmd

That's actually pretty easy in PowerShell: # Get the process of the listening NetTCPConnection and the session ID of the process $SessionId = (Get-Process -Id (Get-NetTCPConnection -State Listen -...
Gerald Schneider's user avatar
4 votes
Accepted

Run Log-on Script with Admin Rights

This is a poor use case for login scripts. A startup script or a scheduled task can both easily run as the SYSTEM account take care of your software installation. Generally I suggest using some kind ...
Tim Brigham's user avatar
  • 15.6k
4 votes
Accepted

Script for renaming files and adding a timestamp (server 2012 r2)

This bit of powershell does what you want. Just put the full path to the source and destination directories at the top between the quotation marks. Edit: added the bit about file creation time. Edit: ...
Joe Brailsford's user avatar
4 votes
Accepted

How to check that mstsc.exe has successfully set up a RDP connection?

You can check if the RDP logon was sucessful by querying the security log of the target system. Logon events are ID 4624, RDP logons are type 10. Other logon types https://www.ultimatewindowssecurity....
Clayton's user avatar
  • 4,573
4 votes

call batch file and run using its own path/directory

The answer to your question can be drawn from a similar question on Stack Overflow. What is the current directory in a batch file? Using the variables mentioned here, you can update run1.bat to call ...
Sam Erde's user avatar
  • 3,469
3 votes
Accepted

Rename files to add date modified to filename with Windows CMD or simple .TXT

Here is the code you need to use to iterate the text files, obtain the modified date of each file, copy each file using the modified date as part of the file name, and delete the original files. You ...
user5870571's user avatar
  • 3,164
3 votes

How to sleep in a batch file?

Disclaimer: this is not the "ideal" solution, so don't bother beating me over the head with that like done to those recommending ping... When possible, use timeout for sure. But as noted in the ...
BuvinJ's user avatar
  • 419
3 votes
Accepted

Batch file IF %time% not working

Try this: SET "ADJUSTEDTIME=%TIME: =0%" IF "%ADJUSTEDTIME:~0,5%" GEQ "19:58" GOTO :SHUTDOWN IF "%ADJUSTEDTIME:~0,5%" LSS "08:58" GOTO :SHUTDOWN GOTO :EOF :SHUTDOWN MSG * "Your device has not ...
Greg Askew's user avatar
  • 36.9k
3 votes
Accepted

Trigger a batch file when a user choose "Switch User" in windows

Best choice is to configure Advanced security audit policy and windows will register for you events about: User logon Workstation lock/unlock User logout and many others that may be useful to you. ...
Slipeer's user avatar
  • 3,313
3 votes

Robocopy - All data marked as modified

After lots of trial and error I think I found the reason: the Archive attribute. When Robocopy comes across a file with the Archive attribute set, it will systematically display the file as "Modified"...
jcharaoui's user avatar
  • 332
3 votes
Accepted

Run a batch file from network shared folder depending on MS outlook bit version

While debugging batch scripts, don't just launch them by clicking the icon, but from the command line (cmd.exe). Otherwise, you won't get to see the error messages. Now, for the actual problems... ...
Esa Jokinen's user avatar
  • 50.1k
3 votes
Accepted

Windows batch file scripting: how to get directory named with latest date (yyyy-mm-dd format)?

for /f %%d in ('dir /ad /o-n /b .') do ( echo %%d goto break ) :break /ad - Displays files with specified attributes. (D: Directories) /o-n List by files in sorted order. (n: By name (alphabetic)...
k010mb0's user avatar
  • 251
3 votes
Accepted

Command understanding in Batch script

That one line has a lot of functionality in it. An plain-English summary would go something like this: Ask Java what version it is. This gives multiple pieces of information. Find the line in Java's ...
Doug Deden's user avatar
  • 1,904
2 votes

How do you wait for an exe to complete in batch file?

I had the problem @John Gardeniers Had or described, where my exe called another exe and terminated, therefor start /wait didn't work. I created a "while loop" to check if its running and then move on ...
FreeSoftwareServers's user avatar
2 votes

How can I edit local security policy from a batch file?

I looked for so long too. I figured out the answer! To check the current state : auditpol /get /subcategory:"Process Creation" This next line will make the change. It will set the process creation ...
OatBoat's user avatar
  • 21
2 votes

command line REGEDIT command (edit) without a popup

If you are accessing locations in the registry that does not require admin privileges you can use __COMPAT_LAYER environment variable.Just execute this before using regedit: set __COMPAT_LAYER=...
npocmaka's user avatar
  • 431
2 votes

7zip logging & left-over .tmp files

Use ">>7zip.log" instead of ">7zip.log", this will make the output to be appended to the text file, and not overwrite the previous one.
user373350's user avatar
2 votes

How to get an EC2 Windows machine's instance-id into a batch variable

So I found another way of doing this: $instanceId = Get-EC2InstanceMetadata -Path '/instance-id' It seems a lot cleaner and result contains just what you expect "Instance ID"
Borik's user avatar
  • 121
2 votes

How do I script a change to the msmq storage location on Windows 2008 R2 server

Source: http://www.erbrech.com/blog/2016/05/29/Move-msmq-storage-location-with-powershell-and-Desired-State-Configuration.html $Location = 'E:\msmq\storage' If(!(test-path $Location)) { New-...
Matt Canty's user avatar
2 votes

Start /wait in batch file stops waiting after ~5 minutes

I cannot reproduce the 5 minute timeout on either windows 7 or 10. (I went as far as 15 minutes). Why use START at all? Have you tried the snippet below? Looks like you get a new command processor,...
RobW's user avatar
  • 2,796

Only top scored, non community-wiki answers of a minimum length are eligible