[NT Batch]如何从用户输入的文件中获取目录?

时间:2023-01-18 02:23:51

I need to know how to extract directory information from user inputted file, consider this code as example:

我需要知道如何从用户输入的文件中提取目录信息,请将此代码视为示例:


ECHO Drag and drop your .txt file here, after that press Enter:
SET txtfile=
SET /P txtfile=
ECHO.
CD %txtfile%

ofcourse that didn't work since i didn't extract filepath from %txtfile% and here the sample output i want:

因为我没有从%txtfile%中提取文件路径而且这里我想要的示例输出,因此无法正常工作:

C:\>Drag and drop your .txt file here, after that press Enter:
C:\somefolder\somesubfolder\somefile.txt
C:\>Press Enter to continue...

C:\somefolder\somesubfolder\>

notice it have change it working directory

注意它已经改变了它的工作目录

3 个解决方案

#1


You can extract the full path as follows:

您可以按如下方式提取完整路径:

@echo off
setlocal
echo Drag and drop your .txt file here, after that press Enter:
set txtfile=
set /p txtfile=
echo.
for %%i in (%txtfile%) do set txtdir=%%~dpi
for %%i in (%txtfile%) do set txtfil=%%~nxi
cd /d %txtdir%
dir %txtfil%
endlocal

The first for statement gets the drive and path, the second gets the filename and extension. I've used cd /d to change the drive and directory and just used setlocal/endlocal to preserve my path outside the script (you can remove these if you don't care).

第一个for语句获取驱动器和路径,第二个获取文件名和扩展名。我已经使用cd / d来更改驱动器和目录,只是使用了setlocal / endlocal来保留我在脚本之外的路径(如果你不在乎,可以删除它们)。

The full range of ~-modifiers can be found by running "for /?" in a command window. It really is a powerful command, and these modifiers aren't restricted to "for", they can be used on any %1-type arguments to scripts as well.

通过运行“for /?”可以找到全范围的〜-modifiers在命令窗口中。它确实是一个强大的命令,并且这些修饰符不限于“for”,它们也可以用于任何%1类型的脚本参数。

#2


ECHO Drag and drop your .txt file here, after that press Enter:
SET txtfile=
SET /P txtfile=
ECHO.
CD %txtfile%\..

I don't really know why, but this works in XP, could work in NT also.

我真的不知道为什么,但这在XP中工作,也可以在NT工作。

#3


The answer to this is used by taking what one person said and modifying it...

对此的回答是通过采取一个人所说的并修改它来使用的......

paxdiablo was on the right path, however that is not copy/pasteable. For his to work properly (and maybe it's just for me running Windows7) you need 2 files.

paxdiablo是在正确的道路上,但不是复制/粘贴。为了让他正常工作(也许只是为了我运行Windows7)你需要2个文件。

The first file: drag_drop.bat

第一个文件:drag_drop.bat

@echo off
echo Drag and drop your .txt file here, after that press Enter:
set txtfile=
set /p txtfile=
echo.%txtfile%
call c:\temp\process_filename.bat %txtfile%

The second file: process_filename.bat

第二个文件:process_filename.bat

FOR %%i in (%txtfile%) do set txtdir=%~dp1
cmd /K "cd %txtdir%"

The reason I had to use 2 files is because the trigger for %~dp1 (which the syntax was wrong from paxdiablo - no offense I know you have 187k rep and I give you props for that [you had %%~dpi, %% is used in the echo to disable the special character '%' and dp1 is the delim that allows you to strip off the quotes, filepath from the filename - the same thing goes with %%~nxi] )

我必须使用2个文件的原因是因为%~dp1的触发器(paxdiablo的语法错误 - 没有冒犯我知道你有187k代表我给你道具[你有%% ~dpi,%%在echo中用于禁用特殊字符'%',而dp1是允许你从文件名中删除引号,文件路径的delim - 与%% ~nxi相同的东西])

Anyhow, you need to call the batch file passing the other filename with it. This is where the second one comes in. This one strips the information necessary and then allows you to access that path and then opens that directory in your cmd prompt.

无论如何,你需要调用批处理文件传递其他文件名。这是第二个进入的地方。这个删除了必要的信息,然后允许您访问该路径,然后在cmd提示符中打开该目录。

ALTERNATIVELY

You can do this from the same file...

你可以从同一个文件中做到这一点......

@echo off
setlocal
IF '%process%'=='1' goto processFile
echo Drag and drop your .txt file here, after that press Enter:
set txtfile=
set /p txtfile=
echo.%txtfile%
set process=1
call c:\temp\dragdrop.bat %txtfile%

:processFile
set txtdir=
FOR %%1 in (%txtfile%) do set txtdir=%~dp1
cmd /K "cd %txtdir%"
endlocal

#1


You can extract the full path as follows:

您可以按如下方式提取完整路径:

@echo off
setlocal
echo Drag and drop your .txt file here, after that press Enter:
set txtfile=
set /p txtfile=
echo.
for %%i in (%txtfile%) do set txtdir=%%~dpi
for %%i in (%txtfile%) do set txtfil=%%~nxi
cd /d %txtdir%
dir %txtfil%
endlocal

The first for statement gets the drive and path, the second gets the filename and extension. I've used cd /d to change the drive and directory and just used setlocal/endlocal to preserve my path outside the script (you can remove these if you don't care).

第一个for语句获取驱动器和路径,第二个获取文件名和扩展名。我已经使用cd / d来更改驱动器和目录,只是使用了setlocal / endlocal来保留我在脚本之外的路径(如果你不在乎,可以删除它们)。

The full range of ~-modifiers can be found by running "for /?" in a command window. It really is a powerful command, and these modifiers aren't restricted to "for", they can be used on any %1-type arguments to scripts as well.

通过运行“for /?”可以找到全范围的〜-modifiers在命令窗口中。它确实是一个强大的命令,并且这些修饰符不限于“for”,它们也可以用于任何%1类型的脚本参数。

#2


ECHO Drag and drop your .txt file here, after that press Enter:
SET txtfile=
SET /P txtfile=
ECHO.
CD %txtfile%\..

I don't really know why, but this works in XP, could work in NT also.

我真的不知道为什么,但这在XP中工作,也可以在NT工作。

#3


The answer to this is used by taking what one person said and modifying it...

对此的回答是通过采取一个人所说的并修改它来使用的......

paxdiablo was on the right path, however that is not copy/pasteable. For his to work properly (and maybe it's just for me running Windows7) you need 2 files.

paxdiablo是在正确的道路上,但不是复制/粘贴。为了让他正常工作(也许只是为了我运行Windows7)你需要2个文件。

The first file: drag_drop.bat

第一个文件:drag_drop.bat

@echo off
echo Drag and drop your .txt file here, after that press Enter:
set txtfile=
set /p txtfile=
echo.%txtfile%
call c:\temp\process_filename.bat %txtfile%

The second file: process_filename.bat

第二个文件:process_filename.bat

FOR %%i in (%txtfile%) do set txtdir=%~dp1
cmd /K "cd %txtdir%"

The reason I had to use 2 files is because the trigger for %~dp1 (which the syntax was wrong from paxdiablo - no offense I know you have 187k rep and I give you props for that [you had %%~dpi, %% is used in the echo to disable the special character '%' and dp1 is the delim that allows you to strip off the quotes, filepath from the filename - the same thing goes with %%~nxi] )

我必须使用2个文件的原因是因为%~dp1的触发器(paxdiablo的语法错误 - 没有冒犯我知道你有187k代表我给你道具[你有%% ~dpi,%%在echo中用于禁用特殊字符'%',而dp1是允许你从文件名中删除引号,文件路径的delim - 与%% ~nxi相同的东西])

Anyhow, you need to call the batch file passing the other filename with it. This is where the second one comes in. This one strips the information necessary and then allows you to access that path and then opens that directory in your cmd prompt.

无论如何,你需要调用批处理文件传递其他文件名。这是第二个进入的地方。这个删除了必要的信息,然后允许您访问该路径,然后在cmd提示符中打开该目录。

ALTERNATIVELY

You can do this from the same file...

你可以从同一个文件中做到这一点......

@echo off
setlocal
IF '%process%'=='1' goto processFile
echo Drag and drop your .txt file here, after that press Enter:
set txtfile=
set /p txtfile=
echo.%txtfile%
set process=1
call c:\temp\dragdrop.bat %txtfile%

:processFile
set txtdir=
FOR %%1 in (%txtfile%) do set txtdir=%~dp1
cmd /K "cd %txtdir%"
endlocal