gpt4 book ai didi

mysql - 批处理文件没有将结果写入所需的目录?

转载 作者:行者123 更新时间:2023-11-29 07:21:56 25 4
gpt4 key购买 nike

我有以下 这非常接近于只解决一个小问题。

.sql 结果文件写在所需文件夹之外。批处理文件正确创建文件夹和文件,但在创建 .sql 结果文件时无法更改到所需目录。

例如创建D:\Backups\MySQL\MyDatabase__04-27-2018-14-22,写入文件MyDatabase__04-27-2018-14-22.sql MySQL 文件夹中,而不是在具有完全相同名称的文件夹中。

我尝试了多种更改目录的方法,但均未成功。

set dt=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%
set dt=%dt: =0%

if exist "D:\Backups\" (mkdir D:\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
CD /D D:\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="D:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else if exist "H:\Backups\" (mkdir H:\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
H:
CD H:\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="H:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else if exist "G:\Backups\" (mkdir G:\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
G:
CD G:\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="G:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else mkdir C:\Users\Scotty\Desktop\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
C:
CD C:\Users\Scotty\Desktop\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="C:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
CD..
CD..
CD C:\Users\Scotty\Desktop\BackupBatchFiles\

实际结果是 结果文件放置在以相同名称创建的目录之外。我希望将 .sql 结果文件写入批处理文件创建的同名目录中。

最佳答案

嗯,所有带有 mysqldump.exe 的行都指定参数 --result-file with

Backups\MySQL\MyDatabase_%dt%.sql

代替

Backups\MySQL\MyDatabase_%dt%\MyDatabase_%dt%.sql

我建议下面的批处理文件可以避免重复代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DateTime=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%"
set "DateTime=%DateTime: =0%"
set "DoLocalBackup="

for %%I in (D H G) do if exist "%%I:\Backups\" set "BackupFolder=%%I:\Backups\MySQL\MyDatabase__%DateTime%" & goto MakeBackup

echo ERROR: Failed to find network backup drive.

:LocalBackup
set "BackupFolder=%UserProfile%\Desktop\Backups\MySQL\MyDatabase__%DateTime%"
set "DoLocalBackup=1"

:MakeBackup
mkdir "%BackupFolder%" 2>nul
if exist "%BackupFolder%\" (
C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="%BackupFolder%\MyDatabase_%DateTime%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else (
echo ERROR: Failed to create backup folder: "%BackupFolder%"
)
if not defined DoLocalBackup goto LocalBackup

endlocal
CD /D "%UserProfile%\Desktop\BackupBatchFiles\"

此批处理文件在网络驱动器上第一个找到的备份文件夹中创建一个备份,并在当前用户桌面目录的子目录中另外创建一个备份。

另一个解决方案甚至更简单,因为它只是从四个可能的备份位置之一中选择一个作为有问题的批处理文件代码。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DateTime=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%"
set "DateTime=%DateTime: =0%"

for %%I in (D: H: G: "%UserProfile%\Desktop") do if exist "%%~I\Backups\" set "BackupFolder=%%~I\Backups\MySQL\MyDatabase__%DateTime%" & goto MakeBackup

echo ERROR: Failed to find network backup drive or local backup folder.
goto EndBackup

:MakeBackup
mkdir "%BackupFolder%" 2>nul
if exist "%BackupFolder%\" (
C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="%BackupFolder%\MyDatabase_%DateTime%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else (
echo ERROR: Failed to create backup folder: "%BackupFolder%"
)

:EndBackup
endlocal
CD /D "%UserProfile%\Desktop\BackupBatchFiles\"

这里还有第三种解决方案,与第二种解决方案类似,但如果在指定的可能备份存储位置创建备份文件夹失败,则继续FOR循环。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DateTime=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%"
set "DateTime=%DateTime: =0%"
set "BackupFolder=Backups\MySQL\MyDatabase__%DateTime%"

for %%I in (D: H: G: "%UserProfile%\Desktop") do if exist "%%~I\Backups\" (
mkdir "%%~I\%BackupFolder%" 2>nul
if exist "%%~I\%BackupFolder%\" (
C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="%%~I\%BackupFolder%\MyDatabase_%DateTime%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
goto EndBackup
) else echo ERROR: Failed to create backup folder: "%%~I\%BackupFolder%"
)
echo ERROR: Failed to find any specified backup folder or to create the
echo current date backup folder in one of the backup folders.

:EndBackup
endlocal
CD /D "%UserProfile%\Desktop\BackupBatchFiles\"

此外,我建议将第三行更改为:

set "DateTime=%date:~-4,4%-%date:~-7,2%-%date:~-10,2%-%time:~0,2%-%time:~3,2%"

这导致国际日期格式 yyyy-MM-dd 优于 dd-MM-yyyy 因为按字母顺序排序的目录/文件同时排序使用国际日期格式按时间顺序排列。

注意:第三行中引用的环境变量 DATETIME 的日期/时间字符串的日期/时间格式取决于为运行此批处理文件的帐户配置了哪个国家/地区。例如,请参阅 Why does %date% produce a different result in batch file executed as scheduled task? 以了解以特定格式获取当前日期/时间的区域独立解决方案。

要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • cd/?
  • echo/?
  • endlocal/?
  • for/?
  • goto/?
  • 如果/?
  • mkdir/?md/?
  • 设置/?
  • setlocal/?

关于mysql - 批处理文件没有将结果写入所需的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com