如何从批处理文件中的当前工作目录%〜dp0中逃离&符号(&) [英] How do I escape Ampersand (&) from current working directory %~dp0 in batch file

查看:51
本文介绍了如何从批处理文件中的当前工作目录%〜dp0中逃离&符号(&)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常通过给出批处理命令%〜dp0来合并多个csv文件,从而获得当前的工作目录.但是我在获取当前工作目录时遇到了&符号,这使得批处理文件在&"之后损坏了.说出&之后的路径不被识别为内部或外部命令.

I usually get the current working directory by giving the batch command %~dp0 for combine multiple csv files. But I encountered ampersand (&) symbol while getting current working directories which makes batch file broke after the '&' saying the path after & is not recognized as an internal or external command.

你们中的任何一个都可以帮助我修改下面的脚本来识别&并将其替换为^&(因为这可以转义&并运行批处理文件).您的任何建议都会受到赞赏;

Can any of you guys please help me in modifying my below script in identifying & and replacing it with ^& (as this can escape the & and run the batch file). Any of your suggestions is appreciated;

这是我的代码:

@echo off

ECHO Set working directory
pushd %~dp0             - How to escape & in this line???

ECHO Deleting existing combined file
del combined.csv

setlocal ENABLEDELAYEDEXPANSION

REM set count to 1
set cnt=1

REM for each file that matches *.csv

for %%i in (*.csv) do (

REM if count is 1 it's the first time running

  if !cnt!==1 (

REM push the entire file complete with header into combined.csv - this will also create combined.csv

for /f "delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv

REM otherwise, make sure we're not working with the combined file and
  ) else if %%i NEQ combined.csv (

REM push the file without the header into combined.csv
    for /f "skip=1 delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv
  )

REM increment count by 1
  set /a cnt+=1
)

cmd \k

推荐答案

%〜dp0 周围加上引号就足够了:

Putting quotation marks around %~dp0 should be enough:

...
pushd "%~dp0"
...

顺便说一句,我不明白您为什么使用 pushd .您不会在代码中的任何地方 pop ,因此 pushd 似乎毫无用处.如果我正确理解 ECHO Set工作目录,则应将 pushd 替换为 CD%〜dp0 .

Btw, I don't understand why you use pushd. You don't popd anywhere in your code so pushd seems useless. If I understand ECHO Set working directory correctly, you should replace pushd with CD %~dp0.

这篇关于如何从批处理文件中的当前工作目录%〜dp0中逃离&符号(&)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆