需要解析批处理文件所在的当前目录我可以在所述批处理文件中使用
问题描述:
在Windows计算机上使用rsync(cWrsync),我正在将一个包含子目录的大文件夹同步到一个Web服务器,几分钟才能完成。但是,当用户只需要将文件添加到子目录时,我不希望他们必须等待一个小时才能同步该目录。需要解析批处理文件所在的当前目录我可以在所述批处理文件中使用
我的想法 将文件添加到每个子目录(1_sync.bat)中,当它们添加或删除该目录中的文件时,可以执行该文件。我需要的批处理文件,以便能够动态地让rsync同步的目录..这里是静态版本:
@echo off
REM Make environment variable changes local to this batch file
SETLOCAL
REM where ti find rsync and related files
SET CWRSYNCHOME=C:\cwRsync
SET HOME=C:\Users\greg\AppData\Roaming
SET CWORLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
"C:\cwRsync\bin\rsync.exe" -v -e 'ssh -i C:\home\greg\.ssh\id_rsa' --delete --recursive --inplace "/cygdrive/z/1CustomerDocs/2017/Client Folder/" "[email protected]:/var/storage/customer_files/2017/Client\ Folder/"
在上面的例子中,我想有客户端文件夹是会检测一下文件夹的变量批处理脚本实际上是在我可以通过每个子目录中的一个bat文件。
我尝试了%〜dp0这几乎没有技巧,但输出整个路径..我只需要最后两个目录。
所以如果%〜DP0 = \服务器的路径\内容\ 1CustomerDocs \ 2017年\客户端文件夹\
我希望我能砍的最后两个目录关闭,有一个变量,它看起来像
2017年/客户端文件夹(也需要一个逃脱用于Linux的空格)
所以最终的结果会是什么样子
@echo off
REM Make environment variable changes local to this batch file
SETLOCAL
REM where ti find rsync and related files
SET CWRSYNCHOME=C:\cwRsync
SET HOME=C:\Users\greg\AppData\Roaming
SET CWORLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
SET CUST_FOLDER_WINDOWS=*YOUR HELP NEEDED HERE*
SET CUST_FOLDER_LINUX=*YOUR HELP NEEDED HERE*
"C:\cwRsync\bin\rsync.exe" -v -e 'ssh -i C:\home\greg\.ssh\id_rsa' --delete --recursive --inplace "/cygdrive/z/1CustomerDocs/%CUST_FOLDER_WINDOWS%" "[email protected]:/var/storage/customer_files/%CUST_FOLDER_LINUX%"
再次,我需要在Linux FO逃避空间。
感谢您的帮助!
答
想通了如何下面
@echo off
REM Make environment variable changes local to this batch file
SETLOCAL
REM where ti find rsync and related files
SET CWRSYNCHOME=C:\cwRsync
SET HOME=C:\Users\greg\AppData\Roaming
SET CWORLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
for /F "tokens=5,6 delims=\" %%a in ("%0") do (
"C:\cwRsync\bin\rsync.exe" -v -e 'ssh -i C:\home\greg\.ssh\id_rsa' --delete --
recursive --inplace "/cygdrive/z/1CustomerDocs/%%a/%%b/"
"[email protected]:/var/storage/customer_files/%%a/%%b/"
)
答
要独立于当前dirlevel:
@echo off
Echo current dir %CD%
Echo Batch dir %~dp0
for %%a in ("%~dp0.") Do Set "Parent=%%~nxa"
for %%a in ("%~dp0..") Do Set "Grandparent=%%~nxa"
Echo Last 2 dirs \%Grandparent%\%Parent%
current dir Q:\Test\2017\08\10
Batch dir Q:\Test\2017\08\10\
Last 2 dirs \08\10