Click here to Skip to main content
15,885,188 members
Everything / Batch

Batch

batch

Great Reads

by Simon P Stevens
How to set the current directory correctly when running batch scripts as administrator
by Chris Maunder
A quick overview and a simple Windows CMD script to make your terminal output a little more lively
by Dan Randolph
Builds on the existing RunRemote project to run commands on a remote server.
by Reto70
if you want to run the script in the folder where the script is store you can add the following code@ECHO %0 %*@pushd@cd /D "%~dp0"@cd....@pause@popdin most case %0 is the name of the script including the full path%~dp0 is only the drive and the path of the scriptcd...

Latest Articles

by honey the codewitch
Presenting a simple to create understandable and maintainable builds for projects with dependencies
by ToughDev
How to identify used and unused resources in VS Project Resources.resx file
by FormerBIOSGuy
This article describes a set of batch, AWK, and PostScript files that together allow you to create personalized, 12-page PDF calendars for some year.
by Chris Maunder
A quick overview and a simple Windows CMD script to make your terminal output a little more lively

All Articles

Sort by Score

Batch 

20 Oct 2010 by Simon P Stevens
How to set the current directory correctly when running batch scripts as administrator
7 Apr 2022 by Chris Maunder
A quick overview and a simple Windows CMD script to make your terminal output a little more lively
23 Dec 2011 by Dan Randolph
Builds on the existing RunRemote project to run commands on a remote server.
4 Apr 2011 by Alan N
I'm always up for a batch challenge and here is my attempt. Testing was minimal as I didn't want to break it!Alan.@echo offREM parse arguments in the format key=valueREM Example codename=g26 action=new lang=en:chsSETLOCALSET CMDLINE=%*SET CODENAME=SET ACTION=SET...
31 Jan 2014 by Sergey Alexandrovich Kryukov
It really depends on the file system and the kind of file. One way to destroy it is to fill it with random bytes, keeping the same exact size. Moreover, as the magnetic disk have residual magnetization which can be, in principle, read below the new bits, it's better to do it several...
9 Mar 2011 by Manfred Rudolf Bihy
If it were OK for you to use PowerShell multithreading would indeed be an option. I found an example here: http://blog.isaacblum.com/2010/01/22/powershell-multithreading-omg/[^]. Googling +PowerShell +Multithreading yields 111K results. I'm sure you'll find more examples as you come...
16 Mar 2011 by Dalek Dave
This[^] may help.In reality .bat files are just text files with a set of instructions.A simple script that calls commands and programs in order.Not really a c# question, unless I am missing something?
21 Mar 2011 by OriginalGriff
Been a while since I played with this...one.Bat:@echo offecho onecall two.batif %errorlevel%==23 goto twentythreeecho unknown errolevelgoto end:twentythreeecho Success!:endtwo.Bat:@echo OFFecho twoexit /B 23If you just execute two.bat from within one.bat, then it...
30 May 2012 by Richard MacCutchan
start /d "" firefox.exe http://www.ourgoalplan.com /waitTyping start /? at the command line will give you all the command options.
31 Jan 2014 by H.Brydon
I think the answer you are looking for depends on how the file deletion request was made and is one of the following choices:(1) The file is moved to the recycle bin, and if you open the bin, you can peruse recently deleted files and recover most of them to their original locations in the file...
31 Jan 2014 by Philippe Mori
I don't think that the technic of modifying the first character is used anymore in recent file system. But a search on Google give me a few pages that might interest you...
31 Jan 2014 by Abhinav S
There are tools that can help you destroy files completely.Otherwise there is a possibility that some programs might be able to recover them.Try - 5 Tools To Permanently Delete Sensitive Data From Your Hard Drive[^].
2 Dec 2015 by DamithSL
create batch file with your commands and schedule it using task scheduler check http://stackoverflow.com/a/13173752/2558060[^]
1 Mar 2011 by Reto70
if you want to run the script in the folder where the script is store you can add the following code@ECHO %0 %*@pushd@cd /D "%~dp0"@cd....@pause@popdin most case %0 is the name of the script including the full path%~dp0 is only the drive and the path of the scriptcd...
9 Mar 2011 by #realJSOP
Start multiple instances of the same console app. Each running application under Windows exists within its own thread.Beyond that, you shouldn't have to mess with assigning core affinity because .Net apps combined with Windows 7 will make the most efficient use of your system's architecture...
26 Sep 2011 by Niklas L
One idea is to add it as a (text-) resource in your exe. Then either execute it row by row if possible, or write it to a temporary file and run it from there.
30 May 2012 by Sergey Alexandrovich Kryukov
In addition to the perfect answer by Richard:The batch functionality has been considerably improved since DOS time, but for many people this fact remained unnoticeable. Even though batch language is too rudimentary and inadequate to many more or less complicated tasks, many innovations makes...
3 Jun 2012 by Sergey Alexandrovich Kryukov
Just search the Web, you will find enough resources, but please keep in mind that most of them are from the DOS epoch, and with NT the command interpreter has grown considerably, even procedure calls with return to the caller were implemented.So, you can get complete and fresh information...
7 Feb 2013 by Sergey Alexandrovich Kryukov
Remove CMD.EXE part, it's totally redundant.[EDIT]Thank you for clarification. Now, not only getting rid of CMD.EXE mean removing redundant tool, it's also critical.Visual Studio Prompt is interactive, something you don't want. It really is executed via %comspec% which is the same...
29 Nov 2013 by ExcelledProducts CEO
@echo off:loopstart yourtarget.exe ...timeout /t 1200 >nulltaskkill /f /im yourtarget.exe >nulgoto loopThis will restart your program every 20 minutes.I think I will elaborate on what it does and how you can edit it.The key part is the "timeout" function witch is standard...
14 Oct 2015 by Jochen Arndt
You won't be able to find a batch file to Perl converter or vice versa. pl2bat is not a converter. It is a tool that creates a batch file that is executing perl and passes the perl code contained in the same file (see here for an example:...
13 Nov 2016 by Jack Vanlightly
In this article we'll look at common patterns in batch processing and how the Taskling libraries provide a simple and reliable way of using those patterns in your C# batch jobs.
16 Jun 2020 by Richard MacCutchan
You need to use the set and for commands to get and parse the values. The following example merely echoes them back to the console, but should give you the idea. set /P choice=What are your choices? echo %choices% for %%i in (%choices%) do echo...
3 Nov 2010 by JF2015
This is a batch script that can be used for scripting in windows.See here:http://en.wikipedia.org/wiki/Batch_file[^]
21 Jan 2011 by Andrew Brock
IF EXIST C:\Windows GOTO ISWINDOWSecho "C:\Windows" was not foundGOTO END:ISWINDOWS echo "C:\Windows" exists:END
21 Mar 2011 by Sergey Alexandrovich Kryukov
Use new batch feature instead: call:proc parametergoto:eof:proc echo %1goto:eofThe line "goto:oef" in proc actually does not go to the end of file but performs a return. The value of parameter is obtained inside proc as %1.—SA
11 Nov 2011 by Alan N
How USERIDMGREX.EXE works is not clear from the question. If it is a program that displays it's own prompt and waits for the user to type an input then it may be possible to redirect standard input from a file. This is most easily demonstrated with the command processor itself and a text file...
20 Feb 2012 by yudi19872001
Batch file for deleting files automatically using ForFiles.exe.
3 Jun 2012 by ♥…ЯҠ…♥
Refer this 1)http://aumha.org/a/batches.php[^]2)http://dos.rsvs.net/DOSPAGE/BATCHCOM.HTM[^]3)http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true[^]Here is a sample batch file for youOpen Notepad and type the following...
3 Jun 2012 by Rajesh Anuhya
Hi, Interesting Question, long back i have worked with these batch files, even i don't know about the batch programming, here is the link which is help me a lot for DOS batch syntaxs Here[^] Hope this will help you, good luckThanks--RA
6 Nov 2012 by Alan N
Probably, I'm guessing at the problem but I assume "it doesn't work".Try this@echo offecho Testing Set /P and Set /Aset /p NUM1=Enter a number :set /p NUM2=Enter another number :set /a RESULT=NUM1+NUM2echo %NUM1% + %NUM2% = %RESULT%REM your stuff correctedset var=5echo...
18 Oct 2013 by xmark1rax
I found a solution: Instead of %variable%, write %%variable%%.So in order to display/write one %, write ECHO %%.
31 Jan 2014 by PIEBALDconsult
"I need to understand what is exactly happen"No, you don't.Here's something to read: http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html[^]
27 Feb 2014 by Richard MacCutchan
Try this:@echo offSet FOLDER=Shared_Location_pathSet DEST=%FOLDER%\folders.txt:repeatFOR /F %%i IN ('dir %FOLDER% /b /ad-h /o-d') DO ( SET LS=%%i GOTO :found)@echo No more subfolders foundgoto :finish:foundecho Most recent subfolder: %LS%SET...
12 Jun 2014 by Jani Mäkinen
Program return values inspected by .bat files with visual feedback
20 Jun 2014 by Richard Deeming
The Environment.SetEnvironmentVariable[^] method provides three targets via the EnvironmentVariableTarget parameter:Process - the variable only applies to the current process;User - the variable is stored in the registry for the current user, and persists across reboots;Machine - the...
24 Feb 2015 by Maciej Los
Please, read my comment to the question.As per i understand you're trying to run batch file from SQL Job Agent to zip files and put it an archive file onto ftp server.. Please, follow below links:How to: Create a CmdExec Job Step (SQL Server Management Studio)How to run multiple...
2 Dec 2015 by phil.o
I would use robocopy for that task.DamithSL solution is a good one, you should rely on built-in task scheduler to launch your batch regularly.robocopy /?in a console should give you plenty of details about differents options.Robocopy will allow you to copy only modified and newer...
8 Dec 2015 by Richard Deeming
If you don't want to use the Task Scheduler, use robocopy's "MOT" option:robocopy:/MOT:m :: MOnitor source; run again in m minutes Time, if changed.robocopy "C:\Users\Documents\Backup" "C:\Users\Documents\ArchiveUp" /MIR /MOT:2Specifying a Copy Schedule in Robocopy[^]
16 Feb 2016 by Dave Kreskowiak
You cannot protect a BAT file at all. It won't run as anything other than a normal text file.VBScript can be done, albeit not that securely, using the Script Encoder built into the Windows Script host can help a bit. You can find an example here[^].Overall, if protecting the code was a...
10 Nov 2016 by Dave Kreskowiak
If the application wasn't written to support this functionality, there is NO WAY to do what you want.There is also no way to find out if the executable supports this or any other command line parameters unless the vendor for the executable supplied you with documentation on it.
4 Mar 2017 by yuvalsol
Batch script that retrieves checked out and changed files from TFS and saves their paths to output file
16 Mar 2017 by johannesnestler
other's comments are good and right. i just wan't to share the approach I use when developing Windows Services.I don't want to unistall/install Service for a quick test during debug so I run them as console-applications.So I use Environment.UserInteractive to check if I run from...
1 Sep 2018 by Jochen Arndt
You have to set the path and environment variables for command line builds first. See Build C/C++ Code on the Command Line | Microsoft Docs[^]. If you have created a VS project you can build it with the msbuild command line utility. See MSBuild Command-Line Reference - Visual Studio |...
11 Oct 2018 by Richard MacCutchan
if "%count_slu%" EQU 0 ( Echo get out >> C:\SLU\Netsol_SLU_File_Delay\Net_Account_Log_%dt%.txt) Remove the double quotes around "%count_slu%", or add double quotes round the 0. You are trying to compare "0" with 0.
7 Feb 2019 by Dave Kreskowiak
Easy. The print spooler doesn't support delayed auto start.
5 Apr 2019 by Richard Deeming
If you want to stick with a batch file, then I'd suggest using pushd and popd to connect to the UNC path: pushd | Microsoft Docs[^] popd | Microsoft Docs[^] pushd \\Test\abc forfiles /D -4 /S /C "cmd /c if @isdir==FALSE echo /F /Q @path" popd
18 Dec 2020 by Dave Kreskowiak
Look at the value of NAME: \\remote machine\KEYPATH /f "data" Is that a valid registry key? I seriously doubt your registry key name ends with "/f "data"". Your script should look more like set NAME=\\remoteMachine\KEYPATH REG QUERY %NAME% /f...
2 Jul 2021 by RickZeeland
You probably need the full path, e.g. C:\Windows\System32\shutdown.exe
31 Dec 2021 by l o o l
Well, in theory, what you need to do would be: 1) get the decoded strings of a pre-existing string present in a given file 2) save this string/line obtained from the file in a variable 3) generate variable substring using space as delimiter,...
29 Apr 2022 by Richard MacCutchan
And we have already explained to you more than once that you cannot bypass the UAC prompt. It is there for a very good reason, i.e. to prevent illegal access to secure data on your system.
8 May 2023 by ToughDev
How to identify used and unused resources in VS Project Resources.resx file
3 Oct 2023 by honey the codewitch
Presenting a simple to create understandable and maintainable builds for projects with dependencies
19 May 2011 by ZeeroC00l
In Graph theory : Two graphs G1 and G2 are isomorphic if there is a one-to-one correspondence between vertices of G1 and those of G2 with the property that if two vertices of G1 are adjacent then so are their images in G2 Isomorphism[^]Take a look at the above mentioned...
19 May 2011 by Sergey Alexandrovich Kryukov
More accurate definition of isomorphism should also take into account colored, directed or weighted graphs. In this way, there should also be a one-to-one correspondence between graph's arcs and their properties with the property that the vertex and its adjacent arcs and its image in other...
21 Jun 2011 by Geoff Williams
Have you tried:Dim proc as Process = Process.Start("\\10.10.10.1\Public\Folder\BatchFile.bat")proc.WaitForExit()
30 Aug 2011 by Sergey Alexandrovich Kryukov
First, your "\r\n" is non-portable; right end-of-line is: System.Environment.NewLine.Now, I have no idea what do you mean by "????????". It must be just a blank space, " ". Also, it's safer to parse the text in separate line and get each line in quotation marks. This is for the case when you...
19 Sep 2011 by Sergey Alexandrovich Kryukov
Of course you can do it.Most simple thing is using the Microsoft application SC.EXE which simply a console-oriented front-end communicating with the Service Controller, so it's easy to use in the batch files.See...
25 Sep 2011 by Prerak Patel
Replace if %MinsOld% gtr 2 del %file%withif %MinsOld% leq 1 del %file%This will delete the files generated in last one minute.http://technet.microsoft.com/en-us/library/bb490920.aspx[^]
26 Sep 2011 by Mehdi Gholam
Just add commas between the types in the parenthesis.for %%1 in (*.jpg, *.mpg) do somecommand
26 Sep 2011 by Mehdi Gholam
Not really, the file system will cache the files and directory information, but this is not saving the information you want.You can do fancy things in batch files but this is extreme, cryptic and not advised. Check JPsoft TCC/LEYou are better off doing these things in "higher level...
27 Sep 2011 by mbue
Ok you want a suggestion:1. Use ShellExecute to start another program from inside yours with asynchronous behauviour. This can be a 32- or 64- executable.2. If you want to wait for the execution is finished, you should use ShellExecuteEx set the flag SEE_MASK_NOCLOSEPROCESS so you will get a...
27 Sep 2011 by Madhu Nair
If your batch is not running from C:\ put a C: just before the call.also consider the following lines ..CD C:\RIAA\Warning\debug\ IF EXIST "Warning.exe" call Warning.exe ELSE echo "Does not exists"
27 Sep 2011 by Richard MacCutchan
See the answers to your other questions; you really should consider the alternatives.
29 Sep 2011 by Sergey Alexandrovich Kryukov
It's "start Warning.exe" for asynchronous start or just "Warning.exe" to run the application and wait until it terminates.—SA
20 Dec 2011 by Sarvesh Kumar Gupta
for making scheduler: in sql, run sql server agent (SQl should be professional or enterprise edition not to be express edition, express edition not have this facility)Now, go into sql server agent and create job there. You can run sp, or any query at defined time using this. you try this, if...
20 Dec 2011 by #realJSOP
You can use TaskScheduler, but that can be problematic on a network server because it requires an account login to run the scheduled tasks, and if the account's credentials change, you have to update the task scheduler.You didn't specify that it is a database task, so using SQL's jobs...
21 Dec 2011 by Wendelius
If you're using SQL Server Express Edition, yo cannot use the native SQL Agent (it isn't included in that version). In that case, have a look at SQL Agent: A Job Scheduler Framework[^]
20 Feb 2012 by Richard MacCutchan
it is not workingThis gives us absolutely nothing to go on.I would suggest you edit your project so the event reads:echo $(ProjectDir)\build.batThat should help you to figure out what may be happening. You should also check your build output screen and logs for any error or warning...
2 May 2012 by Alan N
You sayWhen I create the batch files, it is created successfully, but while executing it, it opens another instance of the .NET application.I'm not clear if you are running the batch file at all, as it would not start the .NET application unless it contained a command to do...
30 May 2012 by bhagirathimfs
Hi allI am trying to build a batch file in which i want to open more 8 application at a time.so i wrote thisecho offclsstart /d "" firefox.exe http://www.ourgoalplan.comstart /d "" chrome.exe http://www.gmail.comstart /d "" iexplore.exe http://www.yahoo.comstart /d ""...
29 Aug 2012 by Tim Corey
You would need to create an application to do this. You could make that application a console application if you wanted. Here is an article where all of the work is done for you. You might even get a way with just running the exe file if it fits your needs:Watching Folder Activity in C#...
15 Sep 2012 by Richard MacCutchan
Some suggestions[^].
26 Sep 2012 by Member 4531085
Hi All,I want to check if folder exits or not using batch file, if exits i want to delete that folder else do nothing.Path of the folder is : %userprofile%\AppData\LocalLow\DictionaryCondition is i need to do it by batch file.Need to delete folder for all the existing users can...
26 Sep 2012 by Dave963
The pause isn’t really necessaryThe whole line of code should be on a single line in the batch file except for the "pause" that should be on the second line.for /d %%a in ("%systemdrive%\Users\*.*") do if exist %%a\AppData\LocalLow\Dictionary rmdir /s /q %%a\AppData\LocalLow\Dictionarypause
6 Nov 2012 by E.F. Nijboer
You would need to add a DeleteTable macro to it. You could then use the Command to read the given commandline argument, which would be the table name in this case. Try add a macro something like this:Public Sub DropTable() On Error Resume Next DoCmd.RunSQL "DROP TABLE " & CommandEnd...
5 Dec 2012 by Pablo Aliskevicius
What do you mean by "get"? ECHO? Write to a file? Load into a variable?To get echo, try this:DIR 2012*.XML /B > TEST.TXTfor /F "eol=. tokens=2 delims=2." %i in (test.txt) do @echo %iThe first line creates a text file with bare names of all matching files.The second line runs on the...
5 Dec 2012 by Elerian
If you want to move all files except one single file, you can rename it before moving the other files and restore it when your done.set KEEPFILE=2012P02B.XMLset TARGETDIR=C:\FOLDERBren %KEEPFILE% %KEEPFILE%.keptmove *.XML %TARGETDIR%ren %KEEPFILE%.kept %KEEPFILE%
6 Feb 2013 by H.Brydon
I think what you want to do is open a command window with the VS2010 compiler environment defined and execute the 'makecert' stuff in it.Rather than what you have described above, do this:Click Start -> (all programs) -> Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual...
7 Feb 2013 by H.Brydon
It seems that you want to add the VS2010 environment to the current command window. Here is how to do it... Go to the "Visual Studio Command Prompt (2010)" menu described in Solution 1, but instead of clicking on it (ie. left click), do a right click, select properties. In the 'Target' edit...
18 Feb 2013 by Sergey Alexandrovich Kryukov
Please see my comment to the question. Are you merging all directories into one? Then you can have problems: some sub-directories/files in different merging branches can have identical names; another possible problem is permission. You need to check it up.—SA
20 Feb 2013 by Sergey Alexandrovich Kryukov
If you really want to do it without using any 3-rd party compress utility, with just the batch, you can use Windows Script Host (WSH):http://en.wikipedia.org/wiki/Windows_Script_Host[^].You can find a good number of code samples, this is just one of them:...
11 Mar 2013 by H.Brydon
There are several ways, and I might note that what you are doing is a place where angels might fear to tread. At any rate:- Put the updated DLLs in a zip file (you still will need to unpack it and execute something - this would be 2 steps for the user).- Write an installer (the most robust...
1 Apr 2013 by Richard MacCutchan
zz.batcd %1copy %2 %3\pfx%2zz sourcedirectory filename destinationdirectory
14 Jul 2013 by OriginalGriff
If you mean that your want the bot.temp file to contain the stringif %query%==%s% (Then it's pretty simple: double up the '%' character.echo if %%query%%==%%s%% ( >> bot.temp
3 Oct 2013 by CHill60
You need to run the batch file as Administrator.There are several ways to achieve this ... e.g. if on your own PC you can right click on cmd and select "Run as Administrator", you can use the RunAs facility etc etcSome more ideas here[^]
22 Oct 2013 by Sergey Alexandrovich Kryukov
Please see my past answer: Problem a Window Services[^].—SA
7 Nov 2013 by Ron Beyer
You can use the rd or rmdir commands with the /s switch:rmdir/rd command[^]
9 Nov 2013 by OriginalGriff
To be honest, Sergey is probably right - doing this kind of thing in a batch file is not the easiest way: it's a bit like trying to move house using a Ford Ka. You can do it...but it's going to take some serious effort and you'd be much better off using a Transit instead. :laugh:But, if you...
8 Jan 2014 by hari111r
How to delete all the files in a folder and its subfolder using batch without prompt and also i want to delete the files even its is read only.Please help me to find solution.Thanks
17 Jan 2014 by OriginalGriff
If you look at the findstr description: http://technet.microsoft.com/en-us/library/bb490907.aspx[^] your code is not going to work, or even close to.The format for findstr is:FINDSTR [options] string filenameYour code doesn't include the file, so it has nothing to serach, and it is looking...
30 Jan 2014 by Kornfeld Eliyahu Peter
There is no error - error code 0 means, no error!
31 Jan 2014 by osamaworx
some one told me that when you delete file its not really destroyedit just add something like& or $ or ~ to it likeif your file is 1.txt it will be something like ~1.txtIs that rightWhat is the character added to initial file namecan I add it using ren commandhow to...
16 Feb 2014 by OriginalGriff
Basically? No.You are trying to write a text based adventure in a batch file - and trust me on this, that is a whole load more work than you need it to be.I would strongly suggest that abandon that approach - which is going to get increasingly complicated and error prone as your adventure...
24 Feb 2014 by Sergey Alexandrovich Kryukov
Almost anything here is not accessible to batch. You can do it in the batch, but only if you use it to run some applications for database operations, logging, pretty much everything. But if you have to write such applications anyway, it would become quite questionable: why using the...
7 Mar 2014 by Partha_sarathi_ghosh
Zip or Archive and delete old log files using Windows Task Scheduler in Winrar
21 Mar 2014 by Richard MacCutchan
You need to study Access Control[^].
22 Mar 2014 by Sergey Alexandrovich Kryukov
First of all, you cannot use "%jlinklocation %", it should be "%jlinklocation%", otherwise reference to the set symbol won' work. As to the blank space in "AT91SAM7S64 " and related problem is not manifested in your code sample anyhow.echo Device AT91SAM7S64simply outputs string in the...
19 Jul 2014 by Alan N
You question doesn't say what you expected to happen but but I assume that want to open an Explorer window at "c:\program files".I've found that Start command is either peculiar or just not documented corrected as it seems that the optional title field is not always optional!My tip would...
7 Aug 2014 by CPallini
That's the correct behaviour of the stringstream's extraction operator: you requested an int and it provided the first valid int represented in the string.If you want to extract the four ip octects as integers then try the following code:#include #include #include...