我通常通过给出批处理命令%~dp0来组合多个csv文件来获取当前工作目录。 但是我在获取当前工作目录时遇到了&符号(&)符号,这使得'&'后批处理文件中断了说&之后的路径不被视为内部或外部命令。
你们中的任何人都可以帮我修改我的下面的脚本来识别&并用^&替换它(因为这可以逃避&并运行批处理文件)。您的任何建议都表示赞赏;
这是我的代码:
@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
答案 0 :(得分:1)
在%~dp0
附近加上引号应该足够了:
...
pushd "%~dp0"
...
是的,我不明白你使用pushd
的原因。您不必在代码中的任何位置popd
,因此pushd
似乎毫无用处。如果我正确理解ECHO Set working directory
,您应该将pushd
替换为CD %~dp0
。