【问题标题】:Get the filename of a running batch file获取正在运行的批处理文件的文件名
【发布时间】:2014-11-24 09:16:42
【问题描述】:
我正在尝试制作一个批处理文件,该文件将写入我们为其命名的名称。
例如,如果我将文件命名为“test1”并启动它,它只会将标题更改为“test1” 如果我将名称更改为“bat file”,它将在标题中显示“bat file”。
是否可以创建一个文件,该文件将写入与 bat 文件同名的 .txt 文件?我正在尝试制作一个批处理文件,该文件将记录一些内容并将其写入“.txt”,其中 bat 文件的名称是不带扩展名的
【问题讨论】:
标签:
windows
batch-file
cmd
filenames
【解决方案1】:
在批处理文件中%0 存储对当前批处理文件的引用。值的确切格式取决于文件的调用方式,但有一系列修饰符可用于检索所需信息(您可以查看文档或执行for /?,因为此命令的帮助还包括修饰符的完整列表)
@echo off
title %~n0
echo I am %~n0
> "%~dpn0.txt" echo This goes to the file
在哪里
%~n0 = name of the current batch file (without extension)
%~dpn0 = drive, path and file name (without extension) of the current batch file
【解决方案2】:
echo %~f0 用于写入批处理文件名。使用“>”将任何内容写入文件。例如使用 echo "write this" > file.txt