我正在寻找一种方法让批处理文件在执行后将自身移动到已知位置。自移动 - 似乎是最恰当的名称,但我确信有一个技术术语。我想在所有其他代码运行后移动批处理文件。
move "C:\temp\move_me.bat" "D:\temp\move_me.bat"
其中 move_me.bat 是放置在 c:\temp 中的同名批处理文件,将被移动到 d:\temp。
我得到了错误
The batch file cannot be found.
即使批处理文件移动了位置。所以我有点困惑。我需要抑制错误还是最好复制批处理文件并删除源代码?或者有更好的方法吗?
最佳答案
Windows 命令提示符 cmd 不会在内存中缓存批处理文件,它会逐行从文件中读取它们1。因此,您会在 move 命令完成后立即收到错误消息,因为无法再找到该文件。
您可以像这样调用批处理文件来抑制错误:
"C:\temp\move_me.bat" 2> nul
但这也无意中抑制了所有其他错误消息。
无论如何,也许以下方法对您有用——这是脚本 C:\temp\move_me.bat:
if /I "%~dp0"=="D:\temp\" exit /B
rem // (some other code here...)
copy "%~f0" "D:\temp\%~nx0"
"D:\temp\%~nx0" & del "%~f0"
首先,根据 D:\temp\ 检查当前运行的批处理文件的位置;如果相等,则批处理文件立即终止。
最后,原始批处理文件(由 %~f0 2 访问)被复制(未移动)到新位置 D:\temp(文件名, %~nx0 ,保持不变)。
下一行从新位置运行批处理文件,但不使用 call ,这是返回调用批处理脚本所必需的,但这不是我们想要的。 & operator让下一个命令在前一个命令完成时执行。尽管未使用call,但由于整行已被cmd 读取和解析,因此仍将执行下一条命令。但是执行控制现在位于批处理文件的新实例中,因此错误消息 The batch file cannot be found. 不再出现。
前面提到的 if 查询会立即退出批处理文件副本的执行,因此它的其他代码不会运行两次。
如果您不想跳过复制的批处理文件的执行,请删除 if 命令行并修改 copy 命令行以获取此命令:
rem // (some other code here...)
copy "%~f0" "D:\temp\%~nx0" > nul || exit /B
"D:\temp\%~nx0" & del "%~f0"
> nul portion禁止显示消息(包括摘要 1 file(s) copyed.)。 || operator仅在复制失败时才执行下一条命令。因此,当执行原始批处理文件时,复制会按预期完成。当复制的批处理文件运行时,copy 会尝试将批处理文件复制到自身,这会导致消息 The file cannot be copy to itself.(被 抑制> nul) 并且在触发 exit/B 命令(由于 ||)离开批处理文件的错误中,所以最后一行不是试图被处决。
您也可以使用 move 实现相同的行为;所以相关代码如下所示:
if /I "%~dp0"=="D:\temp\" exit /B
rem // (some other code here...)
move "%~f0" "D:\temp\%~nx0" & "D:\temp\%~nx0"
或者,如果您不想为移动的脚本跳过其他代码:
rem // (some other code here...)
if /I not "%~dp0"=="D:\temp\" move "%~f0" "D:\temp\%~nx0" & "D:\temp\%~nx0"
if 查询是必需的,因为 move 与 copy 不同,如果源和目标相等,则不会返回错误。
这是一个批处理文件的全面解决方案,它可以自行移动并随后将控制权交给被移动的文件。看看所有的解释性注释,找出哪个批处理文件实例运行的代码:
@echo off
rem // Define constants here:
set "_TARGET=D:%~pnx0" & rem /* (this defines the movement destination;
rem in your situation, the original batch file is
rem `C:\temp\move_me.bat`, so the target file is
rem `D:\temp\move_me.bat` (only drive changes)) */
rem // (code that runs for both batch file instances...)
echo Batch file: "%~f0"
echo [executed by both files before the movement check]
rem // Check whether current batch file is the moved one:
if /I "%~f0"=="%_TARGET%" (
rem // (code that runs for the moved batch file instance only...)
echo Batch file: "%~f0"
echo [executed only by the moved file]
) else (
rem // (code than runs for the original batch file instance only...)
echo Batch file: "%~f0"
echo [executed only by the original file]
rem // Actually move the batch file here, then give control to the moved one:
> nul move "%~f0" "%_TARGET%"
"%_TARGET%"
rem /* (code that runs for the original batch file instance only;
rem this is run after the moved batch file has finished;
rem you must not use `goto` herein as the target label cannot be found,
rem because the original file does no longer exist at this point!) */
echo Batch file: "%~f0"
echo [executed only by the original file, but after the moved one has finished]
)
rem // (code that runs for the moved batch file instance only...)
echo Batch file: "%~f0"
echo [executed only by the moved file after the movement check]
exit /B
1) 请注意,带括号的代码块 (/) 和连续行 ^ 被视为单个命令行:
(
echo This entire parenthesised block is
echo considered as a single command line.
)
echo This continued line & ^
echo as well.
2) 请注意,一旦读取和解析命令行或 block ,就会立即解析此类参数引用,因此在实际执行之前。
关于windows - 自移动批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42861154/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信