New answers tagged

1 vote

Get String Length - How It Works

@echo off set "_var=My len = 59 | It can be summarized in a single for /f loop:" for /f usebackq %%G in =;(` %ComSpec% /u /c set /p "=%_var:"= %"^<nul^|find /v /c &...
Io-oI's user avatar
  • 8,243
1 vote

Getting Two Character Variable Without Pressing Enter (PowerShell/ CMD)

With batch, you may only ask for a string and then truncate it to two characters. The user will need to press Enter to finish entering the string. I assume that you would like for the script to ...
harrymc's user avatar
  • 481k
0 votes

Entering cmd in windows 10 file explorer nothing happens

I had a similar issue. Here are a few things to try. Make sure there is no cmd.exe file on your desktop. cmd.exe should only be located in Windows\System32. Check the permissions of Windows\System32\...
UCYT5040's user avatar
  • 101
0 votes

why is cmd taking like 20 seconds to open up in windows 11?

As booting in Safe Mode fixed the problem, this indicates that some third-party application is responsible for it. To locate the problematic application, you may use the free tool Autoruns for Windows....
harrymc's user avatar
  • 481k
1 vote
Accepted

Get String Length - How It Works

How can the script get len value when it is not in the nums list? len is passed into the function when it is called using: Call :$len str len The functions sets it using this code snippet: set %~2=%c%...
DavidPostill's user avatar
  • 157k
-1 votes

Moving Multiple Specific Files Up One Directory

$parent = "C:\temp\Parent Folder" if ( !(test-path $parent) ) { exit 0 } $lists = @('orange.jpg') $tomoved = gci $parent -recurse -force | ? {$_.Name -in $lists} if ( !$tomoved ) { exit 0 } ...
Mr.Key7's user avatar
  • 175
1 vote

In there some description of all the command processor macros in Windows?

Those are environment variables. In Command Prompt (cmd.exe) you just type in set and hit enter. In Powershell you can use get-item env: or ls env:
squillman's user avatar
  • 7,776
0 votes

How to make Command prompt on Windows 10 to display files and folders in different colours?

A good start is lsd which can be installed with winget install --id lsd-rs.lsd Then to make your command prompt more colorful overall there two things you can use: clink (winget install clink) ...
albert doh's user avatar
1 vote
Accepted

Get TimeZone Display Name Batch Script

This code works for me : @echo off for /f "tokens=2 delims=)" %%a in ('wmic timezone get caption /value') do set tzone=%%a echo %tzone:~1,99% The timezone value looks like (UTC+xx) yyy zzz. ...
harrymc's user avatar
  • 481k
0 votes

Additional options to print to pdf using chrome headless options

Time changes abilities so modern Windows includes Chromium based MS Edge without needing to install a duplicate Chrome suite. And over time many switches have been updated or changed. So disabling the ...
K J's user avatar
  • 670
2 votes
Accepted

Convert Output Date to Another Format

This .bat file will extract the date parts : @echo off setlocal enabledelayedexpansion set "months= 1:Jan 2:Feb 3:Mar 4:Apr 5:May 6:Jun 7:Jul 8:Aug 9:Sep 10:Oct 11:Nov 12:Dec" for /f "...
harrymc's user avatar
  • 481k
2 votes

Convert Output Date to Another Format

I often find myself wanting to have the current date and/or time available within a batch file, broken down into its individual components. A typical example is when I want to create an output file ...
Doug Deden's user avatar
  • 2,044
0 votes

cmd // stop service ONLY if it's running, otherwise declare a variable

not the most optimal solution, but it's a way to go (beta version) basically I wanted to delete C:\Windows\SoftwareDistribution folder created 2 variables (svc_1, svc_2) to check is BITS/wuauser are ...
superfractal's user avatar
2 votes

Set Variable in one line Batch

You can do this without using Delayed Expansion (without "setlocal enabledelayedexpansion"), by using a "for" (loop) with "call". @echo off set "var1=" & ...
Kevin Fegan's user avatar
  • 4,887
3 votes
Accepted

Set Variable in one line Batch

Why doesn't set var2 work if in one line with set var1? As harrymc points out: The variable substitution is done on the whole command-line before the line is executed. At that time, the var1 variable ...
DavidPostill's user avatar
  • 157k
3 votes

Set Variable in one line Batch

The variable substitution is done on the whole command-line before the line is executed. At that time, the var1 variable is not yet defined. The rules for an undefined variable substitution are: If ...
harrymc's user avatar
  • 481k
2 votes
Accepted

Get some spaces in a Batch

It's also possible in batch, but the code is much less elegant than in bash. Here is an example : @echo off setlocal enabledelayedexpansion set "char= " set number=5 set result= for /l %%a ...
harrymc's user avatar
  • 481k
2 votes
Accepted

Get Highest Value from String Batch File

The following batch code will find the highest integer : @echo off setlocal enabledelayedexpansion set list=4 8 1 set max= for %%a in (%list%) do ( if not defined max (Set max=%%a) if %%a GTR !...
harrymc's user avatar
  • 481k
1 vote

CMD/Powershell looking for "curl" executable in the wrong location despite PATH

As pointed out in the comments, it is indeed running curl in the correct location, but the executable is a Chocolatey shim which tries to defer to another one. For some reason I had manually replaced ...
Syst3ms's user avatar
  • 11
1 vote
Accepted

How do I show parts of a string?

How do I echo CDEF? You are using the correct expression, you just need to change it a little: echo !keys:~2,5! Should do the trick. Start at 2 and end at 5. Further Reading An A-Z Index of the ...
DavidPostill's user avatar
  • 157k
1 vote
Accepted

How do I delete files with "- copy" in the name using wildcards?

What's happening at the moment is that your *- copy.* is being parsed as two space-separated arguments: *- and copy.*. Once you see this, it's straightforward to recognise that second of these will ...
Chris Davies's user avatar
  • 3,833
0 votes

How do I delete files with "- copy" in the name using wildcards?

Have a look at a useful free tool - Bulk Renaming Utility. https://www.bulkrenameutility.co.uk It can recursively scan and preview bulk renaming tasks and is extremely useful as it has a preview. Be ...
JohnnyVegas's user avatar
  • 3,508
0 votes

How can I echo the actual logged in user if running batch as administrator?

Use the command query user. It will show all user sessions with their user names, like this :
harrymc's user avatar
  • 481k
0 votes

Add confirmation after executing ROBOCOPY command

Asking for a return entry is done using the SET command. A simple test example is: @echo Off set /p "ans=are you sure y/n - " if "%ans" equ "y" ( echo you said y ) ...
harrymc's user avatar
  • 481k
0 votes

How do I delete files with "- copy" in the name using wildcards?

The .bat file doesn't need to be in the same folder as the files that are being deleted. You may run it from another folder by running \path\copy.bat. If more files are affected in addition to copy....
harrymc's user avatar
  • 481k
0 votes

How to get the directory path where a Batch file is called from?

He was looking to determine the directory where the batch file was called, not the directory of the batch file. your answer is like %CD%, not valid in this case.
Carlos V.'s user avatar
0 votes

How can I echo the actual logged in user if running batch as administrator?

If you make your own user an administrator, and you right-click run as admin, and then provide your user's credential, then %username% will contain the correct user. If that is really not possible, ...
LPChip's user avatar
  • 61.5k
1 vote
Accepted

cmd.exe ignoring /t color when using /c instead of /k

I haven't seen any mention in Microsoft's documentation that /t is not supported by /c After looking at the source code, it seems that this combination is deliberately not supported, even if that's ...
u1686_grawity's user avatar
1 vote

Add confirmation after executing ROBOCOPY command

What you want is not directly possible. What you CAN do is write a script that takes your input, presents the confirmation prompt, and then runs robocopy to complete the actions. PowerShell can do ...
music2myear's user avatar
  • 41.8k
0 votes

List all the serial ports (available and busy) on any of windows terminals

To list all serial ports on a Windows system, whether they're available or busy, one can use this PowerShell script. This script enhances the functionality mentioned from both CMD and PowerShell, ...
Foad's user avatar
  • 626

Top 50 recent answers are included