cmd - 优化bat脚本之删除指定的目录下文件?

 

问题描述:

目前写了一个bat脚本,根据传入的参数 来删除指定architecture的安装介质,有如下两个问题

  1. 目前执行脚本以后 对应的目录确实已经删除 但是会报错 找不到指定的文件夹,提示不美观
  2. 目前是根据architcure,我想加一个参数 根据 architecture和 指定的具体组件进行删除

如redis mysql 等 请问如何优化 脚本如下 谢谢各位了

@echo off

%~d0
cd %~dp0

echo Unless you know what you are trying to do, this operation will be a dangerous experience. To clarify the architecture of the hcs product service you need to install, you can also run `uname -m` directly on the target machine to determine this.
echo[
echo 1.Select x86_64 architecture as the media for installation
echo 2.Select aarch64 architecture as the media for installation
echo[

:main
set /p archtype=Enter your option:

set arm_arch=aarch64
set x86_arch=x86_64

if %archtype% == 1 goto x86
if %archtype% == 2 goto arm64

echo Invalid option
goto main

:x86
rd /s /q "%~dp001-ams\bam\installFiles\%arm_arch%"
rd /s /q "%~dp001-ams\epass\installFiles\%arm_arch%"
rd /s /q "%~dp001-ams\logstash\installFiles\%arm_arch%"
rd /s /q  "%~dp002-ims\apphub\installFiles\%arm_arch%"
rd /s /q  "%~dp002-ims\bim\installFiles\%arm_arch%"
echo[
echo remove aarch64 files successful!
pause > nul
exit

:arm64
rd /s /q "%~dp001-ams\bam\installFiles\%x86_arch%"
rd /s /q "%~dp001-ams\epass\installFiles\%x86_arch%"
rd /s /q "%~dp001-ams\logstash\installFiles\%x86_arch%"
rd /s /q  "%~dp002-ims\apphub\installFiles\%x86_arch%"
rd /s /q  "%~dp002-ims\bim\installFiles\%x86_arch%"
echo[
echo remove x86_64 files successful!
pause > nul
exit

 

第 1 个答案:

自己优化了一波

@echo off

%~d0
cd %~dp0

echo Unless you know what you are trying to do, this operation will be a dangerous experience. To clarify the architecture of the hcs product service you need to install, you can also run `uname -m` directly on the target machine to determine this.
echo[
echo 1.Select x86_64 architecture as the media for installation
echo 2.Select aarch64 architecture as the media for installation
echo[

:main
set /p archtype=Enter your arch option(1=x86,2=arm64):
set /p dictype=Enter your dictory option(ims or ams ,if not input,that mean all):

set arm_arch=aarch64
set x86_arch=x86_64

if %archtype% == 1 goto x86
if %archtype% == 2 goto arm64

echo Invalid option
goto main

:x86
if "%dictype%"=="" (
rd /s /q "%~dp001-ams\bam\installFiles\%arm_arch%"
rd /s /q "%~dp001-ams\epass\installFiles\%arm_arch%"
rd /s /q "%~dp001-ams\logstash\installFiles\%arm_arch%"
rd /s /q  "%~dp002-ims\apphub\installFiles\%arm_arch%"
rd /s /q  "%~dp002-ims\bim\installFiles\%arm_arch%"
echo[
echo remove aarch64 files successful!
) else if "%dictype%"=="ams" (
rd /s /q "%~dp001-ams\bam\installFiles\%arm_arch%"
rd /s /q "%~dp001-ams\epass\installFiles\%arm_arch%"
rd /s /q "%~dp001-ams\logstash\installFiles\%arm_arch%"
echo[
echo remove ams files successful!
) else if "%dictype%"=="ims" (
rd /s /q  "%~dp002-ims\apphub\installFiles\%arm_arch%"
rd /s /q  "%~dp002-ims\bim\installFiles\%arm_arch%"
echo[
echo remove ims files successful!
) else (
    echo please input correct words!
)

pause > nul
exit

:arm64
if "%dictype%"=="" (
rd /s /q "%~dp001-ams\bam\installFiles\%x86_arch%"
rd /s /q "%~dp001-ams\epass\installFiles\%x86_arch%"
rd /s /q "%~dp001-ams\logstash\installFiles\%x86_arch%"
rd /s /q  "%~dp002-ims\apphub\installFiles\%x86_arch%"
rd /s /q  "%~dp002-ims\bim\installFiles\%x86_arch%"
echo[
echo remove aarch64 files successful!
) else if "%dictype%"=="ams" (
rd /s /q "%~dp001-ams\bam\installFiles\%x86_arch%"
rd /s /q "%~dp001-ams\epass\installFiles\%x86_arch%"
rd /s /q "%~dp001-ams\logstash\installFiles\%x86_arch%"
echo[
echo remove ams files successful!
) else if "%dictype%"=="ims" (
rd /s /q  "%~dp002-ims\apphub\installFiles\%x86_arch%"
rd /s /q  "%~dp002-ims\bim\installFiles\%x86_arch%"
echo[
echo remove ims files successful!
) else (
    echo please input correct words!
)
pause > nul
exit

 

第 2 个答案:

现在这个时间, 我觉得用powershell脚本.ps1比cmd好.


一般来说这个时间是不联网的吧,如果是用机器自带的计时器获取时间,那不同机器的时间会不会不一样,万一某机器的计时器坏掉了呢?特别是授时协议还是用毫秒为单位的。