windows cmd memo

@echo off 
rem a command that instructs the Command Prompt to stop displaying the commands while the script is running. 

command1 & command2
rem In this case, command1 is executed first, and then command2 is executed, 
rem regardless of the exit code of command1. 
rem Both commands will run, even if command1 encounters an error.

command1 && command2
rem In this case, command1 is executed first. 
rem If command1 completes successfully (exit code 0), command2 is executed. 
rem However, if command1 fails with a non-zero exit code, command2 is not executed.

command1 || command2
rem In this case, command1 is executed first. 
rem If command1 fails (non-zero exit code), command2 is executed. 
rem However, if command1 succeeds with an exit code of 0, command2 is not executed.

command1 | command2
rem In this case, the output of command1 is redirected as input to command2. 
rem The output of command1 is sent through the pipe (|) and becomes the input for command2. 
rem This allows you to perform operations on the output of one command using another command.


define and call a function AND use timeout to sleep and cmd /k command

In Windows command prompt (CMD), the /k option is used to execute a command and keep the command prompt window open afterward. It is often used to run a specific command or program while retaining the command prompt for further interactions or viewing the output.

@echo off
echo %3
echo %4
call :func1
exit 0

:func1
rem the next code will be runned after 3 seconds
timeout 3  
echo %1
echo %2
exit /b 0

rem do not call func1 here,otherwise it will be called recursively
rem call :func1 

cmd /k a.bat a1 a2 a3 a4
output:

a3
a4
a1
a2

define and call a function passing parameter and pause command

  • The pause command is used to pause the script’s execution, allowing the user to view the output before the console window closes.Execution will be paused until user press some key
  • %~dp0 is a special syntax that represents the drive and path of the currently executing batch file.
  • %cd%:current directory
@echo off

call :func2 3,5
call :func3
echo %~dp0
pause
exit 0

:func2
  for /L %%i in (%1,1,%2) do (
    echo This is iteration number %%i
  )
exit /b 0

:func3
  echo func3
exit /b 0

This is iteration number 3
This is iteration number 4
This is iteration number 5
func3
C:****\src\main\bin\

using variables defined in other bash file

rem b.bat
set xx=6
@set yy=7
rem a.bat
@echo off
call b.bat
echo %xx%
echo %yy%
exit 0

cmd /k a.bat
output:

6
7

set a variable within a loop or a block

using setlocal enabledelayedexpansion and !variable! can change value of variable in loop

@echo off
setlocal enabledelayedexpansion
set "filename=b.bat"
set "content="

for /f "delims=" %%A in (%filename%) do (
    set "content=!content!%%A"
    echo inner !content!
)

echo The content of the file is: %content%

set "content=%content%xxx"
set "content=%content%yyy"
echo The content of the file is: %content%

endlocal
exit 0

output

inner set xx=6
inner set xx=6@set yy=7
The content of the file is: set xx=6@set yy=7
The content of the file is: set xx=6@set yy=7xxxyyy
The endlocal command is used in Windows batch scripting to end the localization of environment variables and restore the previous environment state. It is typically used in conjunction with the setlocal command to define a specific scope for environment variable changes.

When you use the setlocal command,
it creates a new local environment scope where any changes to environment variables are confined to that scope.
This means that modifications made to variables or their values within the scope of setlocal will not affect the variables outside of that scope.

The setlocal command is used at the beginning to initiate a local scope,
and the endlocal command is used at the end to mark the end of that local scope and restore the previous environment state.
This ensures that any changes made to variables within the local scope do not persist beyond that scope.

By placing endlocal at the end, it ensures that any changes made within the local scope,
such as the modifications to the content variable using delayed variable
expansion, do not persist beyond the local scope.

exit 0 is recommend if ur bat file executed successfully

loop through .bat files in current path and assign filename to variable

@echo off

rem output is: a.bat b.bat c.txt
for /f "usebackq" %%i in (`dir /b `) do (echo %%i)

setlocal enabledelayedexpansion
rem output is: a.bat b.bat
for /f "usebackq" %%i in (`dir /b *.bat`) do (set x=%%i && echo !x!)
rem output is: b.bat
echo %x%
endlocal
exit 0

“usebackq” option allows the usage of backticks to enclose the command or file path in the “for /f” loop, providing more flexibility in handling special characters or spaces in the input.

In a batch script, when you use a for loop to iterate over values,
you need to use double percent signs (%%i) as the loop variable if you are executing the script from a command prompt or a batch file.
In summary , within a batch script, use %%i as the loop variable,
and when executing the command directly from the command prompt, use %i.

rem echo file or directory whose name start with "aa"
for /f "usebackq" %i in (`dir /b aa*`) do echo %i

iterate through rows of a file

@echo off 
FOR /F "tokens=*" %%g IN ('type b.bat') do (SET ttt=%%g && echo %%g) 
echo %ttt% 

loop some times

@echo off
for /L %%i in (2,1,4) do (
    echo This is iteration number %%i
)

output:

This is iteration number 2
This is iteration number 3
This is iteration number 4

setx

The setx command sets the environment variable in both the current session and the user or system environment variables, depending on the scope. Keep in mind that changes to the system environment variables require administrative privileges.

After running the setx command, you will need to open a new command prompt session for the environment variable to take effect.

setx VARIABLE_NAME value

To remove an environment variable in the Windows command prompt (cmd.exe), you can use the setx command with an empty value. Here’s how you can remove an environment variable:

setx VARIABLE_NAME ""

delete a directory without waiting for confirmation

rem remove directory a who has subdirectories and files , after running it, Y/N needs to be inputed
rmdir /s a

rem remove directory a who has subdirectories and files
echo Y|rmdir /s  a

In the command echo Y|rmdir /s directory, the echo Y part generates the input “Y” and pipes it (|) as input to the rmdir /s command.

The rmdir /s command is used to remove a directory and its contents recursively. By default, when you use the rmdir command to delete a directory, it prompts for confirmation from the user. However, when the input “Y” is piped as input, it simulates the user pressing “Y” to confirm the deletion, thereby bypassing the prompt for confirmation.

So, when the command echo Y|rmdir /s directory is executed, it effectively sends the “Y” character as input to the rmdir command, which confirms the deletion without requiring the user to manually type “Y” in response to the confirmation prompt.

copy a directory without waiting for confirmation

rem copy a to b , if file b or directory b does not exist, F/D needs to be inputed
XCOPY a b /s /e /y

rem echo D|XCOPY a b /s /e /y copies the contents of directory a (including subdirectories and empty directories) to directory b, 
rem overwriting existing files without prompting for confirmation.
echo D|XCOPY a b /s /e /y
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值