在DOS批处理脚本中将文件写入相对路径

发布于 2024-10-24 10:17:48 字数 811 浏览 9 评论 0原文

参考什么是最好的如何在批处理文件中执行子字符串? 我认为下面的方法应该可行。

FOR %%f IN (*.wav) DO CALL :runthis "%%f"
rem del temp.wav tmpfile
GOTO :EOF

:runthis
set "outdir=%~p1\output\"
copy "%~1" "%outdir%%~1"

最后一个命令应该对当前目录中的所有 .wav 文件进行dosomthing,并输出到现有子目录“输出”,并保留原始文件名。知道我哪里出错了吗?

更新1:谢谢,修复了语法。我没有注意到 %pI 仅将 I 扩展为路径,没有仔细阅读。现在的问题是它用 "s 扩展

dosomething "11.wav" "\Users\t4\Desktop\Airlines\WavRepeaters\\outdir\"11.wav""

它应该类似于::

dosomething "11.wav" c:\Users\t4\Desktop\Airlines\WavRepeaters\outdir\11.wav

Update2: %~dp1 - 仅将 %1 扩展为驱动器号和路径,不带引号!

Referring to What is the best way to do a substring in a batch file? I think the below should work.

FOR %%f IN (*.wav) DO CALL :runthis "%%f"
rem del temp.wav tmpfile
GOTO :EOF

:runthis
set "outdir=%~p1\output\"
copy "%~1" "%outdir%%~1"

The last command is supposed to dosomthing to all .wav files in current directory and output to existing sub-directory "output" keeping the original filename. Any idea where I'm going wrong?

Update1: Thanks, fixed the syntax. I didn't notice that %pI expands I to path only, didn't read carefully. Now what's wrong is that it's expanded with the "s

dosomething "11.wav" "\Users\t4\Desktop\Airlines\WavRepeaters\\outdir\"11.wav""

It should be something like::

dosomething "11.wav" c:\Users\t4\Desktop\Airlines\WavRepeaters\outdir\11.wav

Update2: %~dp1 - expands %1 to a drive letter and path only, without the quotations!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2024-10-31 10:17:48

主要问题似乎是参数的使用。

:runthis 中,您使用 %~pI,但没有任何 %I
另一个陷阱是文件名中的空格,然后需要在文件名/路径周围加上引号。

经过一些改变,它应该可以工作

@echo off
FOR %%f IN (*.bat) DO CALL :runthis "%%~f"
rem del temp.wav tmpfile
GOTO :EOF

:runthis
set "outdir=%~p1\output\"
echo dosomthing "%~1" "%outdir%%~1"
goto :eof

The main problem seems to be the use of the parameters.

In :runthis you use the %~pI, but there isn't any %I.
Another pitfall are spaces in the filenames, then dosomething need quotes around the filename / path.

With some changes it should work

@echo off
FOR %%f IN (*.bat) DO CALL :runthis "%%~f"
rem del temp.wav tmpfile
GOTO :EOF

:runthis
set "outdir=%~p1\output\"
echo dosomthing "%~1" "%outdir%%~1"
goto :eof
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文